Skip to content

Commit

Permalink
TAP-603: Added a test to verify correct refresh behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
sula-sa committed Jun 25, 2024
1 parent 3e3ce20 commit bef657e
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion auth/src/test/kotlin/com/tidal/sdk/auth/TokenRepositoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
}

0 comments on commit bef657e

Please sign in to comment.