@@ -4,13 +4,26 @@ import io.mockk.coVerifySequence
4
4
import io.mockk.spyk
5
5
import io.mockk.unmockkAll
6
6
import kotlinx.coroutines.runBlocking
7
+ import okhttp3.mockwebserver.Dispatcher
7
8
import okhttp3.mockwebserver.MockResponse
8
9
import okhttp3.mockwebserver.MockWebServer
10
+ import okhttp3.mockwebserver.RecordedRequest
9
11
import org.junit.After
10
12
import org.junit.Assert.assertEquals
13
+ import org.junit.Before
11
14
import org.junit.Test
12
15
13
- class NetworkTest {
16
+ internal class NetworkTest {
17
+ var server = MockWebServer ()
18
+ var logEventCount = 0
19
+ val metadata = StatsigMetadata ()
20
+ val options = StatsigOptions ()
21
+ val eb = ErrorBoundary (" " , options, metadata)
22
+ @Before
23
+ fun setup () {
24
+ server = MockWebServer ()
25
+ logEventCount = 0
26
+ }
14
27
15
28
@After
16
29
fun tearDown () {
@@ -19,7 +32,6 @@ class NetworkTest {
19
32
20
33
@Test
21
34
fun testRetry () = runBlocking {
22
- val server = MockWebServer ()
23
35
server.start()
24
36
25
37
val errResponse = MockResponse ()
@@ -32,11 +44,8 @@ class NetworkTest {
32
44
successResponse.setBody(" {}" )
33
45
server.enqueue(successResponse)
34
46
35
- val metadata = StatsigMetadata ()
36
- val options = StatsigOptions ()
37
47
options.api = server.url(" /v1" ).toString()
38
48
39
- val eb = ErrorBoundary (" " , options, metadata)
40
49
val net = spyk(StatsigNetwork (" secret-123" , options, metadata, eb, 1 ))
41
50
42
51
net.postLogs(listOf (StatsigEvent (" TestEvent" )), metadata)
@@ -49,4 +58,54 @@ class NetworkTest {
49
58
net.retryPostLogs(any(), any(), any(), any())
50
59
}
51
60
}
61
+
62
+ @Test
63
+ fun testLogEventRetry () = runBlocking {
64
+ server.apply {
65
+ dispatcher = object : Dispatcher () {
66
+ @Throws(InterruptedException ::class )
67
+ override fun dispatch (request : RecordedRequest ): MockResponse {
68
+ if (" /v1/log_event" in request.path!! ) {
69
+ val logBody = request.body.readUtf8()
70
+ if (request.getHeader(" Content-Type" ) != " application/json; charset=utf-8" ) {
71
+ throw Exception (" No content type set!" )
72
+ }
73
+ ++ logEventCount
74
+ return MockResponse ().setResponseCode(503 )
75
+ }
76
+ return MockResponse ().setResponseCode(400 )
77
+ }
78
+ }
79
+ }
80
+ options.api = server.url(" /v1" ).toString()
81
+ val net = spyk(StatsigNetwork (" secret-123" , options, metadata, eb))
82
+ net.postLogs(listOf (StatsigEvent (" TestEvent" )), metadata)
83
+ println (logEventCount)
84
+ assert (logEventCount == = LOG_EVENT_RETRY_COUNT + 1 )
85
+ }
86
+
87
+ @Test
88
+ fun testLogEventNoRetry () = runBlocking {
89
+ server.apply {
90
+ dispatcher = object : Dispatcher () {
91
+ @Throws(InterruptedException ::class )
92
+ override fun dispatch (request : RecordedRequest ): MockResponse {
93
+ if (" /v1/log_event" in request.path!! ) {
94
+ val logBody = request.body.readUtf8()
95
+ if (request.getHeader(" Content-Type" ) != " application/json; charset=utf-8" ) {
96
+ throw Exception (" No content type set!" )
97
+ }
98
+ ++ logEventCount
99
+ return MockResponse ().setResponseCode(400 )
100
+ }
101
+ return MockResponse ().setResponseCode(400 )
102
+ }
103
+ }
104
+ }
105
+ options.api = server.url(" /v1" ).toString()
106
+ val net = spyk(StatsigNetwork (" secret-123" , options, metadata, eb))
107
+ net.postLogs(listOf (StatsigEvent (" TestEvent" )), metadata)
108
+ println (logEventCount)
109
+ assert (logEventCount == = 1 )
110
+ }
52
111
}
0 commit comments