-
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: multi initialize/initializeasync (#247)
* test: multi initialize/initializeasync * fix: cleanup test, add long timeout case * fix: test * fix: lints * feat: make initialize and initializeAsync threadsafe * fix: unset in shutdown * fix: eb test * fix: lint * fix: async test code
- Loading branch information
1 parent
f102299
commit 7e9e0c5
Showing
5 changed files
with
272 additions
and
15 deletions.
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
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
91 changes: 91 additions & 0 deletions
91
src/test/java/com/statsig/androidsdk/StatsigLongInitializationTimeoutTest.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,91 @@ | ||
package com.statsig.androidsdk | ||
|
||
import android.app.Application | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.spyk | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.test.runBlockingTest | ||
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 StatsigLongInitializationTimeoutTest { | ||
|
||
private var app: Application = mockk() | ||
private lateinit var client: StatsigClient | ||
private lateinit var errorBoundary: ErrorBoundary | ||
private lateinit var mockWebServer: MockWebServer | ||
private var initializeHits = 0 | ||
|
||
@Before | ||
fun setup() { | ||
mockWebServer = MockWebServer() | ||
val dispatcher = object : Dispatcher() { | ||
override fun dispatch(request: RecordedRequest): MockResponse { | ||
return if (request.path!!.contains("initialize")) { | ||
initializeHits++ | ||
runBlocking { | ||
delay(500) | ||
} | ||
MockResponse() | ||
.setBody("{\"result\":\"error logged\"}") | ||
.setResponseCode(503) | ||
} else { | ||
MockResponse().setResponseCode(404) | ||
} | ||
} | ||
} | ||
mockWebServer.dispatcher = dispatcher | ||
mockWebServer.start() | ||
client = spyk(StatsigClient(), recordPrivateCalls = true) | ||
client.errorBoundary = spyk(client.errorBoundary) | ||
errorBoundary = client.errorBoundary | ||
|
||
TestUtil.mockDispatchers() | ||
TestUtil.stubAppFunctions(app) | ||
|
||
every { | ||
errorBoundary.getUrl() | ||
} returns mockWebServer.url("/v1/sdk_exception").toString() | ||
|
||
client.errorBoundary = errorBoundary | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
mockWebServer.shutdown() | ||
} | ||
|
||
@Test | ||
fun testInitializeAsyncWithSlowErrorBoundary() = runBlockingTest { | ||
var initTimeout = 10000L | ||
val latch = CountDownLatch(1) | ||
|
||
client.initializeAsync( | ||
app, | ||
"client-key", | ||
StatsigUser("test_user"), | ||
object : IStatsigCallback { | ||
override fun onStatsigInitialize(details: InitializationDetails) { | ||
latch.countDown() | ||
} | ||
|
||
override fun onStatsigUpdateUser() { | ||
// no op | ||
} | ||
}, | ||
StatsigOptions(initTimeoutMs = initTimeout, api = mockWebServer.url("/").toString()), | ||
) | ||
latch.await(initTimeout, TimeUnit.SECONDS) | ||
assert(client.isInitialized()) | ||
assertTrue(initializeHits === 1) | ||
} | ||
} |
170 changes: 170 additions & 0 deletions
170
src/test/java/com/statsig/androidsdk/StatsigMultipleInitializeTest.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,170 @@ | ||
package com.statsig.androidsdk | ||
|
||
import android.app.Application | ||
import io.mockk.* | ||
import kotlinx.coroutines.* | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class StatsigMultipleInitializeTest { | ||
|
||
private lateinit var client: StatsigClient | ||
private lateinit var app: Application | ||
private lateinit var network: StatsigNetwork | ||
|
||
@Before | ||
fun setup() { | ||
TestUtil.mockDispatchers() | ||
app = mockk(relaxed = true) | ||
client = spyk(StatsigClient(), recordPrivateCalls = true) | ||
network = TestUtil.mockNetwork() | ||
client.statsigNetwork = network | ||
|
||
TestUtil.stubAppFunctions(app) | ||
|
||
coEvery { | ||
network.initialize( | ||
api = any(), | ||
user = any(), | ||
sinceTime = any(), | ||
metadata = any(), | ||
coroutineScope = any(), | ||
context = any(), | ||
diagnostics = any(), | ||
hashUsed = any(), | ||
previousDerivedFields = any(), | ||
) | ||
} coAnswers { | ||
TestUtil.makeInitializeResponse() | ||
} | ||
} | ||
|
||
@Test | ||
fun testMultipleInitializeAsyncCalls() { | ||
val job1 = GlobalScope.launch(Dispatchers.IO) { | ||
client.initializeAsync(app, "client-key", StatsigUser("test_user")) | ||
} | ||
|
||
val job2 = GlobalScope.launch(Dispatchers.IO) { | ||
client.initializeAsync(app, "client-key", StatsigUser("test_user")) | ||
} | ||
|
||
val job3 = GlobalScope.launch(Dispatchers.IO) { | ||
client.initializeAsync(app, "client-key", StatsigUser("test_user")) | ||
} | ||
|
||
runBlocking { | ||
joinAll(job1, job2, job3) | ||
} | ||
coVerify(exactly = 1) { | ||
network.initialize( | ||
api = any(), | ||
user = any(), | ||
sinceTime = any(), | ||
metadata = any(), | ||
coroutineScope = any(), | ||
context = any(), | ||
diagnostics = any(), | ||
hashUsed = any(), | ||
previousDerivedFields = any(), | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun testMultipleInitializeCalls() { | ||
val job1 = GlobalScope.launch(Dispatchers.IO) { | ||
client.initialize(app, "client-key", StatsigUser("test_user")) | ||
} | ||
|
||
val job2 = GlobalScope.launch(Dispatchers.IO) { | ||
client.initialize(app, "client-key", StatsigUser("test_user")) | ||
} | ||
|
||
val job3 = GlobalScope.launch(Dispatchers.IO) { | ||
client.initialize(app, "client-key", StatsigUser("test_user")) | ||
} | ||
|
||
runBlocking { | ||
joinAll(job1, job2, job3) | ||
} | ||
coVerify(exactly = 1) { | ||
network.initialize( | ||
api = any(), | ||
user = any(), | ||
sinceTime = any(), | ||
metadata = any(), | ||
coroutineScope = any(), | ||
context = any(), | ||
diagnostics = any(), | ||
hashUsed = any(), | ||
previousDerivedFields = any(), | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun testMultipleInitializeCallsOnMain() { | ||
val job1 = GlobalScope.launch(Dispatchers.Default) { | ||
client.initialize(app, "client-key", StatsigUser("test_user")) | ||
} | ||
|
||
val job2 = GlobalScope.launch(Dispatchers.Default) { | ||
client.initialize(app, "client-key", StatsigUser("test_user")) | ||
} | ||
|
||
val job3 = GlobalScope.launch(Dispatchers.Default) { | ||
client.initialize(app, "client-key", StatsigUser("test_user")) | ||
} | ||
|
||
runBlocking { | ||
joinAll(job1, job2, job3) | ||
} | ||
|
||
coVerify(exactly = 1) { | ||
network.initialize( | ||
api = any(), | ||
user = any(), | ||
sinceTime = any(), | ||
metadata = any(), | ||
coroutineScope = any(), | ||
context = any(), | ||
diagnostics = any(), | ||
hashUsed = any(), | ||
previousDerivedFields = any(), | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun testMultipleInitializeAsyncCallsOnMain() { | ||
val job1 = GlobalScope.launch(Dispatchers.Default) { | ||
client.initializeAsync(app, "client-key", StatsigUser("test_user")) | ||
} | ||
|
||
val job2 = GlobalScope.launch(Dispatchers.Default) { | ||
client.initializeAsync(app, "client-key", StatsigUser("test_user")) | ||
} | ||
|
||
val job3 = GlobalScope.launch(Dispatchers.Default) { | ||
client.initializeAsync(app, "client-key", StatsigUser("test_user")) | ||
} | ||
|
||
runBlocking { | ||
joinAll(job1, job2, job3) | ||
} | ||
coVerify(exactly = 1) { | ||
network.initialize( | ||
api = any(), | ||
user = any(), | ||
sinceTime = any(), | ||
metadata = any(), | ||
coroutineScope = any(), | ||
context = any(), | ||
diagnostics = any(), | ||
hashUsed = any(), | ||
previousDerivedFields = any(), | ||
) | ||
} | ||
} | ||
} |