mirror of
https://github.com/square/okhttp.git
synced 2025-08-08 23:42:08 +03:00
Attempt to minimise WebSocket test flakiness (#6045)
This commit is contained in:
@@ -26,7 +26,7 @@ class ClientRuleEventListener(
|
|||||||
var logger: (String) -> Unit
|
var logger: (String) -> Unit
|
||||||
) : EventListener(),
|
) : EventListener(),
|
||||||
EventListener.Factory {
|
EventListener.Factory {
|
||||||
private var startNs: Long = 0
|
private var startNs: Long? = null
|
||||||
|
|
||||||
override fun create(call: Call): EventListener = this
|
override fun create(call: Call): EventListener = this
|
||||||
|
|
||||||
@@ -266,7 +266,14 @@ class ClientRuleEventListener(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun logWithTime(message: String) {
|
private fun logWithTime(message: String) {
|
||||||
val timeMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs)
|
val startNs = startNs
|
||||||
|
val timeMs = if (startNs == null) {
|
||||||
|
// Event occurred before start, for an example an early cancel.
|
||||||
|
0L
|
||||||
|
} else {
|
||||||
|
TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs)
|
||||||
|
}
|
||||||
|
|
||||||
logger.invoke("[$timeMs ms] $message")
|
logger.invoke("[$timeMs ms] $message")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -43,6 +43,7 @@ class OkHttpClientTestRule : TestRule {
|
|||||||
private var testClient: OkHttpClient? = null
|
private var testClient: OkHttpClient? = null
|
||||||
private var uncaughtException: Throwable? = null
|
private var uncaughtException: Throwable? = null
|
||||||
var logger: Logger? = null
|
var logger: Logger? = null
|
||||||
|
lateinit var testName: String
|
||||||
|
|
||||||
var recordEvents = true
|
var recordEvents = true
|
||||||
var recordTaskRunner = false
|
var recordTaskRunner = false
|
||||||
@@ -131,7 +132,16 @@ class OkHttpClientTestRule : TestRule {
|
|||||||
fun ensureAllConnectionsReleased() {
|
fun ensureAllConnectionsReleased() {
|
||||||
testClient?.let {
|
testClient?.let {
|
||||||
val connectionPool = it.connectionPool
|
val connectionPool = it.connectionPool
|
||||||
|
|
||||||
connectionPool.evictAll()
|
connectionPool.evictAll()
|
||||||
|
if (connectionPool.connectionCount() > 0) {
|
||||||
|
// Minimise test flakiness due to possible race conditions with connections closing.
|
||||||
|
// Some number of tests will report here, but not fail due to this delay.
|
||||||
|
println("Delaying to avoid flakes")
|
||||||
|
Thread.sleep(500L)
|
||||||
|
println("After delay: " + connectionPool.connectionCount())
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals(0, connectionPool.connectionCount())
|
assertEquals(0, connectionPool.connectionCount())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -156,6 +166,8 @@ class OkHttpClientTestRule : TestRule {
|
|||||||
): Statement {
|
): Statement {
|
||||||
return object : Statement() {
|
return object : Statement() {
|
||||||
override fun evaluate() {
|
override fun evaluate() {
|
||||||
|
testName = description.methodName
|
||||||
|
|
||||||
val defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
|
val defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
|
||||||
Thread.setDefaultUncaughtExceptionHandler { _, throwable ->
|
Thread.setDefaultUncaughtExceptionHandler { _, throwable ->
|
||||||
initUncaughtException(throwable)
|
initUncaughtException(throwable)
|
||||||
@@ -233,7 +245,7 @@ class OkHttpClientTestRule : TestRule {
|
|||||||
@Synchronized private fun logEvents() {
|
@Synchronized private fun logEvents() {
|
||||||
// Will be ineffective if test overrides the listener
|
// Will be ineffective if test overrides the listener
|
||||||
synchronized(clientEventsList) {
|
synchronized(clientEventsList) {
|
||||||
println("Events (${clientEventsList.size})")
|
println("$testName Events (${clientEventsList.size})")
|
||||||
|
|
||||||
for (e in clientEventsList) {
|
for (e in clientEventsList) {
|
||||||
println(e)
|
println(e)
|
||||||
|
@@ -78,7 +78,6 @@ import static okhttp3.tls.internal.TlsUtil.localhost;
|
|||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.hamcrest.CoreMatchers.any;
|
import static org.hamcrest.CoreMatchers.any;
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
import static org.junit.Assume.assumeThat;
|
import static org.junit.Assume.assumeThat;
|
||||||
|
|
||||||
|
@@ -100,7 +100,7 @@ public final class WebSocketHttpTest {
|
|||||||
platform.assumeNotBouncyCastle();
|
platform.assumeNotBouncyCastle();
|
||||||
}
|
}
|
||||||
|
|
||||||
@After public void tearDown() {
|
@After public void tearDown() throws InterruptedException {
|
||||||
clientListener.assertExhausted();
|
clientListener.assertExhausted();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user