Skip to content

Commit 87447f3

Browse files
committed
tests: fix a flaky test in NotificationGeenerationProcessorTests
1 parent 53e32b7 commit 87447f3

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

OneSignalSDK/onesignal/notifications/src/test/java/com/onesignal/notifications/internal/generation/NotificationGenerationProcessorTests.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import com.onesignal.common.threading.suspendifyOnIO
55
import com.onesignal.debug.LogLevel
66
import com.onesignal.debug.internal.logging.Logging
77
import com.onesignal.mocks.AndroidMockHelper
8+
import com.onesignal.mocks.IOMockHelper
9+
import com.onesignal.mocks.IOMockHelper.awaitIO
810
import com.onesignal.mocks.MockHelper
911
import com.onesignal.notifications.INotificationReceivedEvent
1012
import com.onesignal.notifications.INotificationWillDisplayEvent
@@ -96,6 +98,9 @@ private class Mocks {
9698
}
9799

98100
class NotificationGenerationProcessorTests : FunSpec({
101+
102+
listener(IOMockHelper)
103+
99104
beforeAny {
100105
Logging.logLevel = LogLevel.NONE
101106

@@ -276,16 +281,17 @@ class NotificationGenerationProcessorTests : FunSpec({
276281
coEvery { mocks.notificationLifecycleService.externalNotificationWillShowInForeground(any()) } coAnswers {
277282
val willDisplayEvent = firstArg<INotificationWillDisplayEvent>()
278283
willDisplayEvent.preventDefault(false)
284+
// Call preventDefault(true) synchronously to wake the waiter before it times out
285+
willDisplayEvent.preventDefault(true)
286+
// Then call display() asynchronously to ensure it happens after waitForWake completes
279287
suspendifyOnIO {
280-
delay(100)
281-
willDisplayEvent.preventDefault(true)
282-
delay(100)
283288
willDisplayEvent.notification.display()
284289
}
285290
}
286291

287292
// When
288293
mocks.notificationGenerationProcessor.processNotificationData(mocks.context, 1, mocks.notificationPayload, false, 1111)
294+
awaitIO()
289295

290296
// Then
291297
coVerify(exactly = 0) {
@@ -301,15 +307,14 @@ class NotificationGenerationProcessorTests : FunSpec({
301307
val receivedEvent = firstArg<INotificationReceivedEvent>()
302308
receivedEvent.preventDefault(false)
303309
suspendifyOnIO {
304-
delay(100)
305310
receivedEvent.preventDefault(true)
306-
delay(100)
307311
receivedEvent.notification.display()
308312
}
309313
}
310314

311315
// When
312316
mocks.notificationGenerationProcessor.processNotificationData(mocks.context, 1, mocks.notificationPayload, true, 1111)
317+
awaitIO()
313318

314319
// Then
315320
coVerify(exactly = 0) {

0 commit comments

Comments
 (0)