Skip to content

Commit 2c90219

Browse files
committed
chores: tests: replace or remove thread.sleep or delay in tests
1 parent fb0fc32 commit 2c90219

File tree

7 files changed

+32
-63
lines changed

7 files changed

+32
-63
lines changed

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/common/threading/CompletionAwaiterTests.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ class CompletionAwaiterTests : FunSpec({
115115
jobs.add(thread)
116116
}
117117

118-
// Wait a bit to ensure all threads are waiting
119-
Thread.sleep(100)
120-
121118
// Complete the awaiter
122119
awaiter.complete()
123120

@@ -257,9 +254,6 @@ class CompletionAwaiterTests : FunSpec({
257254
}
258255
blockingThreads.forEach { it.start() }
259256

260-
// Wait a bit to ensure all are waiting
261-
Thread.sleep(100)
262-
263257
// Complete the awaiter
264258
awaiter.complete()
265259

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/core/internal/application/ApplicationServiceTests.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@ import com.onesignal.common.threading.suspendifyOnIO
99
import com.onesignal.core.internal.application.impl.ApplicationService
1010
import com.onesignal.debug.LogLevel
1111
import com.onesignal.debug.internal.logging.Logging
12+
import com.onesignal.mocks.IOMockHelper
13+
import com.onesignal.mocks.IOMockHelper.awaitIO
1214
import io.kotest.core.spec.style.FunSpec
1315
import io.kotest.matchers.shouldBe
1416
import io.mockk.spyk
1517
import io.mockk.verify
16-
import kotlinx.coroutines.delay
1718
import org.robolectric.Robolectric
1819

1920
@RobolectricTest
2021
class ApplicationServiceTests : FunSpec({
2122

23+
listener(IOMockHelper)
24+
2225
beforeAny {
2326
Logging.logLevel = LogLevel.NONE
2427
}
@@ -199,7 +202,7 @@ class ApplicationServiceTests : FunSpec({
199202
waiter.wake(response)
200203
}
201204

202-
delay(7000)
205+
awaitIO(7_000)
203206

204207
applicationService.onActivityStarted(activity)
205208
val response = waiter.waitForWake()
@@ -224,8 +227,6 @@ class ApplicationServiceTests : FunSpec({
224227
waiter.wake(response)
225228
}
226229

227-
delay(3000)
228-
229230
applicationService.onActivityStarted(activity)
230231
val response = waiter.waitForWake()
231232

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/core/internal/operations/OperationRepoTests.kt

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ private class Mocks {
7777
}
7878

7979
class OperationRepoTests : FunSpec({
80-
8180
beforeAny {
8281
Logging.logLevel = LogLevel.NONE
8382
}
@@ -133,13 +132,6 @@ class OperationRepoTests : FunSpec({
133132
// insertion from the main thread is done without blocking
134133
mainThread.join(500)
135134
mainThread.state shouldBe Thread.State.TERMINATED
136-
137-
// Wait for the async enqueue to complete (give it more time)
138-
var attempts = 0
139-
while (operationRepo.queue.size == 0 && attempts < 50) {
140-
Thread.sleep(10)
141-
attempts++
142-
}
143135
operationRepo.queue.size shouldBe 1
144136

145137
// after loading is completed, the cached operation should be at the beginning of the queue
@@ -166,13 +158,6 @@ class OperationRepoTests : FunSpec({
166158
operationRepo.start()
167159
operationRepo.enqueue(MyOperation())
168160

169-
// Wait for the async enqueue to complete
170-
var attempts = 0
171-
while (!operationRepo.containsInstanceOf<MyOperation>() && attempts < 50) {
172-
Thread.sleep(10)
173-
attempts++
174-
}
175-
176161
// Then
177162
operationRepo.containsInstanceOf<MyOperation>() shouldBe true
178163
operationRepo.containsInstanceOf<MyOperation2>() shouldBe false
@@ -276,7 +261,7 @@ class OperationRepoTests : FunSpec({
276261
// When
277262
opRepo.start()
278263
opRepo.enqueue(mockOperation())
279-
Thread.sleep(200) // Give time for the operation to be processed and retry delay to be set
264+
280265
val response1 =
281266
withTimeoutOrNull(500) {
282267
opRepo.enqueueAndWait(mockOperation())
@@ -751,13 +736,6 @@ class OperationRepoTests : FunSpec({
751736
val op = mockOperation()
752737
mocks.operationRepo.enqueue(op)
753738

754-
// Wait for the async enqueue to complete
755-
var attempts = 0
756-
while (mocks.operationRepo.queue.size == 0 && attempts < 50) {
757-
Thread.sleep(10)
758-
attempts++
759-
}
760-
761739
// When
762740
mocks.operationRepo.loadSavedOperations()
763741

@@ -797,7 +775,6 @@ class OperationRepoTests : FunSpec({
797775
// When
798776
opRepo.start()
799777
opRepo.enqueue(mockOperation())
800-
Thread.sleep(100) // Give time for the operation to be processed and retry delay to be set
801778
val response1 =
802779
withTimeoutOrNull(999) {
803780
opRepo.enqueueAndWait(mockOperation())
@@ -876,23 +853,6 @@ class OperationRepoTests : FunSpec({
876853
mocks.operationRepo.enqueue(groupableOp1) // This needs translation
877854
mocks.operationRepo.enqueueAndWait(groupableOp2) // This doesn't need translation but should be grouped
878855

879-
// Wait for all critical async operations to complete
880-
// We need: execute-translation-source, translate-groupable-1, execute-grouped-operations
881-
var attempts = 0
882-
val maxAttempts = 200 // Increased timeout for CI/CD environments (200 * 20ms = 4 seconds)
883-
while (attempts < maxAttempts) {
884-
val hasTranslationSource = executionOrder.contains("execute-translation-source")
885-
val hasTranslation = executionOrder.contains("translate-groupable-1")
886-
val hasGroupedExecution = executionOrder.contains("execute-grouped-operations")
887-
888-
if (hasTranslationSource && hasTranslation && hasGroupedExecution) {
889-
break // All critical events have occurred
890-
}
891-
892-
Thread.sleep(20)
893-
attempts++
894-
}
895-
896856
// Then verify the critical execution order
897857
executionOrder.size shouldBeGreaterThan 2 // At minimum: Translation source + translation + grouped execution (>= 3)
898858

@@ -964,7 +924,6 @@ class OperationRepoTests : FunSpec({
964924
val executeWaiter = WaiterWithValue<Boolean>()
965925
coEvery { opRepo.executeOperations(any()) } coAnswers {
966926
executeWaiter.wake(true)
967-
delay(10)
968927
firstArg<List<OperationRepo.OperationQueueItem>>().forEach { it.waiter?.wake(true) }
969928
}
970929
return executeWaiter

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/core/internal/preferences/PreferencesServiceTests.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import com.onesignal.core.internal.preferences.impl.PreferencesService
77
import com.onesignal.debug.LogLevel
88
import com.onesignal.debug.internal.logging.Logging
99
import com.onesignal.mocks.AndroidMockHelper
10+
import com.onesignal.mocks.IOMockHelper
11+
import com.onesignal.mocks.IOMockHelper.awaitIO
1012
import com.onesignal.mocks.MockHelper
1113
import io.kotest.assertions.throwables.shouldThrowUnit
1214
import io.kotest.core.spec.style.FunSpec
1315
import io.kotest.matchers.shouldBe
14-
import kotlinx.coroutines.delay
1516

1617
@RobolectricTest
1718
class PreferencesServiceTests : FunSpec({
@@ -22,6 +23,8 @@ class PreferencesServiceTests : FunSpec({
2223
val mockStringPrefStoreKey = "mock-string"
2324
val mockStringSetPrefStoreKey = "mock-string-set"
2425

26+
listener(IOMockHelper)
27+
2528
beforeAny {
2629
Logging.logLevel = LogLevel.NONE
2730
}
@@ -182,7 +185,7 @@ class PreferencesServiceTests : FunSpec({
182185
preferencesService.saveStringSet(mockPrefStoreName, mockStringSetPrefStoreKey, setOf("default1", "default2"))
183186
preferencesService.start()
184187

185-
delay(1000)
188+
awaitIO()
186189

187190
val store =
188191
ApplicationProvider.getApplicationContext<Context>().getSharedPreferences(
@@ -219,7 +222,7 @@ class PreferencesServiceTests : FunSpec({
219222
preferencesService.saveStringSet(mockPrefStoreName, mockStringSetPrefStoreKey, null)
220223
preferencesService.start()
221224

222-
delay(1000)
225+
awaitIO()
223226

224227
// Then
225228
store.contains(mockBoolPrefStoreKey) shouldBe false

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/core/internal/startup/StartupServiceTests.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import com.onesignal.common.services.ServiceBuilder
44
import com.onesignal.common.services.ServiceProvider
55
import com.onesignal.debug.LogLevel
66
import com.onesignal.debug.internal.logging.Logging
7+
import com.onesignal.mocks.IOMockHelper
8+
import com.onesignal.mocks.IOMockHelper.awaitIO
79
import io.kotest.assertions.throwables.shouldThrowUnit
810
import io.kotest.core.spec.style.FunSpec
911
import io.kotest.matchers.shouldBe
@@ -26,6 +28,8 @@ class StartupServiceTests : FunSpec({
2628
return serviceBuilder.build()
2729
}
2830

31+
listener(IOMockHelper)
32+
2933
beforeAny {
3034
Logging.logLevel = LogLevel.NONE
3135
}
@@ -88,7 +92,7 @@ class StartupServiceTests : FunSpec({
8892
startupService.scheduleStart()
8993

9094
// Then
91-
Thread.sleep(10)
95+
awaitIO()
9296
verify(exactly = 1) { mockStartupService1.start() }
9397
verify(exactly = 1) { mockStartupService2.start() }
9498
}
@@ -106,7 +110,7 @@ class StartupServiceTests : FunSpec({
106110
mockStartableService3.start()
107111

108112
// Then
109-
Thread.sleep(10)
113+
awaitIO()
110114
coVerifyOrder {
111115
// service3 will call start() first even though service1 and service2 are scheduled first
112116
mockStartableService3.start()

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/session/internal/outcomes/OutcomeEventsControllerTests.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import com.onesignal.common.exceptions.BackendException
44
import com.onesignal.common.threading.Waiter
55
import com.onesignal.debug.LogLevel
66
import com.onesignal.debug.internal.logging.Logging
7+
import com.onesignal.mocks.IOMockHelper
8+
import com.onesignal.mocks.IOMockHelper.awaitIO
79
import com.onesignal.mocks.MockHelper
810
import com.onesignal.session.internal.influence.IInfluenceManager
911
import com.onesignal.session.internal.influence.Influence
@@ -32,10 +34,12 @@ import io.mockk.just
3234
import io.mockk.mockk
3335
import io.mockk.runs
3436
import io.mockk.spyk
35-
import kotlinx.coroutines.delay
3637
import org.json.JSONArray
3738

3839
class OutcomeEventsControllerTests : FunSpec({
40+
41+
listener(IOMockHelper)
42+
3943
beforeAny {
4044
Logging.logLevel = LogLevel.NONE
4145
}
@@ -595,7 +599,7 @@ class OutcomeEventsControllerTests : FunSpec({
595599
// When
596600
outcomeEventsController.start()
597601

598-
delay(1000)
602+
awaitIO()
599603

600604
// Then
601605
coVerify(exactly = 1) {
@@ -687,7 +691,7 @@ class OutcomeEventsControllerTests : FunSpec({
687691
// When
688692
outcomeEventsController.start()
689693

690-
delay(1000)
694+
awaitIO()
691695

692696
// Then
693697
coVerify(exactly = 1) {
@@ -763,7 +767,7 @@ class OutcomeEventsControllerTests : FunSpec({
763767

764768
// When
765769
outcomeEventsController.start()
766-
delay(1000)
770+
awaitIO()
767771

768772
// Then
769773
coVerify(exactly = 1) {

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/migrations/RecoverFromDroppedLoginBugTests.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import com.onesignal.core.internal.operations.impl.OperationRepo
55
import com.onesignal.core.internal.time.impl.Time
66
import com.onesignal.debug.LogLevel
77
import com.onesignal.debug.internal.logging.Logging
8+
import com.onesignal.mocks.IOMockHelper
9+
import com.onesignal.mocks.IOMockHelper.awaitIO
810
import com.onesignal.mocks.MockHelper
911
import com.onesignal.user.internal.operations.ExecutorMocks
1012
import com.onesignal.user.internal.operations.LoginUserOperation
@@ -16,7 +18,6 @@ import io.mockk.mockk
1618
import io.mockk.runs
1719
import io.mockk.spyk
1820
import io.mockk.verify
19-
import kotlinx.coroutines.delay
2021
import kotlinx.coroutines.withTimeout
2122

2223
private class Mocks {
@@ -76,6 +77,9 @@ private class Mocks {
7677
}
7778

7879
class RecoverFromDroppedLoginBugTests : FunSpec({
80+
81+
listener(IOMockHelper)
82+
7983
beforeAny {
8084
Logging.logLevel = LogLevel.NONE
8185
}
@@ -100,7 +104,7 @@ class RecoverFromDroppedLoginBugTests : FunSpec({
100104
// When
101105
mocks.operationRepo.start()
102106
// give operation repo some time to fully initialize
103-
delay(200)
107+
awaitIO()
104108

105109
mocks.recovery.start()
106110
withTimeout(1_000) { mocks.operationRepo.awaitInitialized() }

0 commit comments

Comments
 (0)