Skip to content

Commit 60d9150

Browse files
committed
multiplatform BroadcastFrameClockTest with sleep
1 parent 7b57a83 commit 60d9150

File tree

5 files changed

+96
-3
lines changed

5 files changed

+96
-3
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.compose.runtime.internal
18+
19+
actual fun isSleepAvailable() = true
20+
21+
actual fun sleep(millis: UInt) =
22+
Thread.sleep(millis.toLong())
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.compose.runtime.internal
18+
19+
actual fun isSleepAvailable() = true
20+
21+
actual fun sleep(millis: UInt) {
22+
platform.posix.usleep(millis)
23+
}

compose/runtime/runtime/src/nonEmulatorCommonTest/kotlin/androidx/compose/runtime/BroadcastFrameClockTest.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
package androidx.compose.runtime
1818

1919
import androidx.compose.runtime.internal.AtomicInt
20+
import androidx.compose.runtime.internal.isSleepAvailable
21+
import androidx.compose.runtime.internal.sleep
2022
import kotlin.test.Test
2123
import kotlin.test.assertEquals
2224
import kotlin.test.assertFalse
2325
import kotlin.test.assertTrue
26+
import kotlin.time.Duration.Companion.milliseconds
2427
import kotlinx.coroutines.CancellationException
2528
import kotlinx.coroutines.CoroutineStart.UNDISPATCHED
2629
import kotlinx.coroutines.Deferred
@@ -84,16 +87,18 @@ class BroadcastFrameClockTest {
8487
}
8588

8689
@OptIn(InternalCoroutinesApi::class)
87-
@Test(timeout = 5_000)
88-
fun locklessCancellation() = runTest {
90+
@Test
91+
fun locklessCancellation() = runTest(timeout = 5_000.milliseconds) {
92+
if (!isSleepAvailable()) return@runTest
93+
8994
val clock = BroadcastFrameClock()
9095
val cancellationGate = AtomicInt(1)
9196

9297
var spin = true
9398
async(start = UNDISPATCHED) {
9499
clock.withFrameNanos {
95100
cancellationGate.add(-1)
96-
@Suppress("BanThreadSleep") while (spin) Thread.sleep(100)
101+
while (spin) sleep(100u)
97102
}
98103
}
99104

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.compose.runtime.internal
18+
19+
internal expect fun isSleepAvailable(): Boolean
20+
21+
internal expect fun sleep(millis: UInt)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.compose.runtime.internal
18+
19+
actual fun isSleepAvailable() = false
20+
21+
actual fun sleep(millis: UInt): Unit =
22+
throw UnsupportedOperationException("Sleep is not supported")

0 commit comments

Comments
 (0)