From f631abab4d6db51048e08410d1ebbe44acd2456c Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Thu, 7 May 2020 06:54:17 +0100 Subject: [PATCH] Add Cache events to LoggingEventListener (#6020) --- .../okhttp3/logging/LoggingEventListener.kt | 16 +++++++++++++ .../logging/LoggingEventListenerTest.java | 24 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/LoggingEventListener.kt b/okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/LoggingEventListener.kt index cb98fd0e7..dfd894a70 100644 --- a/okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/LoggingEventListener.kt +++ b/okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/LoggingEventListener.kt @@ -155,6 +155,22 @@ class LoggingEventListener private constructor( logWithTime("canceled") } + override fun satisfactionFailure(call: Call, response: Response) { + logWithTime("satisfactionFailure: $response") + } + + override fun cacheHit(call: Call, response: Response) { + logWithTime("cacheHit: $response") + } + + override fun cacheMiss(call: Call) { + logWithTime("cacheMiss") + } + + override fun cacheConditionalHit(call: Call, cachedResponse: Response) { + logWithTime("cacheConditionalHit: $cachedResponse") + } + private fun logWithTime(message: String) { val timeMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs) logger.log("[$timeMs ms] $message") diff --git a/okhttp-logging-interceptor/src/test/java/okhttp3/logging/LoggingEventListenerTest.java b/okhttp-logging-interceptor/src/test/java/okhttp3/logging/LoggingEventListenerTest.java index f8afbb0a1..998e7e7e5 100644 --- a/okhttp-logging-interceptor/src/test/java/okhttp3/logging/LoggingEventListenerTest.java +++ b/okhttp-logging-interceptor/src/test/java/okhttp3/logging/LoggingEventListenerTest.java @@ -17,9 +17,12 @@ package okhttp3.logging; import java.io.IOException; import java.net.UnknownHostException; +import okhttp3.Call; +import okhttp3.EventListener; import okhttp3.HttpUrl; import okhttp3.MediaType; import okhttp3.OkHttpClient; +import okhttp3.Protocol; import okhttp3.TestUtil; import okhttp3.testing.PlatformRule; import okhttp3.Request; @@ -240,6 +243,27 @@ public final class LoggingEventListenerTest { .assertNoMoreLogs(); } + @Test + public void testCacheEvents() { + Request request = new Request.Builder().url(url).build(); + Call call = client.newCall(request); + Response response = new Response.Builder().request(request).code(200).message("").protocol(HTTP_2).build(); + + EventListener listener = loggingEventListenerFactory.create(call); + + listener.cacheConditionalHit(call, response); + listener.cacheHit(call, response); + listener.cacheMiss(call); + listener.satisfactionFailure(call, response); + + logRecorder + .assertLogMatch("cacheConditionalHit: Response\\{protocol=h2, code=200, message=, url=" + url + "}") + .assertLogMatch("cacheHit: Response\\{protocol=h2, code=200, message=, url=" + url + "}") + .assertLogMatch("cacheMiss") + .assertLogMatch("satisfactionFailure: Response\\{protocol=h2, code=200, message=, url=" + url + "}") + .assertNoMoreLogs(); + } + private Request.Builder request() { return new Request.Builder().url(url); }