Skip to content

Commit 2e298a3

Browse files
romtsnclaude
andauthored
test(okhttp): Wait until the reader is blocked before closing the body (#6604)
The concurrent close test signaled right after the reader got the first chunk, before it entered the next read(). When close() landed in that gap, the reader's next read() threw Okio's "closed" IllegalStateException, the thread died, and the test timed out waiting for it. ParkingSource now signals right before it blocks, so close() always lands while the reader is inside read(). Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 3b6fa83 commit 2e298a3

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

‎sentry-okhttp/src/test/java/io/sentry/okhttp/CapturedResponseBodyTest.kt‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,16 @@ class CapturedResponseBodyTest {
7171
private val closed = CountDownLatch(1)
7272
private var sent = false
7373

74+
/** Counts down once a read is blocked waiting for [close]. */
75+
val parked = CountDownLatch(1)
76+
7477
override fun read(sink: Buffer, byteCount: Long): Long {
7578
if (!sent) {
7679
sent = true
7780
sink.write(chunk)
7881
return chunk.size.toLong()
7982
}
83+
parked.countDown()
8084
closed.await(10, TimeUnit.SECONDS)
8185
return -1L
8286
}
@@ -260,19 +264,19 @@ class CapturedResponseBodyTest {
260264
val chunk = "data: event-0\n\n".toByteArray()
261265
val source = ParkingSource(chunk)
262266
val (wrapper, captured) = capture(source, 1024)
263-
val chunkRead = CountDownLatch(1)
264267
val readDone = CountDownLatch(1)
265268
val reader = Thread {
266269
val bodySource = wrapper.source()
267270
bodySource.readByteArray(chunk.size.toLong())
268-
chunkRead.countDown()
269271
bodySource.readByteArray()
270272
readDone.countDown()
271273
}
272274
reader.isDaemon = true
273275
reader.start()
274276

275-
assertTrue(chunkRead.await(10, TimeUnit.SECONDS), "the reader must have received the chunk")
277+
// Wait until the reader is blocked inside read(). Closing before that would make the next read
278+
// throw "closed" on the reader thread instead of returning end of stream.
279+
assertTrue(source.parked.await(10, TimeUnit.SECONDS), "the reader must be blocked in read()")
276280
wrapper.close()
277281

278282
assertTrue(

0 commit comments

Comments
 (0)