Skip to content

Commit bdf6d51

Browse files
Fix flaky test. (getsentry#1274)
1 parent 86e9d16 commit bdf6d51

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

sentry-apache-http-client-5/src/test/kotlin/io/sentry/transport/apache/ApacheHttpClientTransportTest.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import io.sentry.SentryOptions
1818
import io.sentry.transport.RateLimiter
1919
import io.sentry.transport.ReusableCountLatch
2020
import java.util.concurrent.CompletableFuture
21+
import java.util.concurrent.Executors
22+
import kotlin.test.AfterTest
2123
import kotlin.test.Test
2224
import kotlin.test.assertEquals
2325
import 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
}

sentry/src/test/java/io/sentry/transport/StdoutTransportTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class StdoutTransportTest {
2626
val event = SentryEvent()
2727
val envelope = SentryEnvelope.from(fixture.serializer, event, null)
2828

29-
val result = transport.send(envelope)
29+
transport.send(envelope)
3030

3131
verify(fixture.serializer).serialize(eq(envelope), any())
3232
}

0 commit comments

Comments
 (0)