@@ -21,10 +21,16 @@ import io.getstream.video.android.core.call.CallType
2121import io.getstream.video.android.core.notifications.internal.service.CallServiceConfigRegistry
2222import io.getstream.video.android.core.notifications.internal.service.callServiceConfig
2323import io.getstream.video.android.model.User
24+ import io.mockk.every
25+ import io.mockk.mockk
2426import junit.framework.TestCase.assertEquals
27+ import junit.framework.TestCase.assertTrue
2528import org.junit.Test
2629import org.junit.runner.RunWith
2730import org.robolectric.RobolectricTestRunner
31+ import java.util.concurrent.CountDownLatch
32+ import java.util.concurrent.TimeUnit
33+ import kotlin.concurrent.thread
2834
2935@RunWith(RobolectricTestRunner ::class )
3036class StreamVideoBuilderTest : TestBase () {
@@ -104,4 +110,51 @@ class StreamVideoBuilderTest : TestBase() {
104110 assertEquals(client.coordinatorConnectionModule.apiUrl, customApiUrl)
105111 assertEquals(client.coordinatorConnectionModule.wssUrl, customWssUrl)
106112 }
113+
114+ @Test
115+ fun build_afterRemovingClientDuringCleanup_doesNotThrow () {
116+ StreamVideo .removeClient()
117+
118+ val cleanupStarted = CountDownLatch (1 )
119+ val cleanupRelease = CountDownLatch (1 )
120+ val blockingClient = mockk<StreamVideo >(relaxed = true )
121+
122+ every { blockingClient.cleanup() } answers {
123+ cleanupStarted.countDown()
124+ cleanupRelease.await(5 , TimeUnit .SECONDS )
125+ }
126+
127+ StreamVideo .install(blockingClient)
128+
129+ val removeThread = thread(start = true ) {
130+ StreamVideo .removeClient()
131+ }
132+
133+ val buildResult = try {
134+ assertTrue(
135+ " Timeout waiting for cleanup to start" ,
136+ cleanupStarted.await(5 , TimeUnit .SECONDS ),
137+ )
138+
139+ runCatching {
140+ StreamVideoBuilder (
141+ ensureSingleInstance = true ,
142+ context = context,
143+ apiKey = authData!! .apiKey,
144+ user = User .anonymous(),
145+ token = " anonymous-token" ,
146+ ).build()
147+ }
148+ } catch (it: Exception ) {
149+ it.printStackTrace()
150+ throw it
151+ } finally {
152+ cleanupRelease.countDown()
153+ removeThread.join(TimeUnit .SECONDS .toMillis(5 ))
154+ StreamVideo .removeClient()
155+ }
156+
157+ println (" buildResult: $buildResult " )
158+ assertTrue(" Expected builder to succeed after removeClient" , buildResult.isSuccess)
159+ }
107160}
0 commit comments