@@ -9,7 +9,6 @@ import okhttp3.mockwebserver.Dispatcher
9
9
import okhttp3.mockwebserver.MockResponse
10
10
import okhttp3.mockwebserver.MockWebServer
11
11
import okhttp3.mockwebserver.RecordedRequest
12
- import org.junit.After
13
12
import org.junit.AfterClass
14
13
import org.junit.Assert.assertEquals
15
14
import org.junit.Before
@@ -23,146 +22,151 @@ class StatsigErrorBoundaryUsage {
23
22
24
23
companion object {
25
24
private lateinit var server: MockWebServer
26
- private val exception = Exception (" Test Exception" )
27
25
private lateinit var onRequestWaiter: CountDownLatch
28
- private lateinit var statsig: StatsigServer
29
26
private val requests = arrayListOf<RecordedRequest >()
30
27
private var throwOnDownloadConfigSpecs = false
31
28
32
29
@BeforeClass
33
30
@JvmStatic
34
31
internal fun beforeAll () {
35
32
mockkConstructor(Evaluator ::class )
36
- every { anyConstructed<Evaluator >().layers } throws exception
33
+ every { anyConstructed<Evaluator >().layers } throws Exception ( " Test Evaluator Layers " )
37
34
38
35
mockkConstructor(StatsigNetwork ::class )
39
- every { anyConstructed<StatsigNetwork >().shutdown() } throws exception
36
+ every { anyConstructed<StatsigNetwork >().shutdown() } throws Exception ( " Test Network Shutdown " )
40
37
41
38
mockkConstructor(ConfigEvaluation ::class )
42
- every { anyConstructed<ConfigEvaluation >().fetchFromServer } throws exception
39
+ every { anyConstructed<ConfigEvaluation >().fetchFromServer } throws Exception ( " Test Config Eval " )
43
40
44
41
mockkConstructor(StatsigLogger ::class )
45
- coEvery { anyConstructed<StatsigLogger >().log(any()) } throws exception
42
+ coEvery { anyConstructed<StatsigLogger >().log(any()) } throws Exception ( " Test Logger Log " )
46
43
47
44
coEvery { anyConstructed<StatsigNetwork >().downloadConfigSpecs() } coAnswers {
48
45
if (throwOnDownloadConfigSpecs) {
49
- throw exception
46
+ throw Exception ( " Bad Config Specs " )
50
47
}
51
48
callOriginal()
52
49
}
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
+ }
53
60
}
54
61
55
62
@AfterClass
56
63
@JvmStatic
57
64
fun afterAll () {
58
65
unmockkAll()
66
+ server.shutdown()
59
67
}
60
68
}
61
69
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
65
80
}
66
81
67
82
@Before
68
83
internal fun beforeEach () {
69
84
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
-
89
85
onRequestWaiter = CountDownLatch (1 )
90
86
requests.clear()
91
- statsig.errorBoundary.seen.clear()
92
87
}
93
88
94
89
@Test
95
90
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 )
98
92
throwOnDownloadConfigSpecs = true
99
93
100
- localStatsig.initialize()
94
+ runBlocking {
95
+ statsig.initialize()
96
+ }
97
+
101
98
assertEquals(1 , requests.size)
102
99
}
103
100
104
101
@Test
105
102
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()
109
104
assertEquals(0 , requests.size)
110
105
111
- localStatsig .shutdown()
106
+ statsig .shutdown()
112
107
assertEquals(1 , requests.size)
113
108
}
114
109
115
110
@Test
116
111
fun testErrorsWithCheckGate () = runBlocking {
112
+ val statsig = getStatsigInstance()
117
113
statsig.checkGate(user, " a_gate" )
118
114
assertEquals(1 , requests.size)
119
115
}
120
116
121
117
@Test
122
118
fun testErrorsWithGetConfig () = runBlocking {
119
+ val statsig = getStatsigInstance()
123
120
statsig.getConfig(user, " a_config" )
124
121
assertEquals(1 , requests.size)
125
122
}
126
123
127
124
@Test
128
125
fun testErrorsWithGetExperiment () = runBlocking {
126
+ val statsig = getStatsigInstance()
129
127
statsig.getExperiment(user, " an_experiment" )
130
128
assertEquals(1 , requests.size)
131
129
}
132
130
133
131
@Test
134
132
fun testErrorsWithGetExperimentWithExposureLoggingDisabled () = runBlocking {
133
+ val statsig = getStatsigInstance()
135
134
statsig.getExperimentWithExposureLoggingDisabled(user, " an_experiment" )
136
135
assertEquals(1 , requests.size)
137
136
}
138
137
139
138
@Test
140
139
fun testErrorsWithGetExperimentInLayerForUser () = runBlocking {
140
+ val statsig = getStatsigInstance()
141
141
statsig.getExperimentInLayerForUser(user, " a_layer" )
142
142
assertEquals(1 , requests.size)
143
143
}
144
144
145
145
@Test
146
146
fun testErrorsWithGetLayer () = runBlocking {
147
+ val statsig = getStatsigInstance()
147
148
statsig.getLayer(user, " a_layer" )
148
149
assertEquals(1 , requests.size)
149
150
}
150
151
151
152
@Test
152
153
fun testErrorsWithGetLayerWithCustomExposureLogging () = runBlocking {
154
+ val statsig = getStatsigInstance()
153
155
statsig.getLayerWithCustomExposureLogging(user, " a_layer" ) {}
154
156
assertEquals(1 , requests.size)
155
157
}
156
158
157
159
@Test
158
160
fun testErrorsWithLogStringEvent () = runBlocking {
161
+ val statsig = getStatsigInstance()
159
162
statsig.logEvent(user, " an_event" , " a_value" )
160
163
onRequestWaiter.await(1 , TimeUnit .SECONDS )
161
164
assertEquals(1 , requests.size)
162
165
}
163
166
164
167
@Test
165
168
fun testErrorsWithLogDoubleEvent () = runBlocking {
169
+ val statsig = getStatsigInstance()
166
170
statsig.logEvent(user, " an_event" , 1.2 )
167
171
onRequestWaiter.await(1 , TimeUnit .SECONDS )
168
172
assertEquals(1 , requests.size)
0 commit comments