@@ -18,6 +18,8 @@ import io.sentry.SentryOptions
1818import io.sentry.transport.RateLimiter
1919import io.sentry.transport.ReusableCountLatch
2020import java.util.concurrent.CompletableFuture
21+ import java.util.concurrent.Executors
22+ import kotlin.test.AfterTest
2123import kotlin.test.Test
2224import kotlin.test.assertEquals
2325import org.apache.hc.client5.http.async.methods.SimpleHttpResponse
@@ -34,6 +36,7 @@ class ApacheHttpClientTransportTest {
3436 val requestDetails = RequestDetails (" http://key@localhost/proj" , mapOf (" header-name" to " header-value" ))
3537 val client = mock<CloseableHttpAsyncClient >()
3638 val currentlyRunning = spy<ReusableCountLatch >()
39+ val executorService = Executors .newFixedThreadPool(2 )
3740
3841 init {
3942 whenever(rateLimiter.filter(any(), anyOrNull())).thenAnswer { it.arguments[0 ] }
@@ -64,6 +67,11 @@ class ApacheHttpClientTransportTest {
6467
6568 private val fixture = Fixture ()
6669
70+ @AfterTest
71+ fun `shutdown executor` () {
72+ fixture.executorService.shutdownNow()
73+ }
74+
6775 @Test
6876 fun `updates retry on rate limiter` () {
6977 val response = SimpleHttpResponse (200 )
@@ -113,7 +121,7 @@ class ApacheHttpClientTransportTest {
113121 fun `flush waits till all requests are finished` () {
114122 val sut = fixture.getSut()
115123 whenever(fixture.client.execute(any(), any())).then {
116- CompletableFuture .runAsync {
124+ fixture.executorService.submit {
117125 Thread .sleep(5 )
118126 (it.arguments[1 ] as FutureCallback <SimpleHttpResponse >).completed(SimpleHttpResponse (200 ))
119127 }
@@ -131,7 +139,7 @@ class ApacheHttpClientTransportTest {
131139 fun `keeps sending events after flush` () {
132140 val sut = fixture.getSut()
133141 whenever(fixture.client.execute(any(), any())).then {
134- CompletableFuture .runAsync {
142+ fixture.executorService.submit {
135143 Thread .sleep(5 )
136144 (it.arguments[1 ] as FutureCallback <SimpleHttpResponse >).completed(SimpleHttpResponse (200 ))
137145 }
@@ -150,22 +158,22 @@ class ApacheHttpClientTransportTest {
150158 fun `logs warning when flush timeout was lower than time needed to execute all events` () {
151159 val sut = fixture.getSut()
152160 whenever(fixture.client.execute(any(), any())).then {
153- CompletableFuture .runAsync {
154- Thread .sleep(100 )
161+ fixture.executorService.submit {
162+ Thread .sleep(1000 )
155163 (it.arguments[1 ] as FutureCallback <SimpleHttpResponse >).completed(SimpleHttpResponse (200 ))
156164 }
157165 }.then {
158- CompletableFuture .runAsync {
159- Thread .sleep(5 )
166+ fixture.executorService.submit {
167+ Thread .sleep(20 )
160168 (it.arguments[1 ] as FutureCallback <SimpleHttpResponse >).completed(SimpleHttpResponse (200 ))
161169 }
162170 }
163171 sut.send(SentryEnvelope .from(fixture.options.serializer, SentryEvent (), null ))
164172 sut.send(SentryEnvelope .from(fixture.options.serializer, SentryEvent (), null ))
165173
166- sut.flush(10 )
174+ sut.flush(200 )
167175
176+ verify(fixture.logger).log(SentryLevel .WARNING , " Failed to flush all events within %s ms" , 200L )
168177 verify(fixture.currentlyRunning, times(1 )).decrement()
169- verify(fixture.logger).log(SentryLevel .WARNING , " Failed to flush all events within %s ms" , 10L )
170178 }
171179}
0 commit comments