|
| 1 | +package com.statsig.sdk |
| 2 | + |
| 3 | +import com.google.gson.Gson |
| 4 | +import com.google.gson.GsonBuilder |
| 5 | +import com.google.gson.ToNumberPolicy |
| 6 | +import kotlinx.coroutines.CompletableDeferred |
| 7 | +import kotlinx.coroutines.runBlocking |
| 8 | +import okhttp3.mockwebserver.Dispatcher |
| 9 | +import okhttp3.mockwebserver.MockResponse |
| 10 | +import okhttp3.mockwebserver.MockWebServer |
| 11 | +import okhttp3.mockwebserver.RecordedRequest |
| 12 | +import org.junit.Before |
| 13 | +import org.junit.Test |
| 14 | + |
| 15 | +class ExceptionHandlerTest { |
| 16 | + private lateinit var gson: Gson |
| 17 | + private lateinit var eventLogInputCompletable: CompletableDeferred<LogEventInput> |
| 18 | + private lateinit var evaluator: Evaluator |
| 19 | + private lateinit var driver: StatsigServer |
| 20 | + private lateinit var user: StatsigUser |
| 21 | + private lateinit var options: StatsigOptions |
| 22 | + |
| 23 | + @Before |
| 24 | + fun setUp() { |
| 25 | + gson = GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create() |
| 26 | + user = StatsigUser("abc") |
| 27 | + eventLogInputCompletable = CompletableDeferred() |
| 28 | + |
| 29 | + val mockGateResponse = APIFeatureGate("a_gate", true, "ruleID") |
| 30 | + val mockResponseBody = gson.toJson(mockGateResponse) |
| 31 | + |
| 32 | + val downloadConfigSpecsResponse = |
| 33 | + StatsigE2ETest::class.java.getResource("/layer_exposure_download_config_specs.json")?.readText() ?: "" |
| 34 | + |
| 35 | + val server = MockWebServer() |
| 36 | + server.apply { |
| 37 | + dispatcher = object : Dispatcher() { |
| 38 | + @Throws(InterruptedException::class) |
| 39 | + override fun dispatch(request: RecordedRequest): MockResponse { |
| 40 | + when (request.path) { |
| 41 | + "/v1/download_config_specs" -> { |
| 42 | + return MockResponse().setResponseCode(200).setBody(downloadConfigSpecsResponse) |
| 43 | + } |
| 44 | + "/v1/check_gate" -> { |
| 45 | + return MockResponse().setResponseCode(200).setBody(mockResponseBody) |
| 46 | + } |
| 47 | + "/v1/log_event" -> { |
| 48 | + val logBody = request.body.readUtf8() |
| 49 | + eventLogInputCompletable.complete(gson.fromJson(logBody, LogEventInput::class.java)) |
| 50 | + return MockResponse().setResponseCode(200) |
| 51 | + } |
| 52 | + } |
| 53 | + return MockResponse().setResponseCode(404) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + options = StatsigOptions().apply { |
| 59 | + api = server.url("/v1").toString() |
| 60 | + disableDiagnostics = true |
| 61 | + } |
| 62 | + |
| 63 | + driver = StatsigServer.create() |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun testDaemonThreadException() = runBlocking { |
| 68 | + driver.initialize("server-key", options) |
| 69 | + val daemon = Thread({ |
| 70 | + throw Exception("Throwing from daemon thread") |
| 71 | + }) |
| 72 | + daemon.isDaemon = true |
| 73 | + daemon.start() |
| 74 | + assert(driver.initialized) |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun testThrowingNonDaemon() = runBlocking { |
| 79 | + driver.initialize("server-key", options) |
| 80 | + val nonDaemon = Thread({ |
| 81 | + throw Exception("Throwing from non-daemon thread") |
| 82 | + }) |
| 83 | + nonDaemon.start() |
| 84 | + // For exception throw in other thread, we don't handle |
| 85 | + assert(driver.initialized) |
| 86 | + driver.shutdown() |
| 87 | + assert(!driver.initialized) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun testThrowingOnSameThread() = runBlocking { |
| 92 | + val t = Thread { |
| 93 | + runBlocking { |
| 94 | + driver.initialize("server-key", options) |
| 95 | + } |
| 96 | + throw java.lang.Exception("throw exception") |
| 97 | + } |
| 98 | + t.start() |
| 99 | + assert(!driver.initialized) |
| 100 | + } |
| 101 | +} |
0 commit comments