-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: initialization timeout hit when eb stalls (#215)
* test: initialization timeout when eb stalls * fix lints * fix test * lints * fix imports * add mock webserver to test
- Loading branch information
1 parent
cd19142
commit 247c7cf
Showing
3 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/test/java/com/statsig/androidsdk/StatsigInitializationTimeoutTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.statsig.androidsdk | ||
|
||
import android.app.Application | ||
import io.mockk.coEvery | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.spyk | ||
import kotlinx.coroutines.* | ||
import okhttp3.mockwebserver.Dispatcher | ||
import okhttp3.mockwebserver.MockResponse | ||
import okhttp3.mockwebserver.MockWebServer | ||
import okhttp3.mockwebserver.RecordedRequest | ||
import org.junit.After | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Test | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.TimeUnit | ||
|
||
class StatsigInitializationTimeoutTest { | ||
|
||
private var app: Application = mockk() | ||
private lateinit var client: StatsigClient | ||
private lateinit var network: StatsigNetwork | ||
private lateinit var errorBoundary: ErrorBoundary | ||
private lateinit var mockWebServer: MockWebServer | ||
private val hitErrorBoundary = CountDownLatch(1) | ||
|
||
@Before | ||
fun setup() { | ||
mockWebServer = MockWebServer() | ||
val dispatcher = object : Dispatcher() { | ||
override fun dispatch(request: RecordedRequest): MockResponse { | ||
return if (request.path!!.contains("sdk_exception")) { | ||
hitErrorBoundary.countDown() | ||
runBlocking { | ||
delay(1000) | ||
} | ||
MockResponse() | ||
.setBody("{\"result\":\"error logged\"}") | ||
.setResponseCode(200) | ||
} else { | ||
MockResponse().setResponseCode(404) | ||
} | ||
} | ||
} | ||
mockWebServer.dispatcher = dispatcher | ||
mockWebServer.start() | ||
client = spyk(StatsigClient(), recordPrivateCalls = true) | ||
client.errorBoundary = spyk(client.errorBoundary) | ||
errorBoundary = client.errorBoundary | ||
network = TestUtil.mockNetwork() | ||
|
||
TestUtil.mockDispatchers() | ||
TestUtil.stubAppFunctions(app) | ||
|
||
coEvery { | ||
network.initialize(any(), any(), any(), any(), any(), any(), any(), any(), any()) | ||
} coAnswers { | ||
TestUtil.makeInitializeResponse() | ||
} | ||
|
||
// Lets get a successful network response, and then trigger error boundary | ||
// so we can test that eb does not block the initialization beyond the init timeout | ||
every { | ||
println("causing exception") | ||
client["pollForUpdates"]() | ||
} throws(Exception("trigger the error boundary")) | ||
|
||
every { | ||
errorBoundary.getUrl() | ||
} returns mockWebServer.url("/v1/sdk_exception").toString() | ||
|
||
client.statsigNetwork = network | ||
client.errorBoundary = errorBoundary | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
mockWebServer.shutdown() | ||
} | ||
|
||
@Test | ||
fun testInitializeAsyncWithSlowErrorBoundary() = runBlocking { | ||
var initializationDetails: InitializationDetails? = null | ||
var initTimeout = 500L | ||
runBlocking { | ||
initializationDetails = client.initialize(app, "client-key", StatsigUser("test_user"), StatsigOptions(initTimeoutMs = initTimeout)) | ||
} | ||
// initialize timeout was hit, we got a value back and we are considered initialized | ||
assert(initializationDetails != null) | ||
assert(client.isInitialized()) | ||
|
||
// error boundary was hit, but has not completed at this point, so the initialization timeout worked | ||
assertTrue(hitErrorBoundary.await(1, TimeUnit.SECONDS)) | ||
assertTrue( | ||
"initialization time ${initializationDetails!!.duration} not less than initTimeout $initTimeout", | ||
initializationDetails!!.duration < initTimeout + 100L, | ||
) | ||
|
||
// error boundary was hit, but has not completed at this point, so the initialization timeout worked | ||
assert(hitErrorBoundary.await(1, TimeUnit.SECONDS)) | ||
} | ||
} |