Skip to content

Commit d6b4f0b

Browse files
committed
Ensure same connection
1 parent f8595e3 commit d6b4f0b

File tree

11 files changed

+88
-30
lines changed

11 files changed

+88
-30
lines changed

okhttp-logging-interceptor/api/logging-interceptor.api

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ public final class okhttp3/logging/LoggingEventListener : okhttp3/EventListener
7878
public fun secureConnectEnd (Lokhttp3/Call;Lokhttp3/Handshake;)V
7979
public fun secureConnectStart (Lokhttp3/Call;)V
8080
public fun socketSinkEnd (Lokhttp3/Call;J)V
81-
public fun socketSinkStart (Lokhttp3/Call;)V
81+
public fun socketSinkStart (Lokhttp3/Call;Lokhttp3/Connection;)V
8282
public fun socketSourceEnd (Lokhttp3/Call;J)V
83-
public fun socketSourceStart (Lokhttp3/Call;)V
83+
public fun socketSourceStart (Lokhttp3/Call;Lokhttp3/Connection;)V
8484
}
8585

8686
public final class okhttp3/logging/LoggingEventListener$Companion {

okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/LoggingEventListener.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ class LoggingEventListener private constructor(
174174
logWithTime("requestFailed: $ioe")
175175
}
176176

177-
override fun socketSinkStart(call: Call) {
177+
override fun socketSinkStart(
178+
call: Call,
179+
connection: Connection,
180+
) {
178181
logWithTime("socketSinkStart")
179182
}
180183

@@ -207,7 +210,10 @@ class LoggingEventListener private constructor(
207210
logWithTime("responseBodyEnd: byteCount=$byteCount")
208211
}
209212

210-
override fun socketSourceStart(call: Call) {
213+
override fun socketSourceStart(
214+
call: Call,
215+
connection: Connection,
216+
) {
211217
logWithTime("socketSourceStart")
212218
}
213219

okhttp-testing-support/src/main/kotlin/okhttp3/ClientRuleEventListener.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,13 @@ class ClientRuleEventListener(
199199
delegate.requestFailed(call, ioe)
200200
}
201201

202-
override fun socketSinkStart(call: Call) {
202+
override fun socketSinkStart(
203+
call: Call,
204+
connection: Connection,
205+
) {
203206
logWithTime("socketSinkStart")
204207

205-
delegate.socketSinkStart(call)
208+
delegate.socketSinkStart(call, connection)
206209
}
207210

208211
override fun socketSinkEnd(
@@ -244,10 +247,13 @@ class ClientRuleEventListener(
244247
delegate.responseBodyEnd(call, byteCount)
245248
}
246249

247-
override fun socketSourceStart(call: Call) {
250+
override fun socketSourceStart(
251+
call: Call,
252+
connection: Connection,
253+
) {
248254
logWithTime("socketSourceStart")
249255

250-
delegate.socketSourceStart(call)
256+
delegate.socketSourceStart(call, connection)
251257
}
252258

253259
override fun socketSourceEnd(

okhttp-testing-support/src/main/kotlin/okhttp3/RecordingEventListener.kt

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,16 @@ open class RecordingEventListener(
146146
}
147147
}
148148

149-
private fun logEvent(e: CallEvent) {
149+
private fun logEvent(
150+
e: CallEvent,
151+
connection: Connection? = null,
152+
) {
150153
for (lock in forbiddenLocks) {
151154
assertThat(Thread.holdsLock(lock), lock.toString()).isFalse()
152155
}
153156

154157
if (enforceOrder) {
158+
checkForSameConnection(e, connection)
155159
checkForStartEvent(e)
156160
}
157161

@@ -173,6 +177,21 @@ open class RecordingEventListener(
173177
}
174178
}
175179

180+
private fun checkForSameConnection(
181+
e: CallEvent,
182+
connection: Connection?,
183+
) {
184+
if (connection == null || eventSequence.none { it is ConnectionAcquired }) {
185+
return
186+
}
187+
eventSequence.forEach loop@{
188+
if (it is ConnectionAcquired && it.connection == connection) {
189+
return // found related ConnectionAcquired event
190+
}
191+
}
192+
fail<Any>("event $e without matching connection acquired event or different connection")
193+
}
194+
176195
override fun dispatcherQueueStart(
177196
call: Call,
178197
dispatcher: Dispatcher,
@@ -264,7 +283,10 @@ open class RecordingEventListener(
264283
ioe: IOException,
265284
) = logEvent(RequestFailed(System.nanoTime(), call, ioe))
266285

267-
override fun socketSinkStart(call: Call) = logEvent(SocketSinkStart(System.nanoTime(), call))
286+
override fun socketSinkStart(
287+
call: Call,
288+
connection: Connection,
289+
) = logEvent(SocketSinkStart(System.nanoTime(), call), connection)
268290

269291
override fun socketSinkEnd(
270292
call: Call,
@@ -285,7 +307,10 @@ open class RecordingEventListener(
285307
byteCount: Long,
286308
) = logEvent(ResponseBodyEnd(System.nanoTime(), call, byteCount))
287309

288-
override fun socketSourceStart(call: Call) = logEvent(SocketSourceStart(System.nanoTime(), call))
310+
override fun socketSourceStart(
311+
call: Call,
312+
connection: Connection,
313+
) = logEvent(SocketSourceStart(System.nanoTime(), call), connection)
289314

290315
override fun socketSourceEnd(
291316
call: Call,

okhttp/api/android/okhttp.api

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,9 @@ public abstract class okhttp3/EventListener {
518518
public fun secureConnectEnd (Lokhttp3/Call;Lokhttp3/Handshake;)V
519519
public fun secureConnectStart (Lokhttp3/Call;)V
520520
public fun socketSinkEnd (Lokhttp3/Call;J)V
521-
public fun socketSinkStart (Lokhttp3/Call;)V
521+
public fun socketSinkStart (Lokhttp3/Call;Lokhttp3/Connection;)V
522522
public fun socketSourceEnd (Lokhttp3/Call;J)V
523-
public fun socketSourceStart (Lokhttp3/Call;)V
523+
public fun socketSourceStart (Lokhttp3/Call;Lokhttp3/Connection;)V
524524
}
525525

526526
public final class okhttp3/EventListener$Companion {

okhttp/api/jvm/okhttp.api

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,9 @@ public abstract class okhttp3/EventListener {
518518
public fun secureConnectEnd (Lokhttp3/Call;Lokhttp3/Handshake;)V
519519
public fun secureConnectStart (Lokhttp3/Call;)V
520520
public fun socketSinkEnd (Lokhttp3/Call;J)V
521-
public fun socketSinkStart (Lokhttp3/Call;)V
521+
public fun socketSinkStart (Lokhttp3/Call;Lokhttp3/Connection;)V
522522
public fun socketSourceEnd (Lokhttp3/Call;J)V
523-
public fun socketSourceStart (Lokhttp3/Call;)V
523+
public fun socketSourceStart (Lokhttp3/Call;Lokhttp3/Connection;)V
524524
}
525525

526526
public final class okhttp3/EventListener$Companion {

okhttp/src/commonJvmAndroid/kotlin/okhttp3/EventListener.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,12 @@ abstract class EventListener {
318318
/**
319319
* Invoked just prior to sending bytes on the socket. Will only be invoked for upgraded connections.
320320
*
321-
* The connection is implicit, and will generally relate to the last [connectionAcquired] event.
321+
* The connection will match the last [connectionAcquired] event.
322322
*/
323-
open fun socketSinkStart(call: Call) {
323+
open fun socketSinkStart(
324+
call: Call,
325+
connection: Connection,
326+
) {
324327
}
325328

326329
/**
@@ -401,13 +404,18 @@ abstract class EventListener {
401404
}
402405

403406
/**
404-
* TODO docs
407+
* Invoked immediately after receiving bytes on the socket.
405408
*/
406-
open fun socketSourceStart(call: Call) {
409+
open fun socketSourceStart(
410+
call: Call,
411+
connection: Connection,
412+
) {
407413
}
408414

409415
/**
410-
* TODO docs
416+
* Invoked immediately before closing the source.
417+
*
418+
* This method is always invoked after [socketSourceStart].
411419
*/
412420
open fun socketSourceEnd(
413421
call: Call,

okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/connection/Exchange.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class Exchange(
274274
try {
275275
if (invokeStartEvent) {
276276
invokeStartEvent = false
277-
eventListener.socketSinkStart(call)
277+
eventListener.socketSinkStart(call, connection)
278278
}
279279
super.write(source, byteCount)
280280
this.bytesReceived += byteCount
@@ -347,7 +347,7 @@ class Exchange(
347347
if (invokeStartEvent) {
348348
invokeStartEvent = false
349349
if (isSocket) {
350-
eventListener.socketSourceStart(call)
350+
eventListener.socketSourceStart(call, connection)
351351
} else {
352352
eventListener.responseBodyStart(call)
353353
}

okhttp/src/jvmTest/kotlin/okhttp3/KotlinSourceModernTest.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,18 +462,21 @@ class KotlinSourceModernTest {
462462
byteCount: Long,
463463
) = TODO()
464464

465-
override fun requestFailed(
465+
override fun socketSinkStart(
466466
call: Call,
467-
ioe: IOException,
467+
connection: Connection,
468468
) = TODO()
469469

470-
override fun socketSinkStart(call: Call) = TODO()
471-
472470
override fun socketSinkEnd(
473471
call: Call,
474472
byteCount: Long,
475473
) = TODO()
476474

475+
override fun requestFailed(
476+
call: Call,
477+
ioe: IOException,
478+
) = TODO()
479+
477480
override fun responseHeadersStart(call: Call) = TODO()
478481

479482
override fun responseHeadersEnd(
@@ -488,6 +491,16 @@ class KotlinSourceModernTest {
488491
byteCount: Long,
489492
) = TODO()
490493

494+
override fun socketSourceStart(
495+
call: Call,
496+
connection: Connection,
497+
) = TODO()
498+
499+
override fun socketSourceEnd(
500+
call: Call,
501+
byteCount: Long,
502+
) = TODO()
503+
491504
override fun responseFailed(
492505
call: Call,
493506
ioe: IOException,

samples/guide/src/main/java/okhttp3/recipes/PrintEvents.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private void printEvent(String name) {
169169
printEvent("requestFailed");
170170
}
171171

172-
@Override public void socketSinkStart(Call call) {
172+
@Override public void socketSinkStart(Call call, Connection connection) {
173173
printEvent("socketSinkStart");
174174
}
175175

@@ -193,7 +193,7 @@ private void printEvent(String name) {
193193
printEvent("responseBodyEnd");
194194
}
195195

196-
@Override public void socketSourceStart(Call call) {
196+
@Override public void socketSourceStart(Call call, Connection connection) {
197197
printEvent("socketSourceStart");
198198
}
199199

0 commit comments

Comments
 (0)