From bef657ee842fa2afaf045cb68b1381e655f7ede4 Mon Sep 17 00:00:00 2001 From: Sultan Seidalin Date: Tue, 25 Jun 2024 11:33:05 +0200 Subject: [PATCH] TAP-603: Added a test to verify correct refresh behavior --- .../com/tidal/sdk/auth/TokenRepositoryTest.kt | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/auth/src/test/kotlin/com/tidal/sdk/auth/TokenRepositoryTest.kt b/auth/src/test/kotlin/com/tidal/sdk/auth/TokenRepositoryTest.kt index e36c2aba..1c9b47b8 100644 --- a/auth/src/test/kotlin/com/tidal/sdk/auth/TokenRepositoryTest.kt +++ b/auth/src/test/kotlin/com/tidal/sdk/auth/TokenRepositoryTest.kt @@ -18,12 +18,13 @@ import com.tidal.sdk.util.TEST_CLIENT_ID import com.tidal.sdk.util.TEST_CLIENT_UNIQUE_KEY import com.tidal.sdk.util.TEST_TIME_PROVIDER import com.tidal.sdk.util.makeCredentials +import java.io.IOException import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.launch import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.advanceUntilIdle import kotlinx.coroutines.test.runTest -import okio.IOException +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test @@ -755,4 +756,31 @@ class TokenRepositoryTest { "No calls to the backend should have been made" } } + + @Test + fun `there can only be one refresh call even when requested from multiple threads`() = runTest { + val credentials = makeCredentials( + userId = "valid", + isExpired = true, + ) + val tokens = Tokens( + credentials, + "refreshToken", + ) + createTokenRepository( + FakeTokenService(), + FakeTokensStore(authConfig.credentialsKey, tokens), + ) + + val numberOfThreads = 100 + val jobs = List(numberOfThreads) { + launch { + tokenRepository.getCredentials(null) + } + } + jobs.forEach { it.join() } + + val numberOfRefreshCalls = fakeTokenService.calls.filter { it == CallType.Refresh }.size + assertEquals(1, numberOfRefreshCalls) + } }