Skip to content

Commit 125f2cc

Browse files
Update StatsigErrorBoundaryUsage.kt (#90)
* Update StatsigErrorBoundaryUsage.kt * Update StatsigErrorBoundaryUsage.kt * Update StatsigErrorBoundaryUsage.kt
1 parent 6a494db commit 125f2cc

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

src/test/java/com/statsig/sdk/StatsigErrorBoundaryUsage.kt

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import okhttp3.mockwebserver.Dispatcher
99
import okhttp3.mockwebserver.MockResponse
1010
import okhttp3.mockwebserver.MockWebServer
1111
import okhttp3.mockwebserver.RecordedRequest
12-
import org.junit.After
1312
import org.junit.AfterClass
1413
import org.junit.Assert.assertEquals
1514
import org.junit.Before
@@ -23,146 +22,151 @@ class StatsigErrorBoundaryUsage {
2322

2423
companion object {
2524
private lateinit var server: MockWebServer
26-
private val exception = Exception("Test Exception")
2725
private lateinit var onRequestWaiter: CountDownLatch
28-
private lateinit var statsig: StatsigServer
2926
private val requests = arrayListOf<RecordedRequest>()
3027
private var throwOnDownloadConfigSpecs = false
3128

3229
@BeforeClass
3330
@JvmStatic
3431
internal fun beforeAll() {
3532
mockkConstructor(Evaluator::class)
36-
every { anyConstructed<Evaluator>().layers } throws exception
33+
every { anyConstructed<Evaluator>().layers } throws Exception("Test Evaluator Layers")
3734

3835
mockkConstructor(StatsigNetwork::class)
39-
every { anyConstructed<StatsigNetwork>().shutdown() } throws exception
36+
every { anyConstructed<StatsigNetwork>().shutdown() } throws Exception("Test Network Shutdown")
4037

4138
mockkConstructor(ConfigEvaluation::class)
42-
every { anyConstructed<ConfigEvaluation>().fetchFromServer } throws exception
39+
every { anyConstructed<ConfigEvaluation>().fetchFromServer } throws Exception("Test Config Eval")
4340

4441
mockkConstructor(StatsigLogger::class)
45-
coEvery { anyConstructed<StatsigLogger>().log(any()) } throws exception
42+
coEvery { anyConstructed<StatsigLogger>().log(any()) } throws Exception("Test Logger Log")
4643

4744
coEvery { anyConstructed<StatsigNetwork>().downloadConfigSpecs() } coAnswers {
4845
if (throwOnDownloadConfigSpecs) {
49-
throw exception
46+
throw Exception("Bad Config Specs")
5047
}
5148
callOriginal()
5249
}
50+
51+
server = MockWebServer()
52+
server.dispatcher =
53+
object : Dispatcher() {
54+
override fun dispatch(request: RecordedRequest): MockResponse {
55+
requests.add(request)
56+
onRequestWaiter.countDown()
57+
return MockResponse().setResponseCode(202)
58+
}
59+
}
5360
}
5461

5562
@AfterClass
5663
@JvmStatic
5764
fun afterAll() {
5865
unmockkAll()
66+
server.shutdown()
5967
}
6068
}
6169

62-
@After
63-
internal fun after() {
64-
server.shutdown()
70+
private fun getStatsigInstance(shouldInitialize: Boolean = true) = runBlocking {
71+
val statsig = StatsigServer.create("secret-key", StatsigOptions(api = "http://localhost"))
72+
if (shouldInitialize) {
73+
runBlocking {
74+
statsig.initialize()
75+
}
76+
}
77+
78+
statsig.errorBoundary.uri = server.url("/v1/sdk_exception").toUri()
79+
return@runBlocking statsig
6580
}
6681

6782
@Before
6883
internal fun beforeEach() {
6984
throwOnDownloadConfigSpecs = false
70-
71-
statsig = StatsigServer.create("secret-key", StatsigOptions(api = "http://localhost"))
72-
runBlocking {
73-
statsig.initialize()
74-
}
75-
76-
server = MockWebServer()
77-
statsig.errorBoundary.uri = server.url("/v1/sdk_exception").toUri()
78-
79-
server.dispatcher =
80-
object : Dispatcher() {
81-
override fun dispatch(request: RecordedRequest): MockResponse {
82-
requests.add(request)
83-
onRequestWaiter.countDown()
84-
return MockResponse().setResponseCode(202)
85-
}
86-
}
87-
88-
8985
onRequestWaiter = CountDownLatch(1)
9086
requests.clear()
91-
statsig.errorBoundary.seen.clear()
9287
}
9388

9489
@Test
9590
fun testErrorsWithInitialize() = runBlocking {
96-
val localStatsig = StatsigServer.create("secret-key", StatsigOptions(api = "http://localhost"))
97-
localStatsig.errorBoundary.uri = server.url("/v1/sdk_exception").toUri()
91+
val statsig = getStatsigInstance(shouldInitialize = false)
9892
throwOnDownloadConfigSpecs = true
9993

100-
localStatsig.initialize()
94+
runBlocking {
95+
statsig.initialize()
96+
}
97+
10198
assertEquals(1, requests.size)
10299
}
103100

104101
@Test
105102
fun testErrorsWithShutdown() = runBlocking {
106-
val localStatsig = StatsigServer.create("secret-key", StatsigOptions(api = "http://localhost"))
107-
localStatsig.errorBoundary.uri = server.url("/v1/sdk_exception").toUri()
108-
localStatsig.initialize()
103+
val statsig = getStatsigInstance()
109104
assertEquals(0, requests.size)
110105

111-
localStatsig.shutdown()
106+
statsig.shutdown()
112107
assertEquals(1, requests.size)
113108
}
114109

115110
@Test
116111
fun testErrorsWithCheckGate() = runBlocking {
112+
val statsig = getStatsigInstance()
117113
statsig.checkGate(user, "a_gate")
118114
assertEquals(1, requests.size)
119115
}
120116

121117
@Test
122118
fun testErrorsWithGetConfig() = runBlocking {
119+
val statsig = getStatsigInstance()
123120
statsig.getConfig(user, "a_config")
124121
assertEquals(1, requests.size)
125122
}
126123

127124
@Test
128125
fun testErrorsWithGetExperiment() = runBlocking {
126+
val statsig = getStatsigInstance()
129127
statsig.getExperiment(user, "an_experiment")
130128
assertEquals(1, requests.size)
131129
}
132130

133131
@Test
134132
fun testErrorsWithGetExperimentWithExposureLoggingDisabled() = runBlocking {
133+
val statsig = getStatsigInstance()
135134
statsig.getExperimentWithExposureLoggingDisabled(user, "an_experiment")
136135
assertEquals(1, requests.size)
137136
}
138137

139138
@Test
140139
fun testErrorsWithGetExperimentInLayerForUser() = runBlocking {
140+
val statsig = getStatsigInstance()
141141
statsig.getExperimentInLayerForUser(user, "a_layer")
142142
assertEquals(1, requests.size)
143143
}
144144

145145
@Test
146146
fun testErrorsWithGetLayer() = runBlocking {
147+
val statsig = getStatsigInstance()
147148
statsig.getLayer(user, "a_layer")
148149
assertEquals(1, requests.size)
149150
}
150151

151152
@Test
152153
fun testErrorsWithGetLayerWithCustomExposureLogging() = runBlocking {
154+
val statsig = getStatsigInstance()
153155
statsig.getLayerWithCustomExposureLogging(user, "a_layer") {}
154156
assertEquals(1, requests.size)
155157
}
156158

157159
@Test
158160
fun testErrorsWithLogStringEvent() = runBlocking {
161+
val statsig = getStatsigInstance()
159162
statsig.logEvent(user, "an_event", "a_value")
160163
onRequestWaiter.await(1, TimeUnit.SECONDS)
161164
assertEquals(1, requests.size)
162165
}
163166

164167
@Test
165168
fun testErrorsWithLogDoubleEvent() = runBlocking {
169+
val statsig = getStatsigInstance()
166170
statsig.logEvent(user, "an_event", 1.2)
167171
onRequestWaiter.await(1, TimeUnit.SECONDS)
168172
assertEquals(1, requests.size)

0 commit comments

Comments
 (0)