@@ -5,7 +5,10 @@ import androidx.lifecycle.LifecycleRegistry
55import androidx.lifecycle.LiveData
66import kotlinx.coroutines.CancellationException
77import kotlinx.coroutines.ExperimentalCoroutinesApi
8+ import kotlinx.coroutines.launch
9+ import kotlinx.coroutines.sync.Mutex
810import org.assertj.core.api.Assertions
11+ import org.junit.Before
912import org.junit.Test
1013import org.junit.runner.RunWith
1114import 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