Skip to content

Commit 993fffa

Browse files
authored
Fix UnifiedPushService tests (#1420)
1 parent 4b7f7ed commit 993fffa

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

app/src/androidTest/kotlin/at/bitfire/davdroid/push/UnifiedPushServiceTest.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.content.Context
88
import android.content.Intent
99
import android.os.IBinder
1010
import androidx.test.rule.ServiceTestRule
11+
import at.bitfire.davdroid.di.MainDispatcher
1112
import dagger.hilt.android.qualifiers.ApplicationContext
1213
import dagger.hilt.android.testing.BindValue
1314
import dagger.hilt.android.testing.HiltAndroidRule
@@ -18,6 +19,10 @@ import io.mockk.every
1819
import io.mockk.impl.annotations.RelaxedMockK
1920
import io.mockk.junit4.MockKRule
2021
import io.mockk.mockk
22+
import kotlinx.coroutines.CoroutineDispatcher
23+
import kotlinx.coroutines.ExperimentalCoroutinesApi
24+
import kotlinx.coroutines.test.advanceUntilIdle
25+
import kotlinx.coroutines.test.runTest
2126
import org.junit.Before
2227
import org.junit.Rule
2328
import org.junit.Test
@@ -26,6 +31,7 @@ import org.unifiedpush.android.connector.PushService
2631
import org.unifiedpush.android.connector.data.PushEndpoint
2732
import javax.inject.Inject
2833

34+
@OptIn(ExperimentalCoroutinesApi::class)
2935
@HiltAndroidTest
3036
class UnifiedPushServiceTest {
3137

@@ -42,6 +48,10 @@ class UnifiedPushServiceTest {
4248
@ApplicationContext
4349
lateinit var context: Context
4450

51+
@Inject
52+
@MainDispatcher
53+
lateinit var mainDispatcher: CoroutineDispatcher
54+
4555
@RelaxedMockK
4656
@BindValue
4757
lateinit var pushRegistrationManager: PushRegistrationManager
@@ -60,32 +70,35 @@ class UnifiedPushServiceTest {
6070

6171

6272
@Test
63-
fun testOnNewEndpoint() {
73+
fun testOnNewEndpoint() = runTest(mainDispatcher) {
6474
val endpoint = mockk<PushEndpoint> {
6575
every { url } returns "https://example.com/12"
6676
}
6777
unifiedPushService.onNewEndpoint(endpoint, "12")
6878

79+
advanceUntilIdle()
6980
coVerify {
7081
pushRegistrationManager.processSubscription(12, endpoint)
7182
}
7283
confirmVerified(pushRegistrationManager)
7384
}
7485

7586
@Test
76-
fun testOnRegistrationFailed() {
87+
fun testOnRegistrationFailed() = runTest(mainDispatcher) {
7788
unifiedPushService.onRegistrationFailed(FailedReason.INTERNAL_ERROR, "34")
7889

90+
advanceUntilIdle()
7991
coVerify {
8092
pushRegistrationManager.removeSubscription(34)
8193
}
8294
confirmVerified(pushRegistrationManager)
8395
}
8496

8597
@Test
86-
fun testOnUnregistered() {
98+
fun testOnUnregistered() = runTest(mainDispatcher) {
8799
unifiedPushService.onRegistrationFailed(FailedReason.INTERNAL_ERROR, "45")
88100

101+
advanceUntilIdle()
89102
coVerify {
90103
pushRegistrationManager.removeSubscription(45)
91104
}

app/src/main/kotlin/at/bitfire/davdroid/push/UnifiedPushService.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
package at.bitfire.davdroid.push
66

7+
import at.bitfire.davdroid.di.ApplicationScope
78
import dagger.Lazy
89
import dagger.hilt.android.AndroidEntryPoint
910
import kotlinx.coroutines.CoroutineScope
10-
import kotlinx.coroutines.SupervisorJob
1111
import kotlinx.coroutines.launch
1212
import org.unifiedpush.android.connector.FailedReason
1313
import org.unifiedpush.android.connector.PushService
@@ -25,6 +25,13 @@ import javax.inject.Inject
2525
@AndroidEntryPoint
2626
class UnifiedPushService : PushService() {
2727

28+
/* Scope to run the requests asynchronously. UnifiedPush binds the service,
29+
* sends the message and unbinds one second later. Our operations may take longer,
30+
* so the scope should not be bound to the service lifecycle. */
31+
@Inject
32+
@ApplicationScope
33+
lateinit var applicationScope: CoroutineScope
34+
2835
@Inject
2936
lateinit var logger: Logger
3037

@@ -34,18 +41,13 @@ class UnifiedPushService : PushService() {
3441
@Inject
3542
lateinit var pushRegistrationManager: Lazy<PushRegistrationManager>
3643

37-
/* Scope to run the requests asynchronously. UnifiedPush binds the service,
38-
* sends the message and unbinds one second later. Our operations may take longer,
39-
* so the scope should not be bound to the service lifecycle. */
40-
val serviceScope = CoroutineScope(SupervisorJob())
41-
4244

4345
override fun onNewEndpoint(endpoint: PushEndpoint, instance: String) {
4446
val serviceId = instance.toLongOrNull() ?: return
4547
logger.warning("Got UnifiedPush endpoint for service $serviceId: ${endpoint.url}")
4648

4749
// register new endpoint at CalDAV/CardDAV servers
48-
serviceScope.launch {
50+
applicationScope.launch {
4951
pushRegistrationManager.get().processSubscription(serviceId, endpoint)
5052
}
5153
}
@@ -55,7 +57,7 @@ class UnifiedPushService : PushService() {
5557
logger.warning("UnifiedPush registration failed for service $serviceId: $reason")
5658

5759
// unregister subscriptions
58-
serviceScope.launch {
60+
applicationScope.launch {
5961
pushRegistrationManager.get().removeSubscription(serviceId)
6062
}
6163
}
@@ -64,13 +66,13 @@ class UnifiedPushService : PushService() {
6466
val serviceId = instance.toLongOrNull() ?: return
6567
logger.warning("UnifiedPush unregistered for service $serviceId")
6668

67-
serviceScope.launch {
69+
applicationScope.launch {
6870
pushRegistrationManager.get().removeSubscription(serviceId)
6971
}
7072
}
7173

7274
override fun onMessage(message: PushMessage, instance: String) {
73-
serviceScope.launch {
75+
applicationScope.launch {
7476
pushMessageHandler.get().processMessage(message, instance)
7577
}
7678
}

0 commit comments

Comments
 (0)