Skip to content

Commit f377b6e

Browse files
committed
Add: unit test simulating the crash in resume onCancellation handler
1 parent c882090 commit f377b6e

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

WordPress/src/test/java/org/wordpress/android/ui/uploads/UploadStarterTest.kt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import androidx.lifecycle.LifecycleRegistry
55
import androidx.lifecycle.LiveData
66
import kotlinx.coroutines.CancellationException
77
import kotlinx.coroutines.ExperimentalCoroutinesApi
8+
import kotlinx.coroutines.launch
9+
import kotlinx.coroutines.sync.Mutex
810
import org.assertj.core.api.Assertions
11+
import org.junit.Before
912
import org.junit.Test
1013
import org.junit.runner.RunWith
1114
import org.mockito.junit.MockitoJUnitRunner
@@ -123,6 +126,13 @@ class UploadStarterTest : BaseUnitTest() {
123126
}
124127
}
125128

129+
private lateinit var mutex: Mutex
130+
131+
@Before
132+
fun setUp() {
133+
mutex = Mutex()
134+
}
135+
126136
@Test
127137
fun `when the internet connection is restored and the app is in foreground, it uploads changed posts & pages`() {
128138
// Given
@@ -227,6 +237,23 @@ class UploadStarterTest : BaseUnitTest() {
227237
)
228238
}
229239

240+
@Test
241+
fun `given an unexpected mutex unlock, when uploading, then all other sites are uploaded`() = test {
242+
val starter = createUploadStarter()
243+
val unlockPoint = draftPosts.first()
244+
whenever(uploadServiceFacade.uploadPost(any(), eq(unlockPoint), any())).thenAnswer { mutex.unlock() }
245+
246+
launch {
247+
starter.queueUploadFromSite(sites[0])
248+
starter.queueUploadFromSite(sites[1])
249+
starter.queueUploadFromAllSites()
250+
}
251+
252+
val expectedInvocations = (draftPosts.size + draftPages.size) * 2
253+
verify(uploadServiceFacade, times(expectedInvocations)).uploadPost(any(), any<PostModel>(), any())
254+
}
255+
256+
230257
@Test
231258
fun `when uploading a single site, only posts & pages of that site are uploaded`() {
232259
// Given
@@ -547,7 +574,7 @@ class UploadStarterTest : BaseUnitTest() {
547574
uploadActionUseCase = UploadActionUseCase(uploadStore, postUtilsWrapper, uploadServiceFacade),
548575
tracker = mock(),
549576
dispatcher = dispatcher,
550-
mutex = mock(),
577+
mutex = mutex,
551578
)
552579
}
553580
}

0 commit comments

Comments
 (0)