Skip to content

Commit

Permalink
Slow connects
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke committed Apr 27, 2024
1 parent 9899f03 commit 6338814
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import javax.net.ssl.SSLSocket
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
import kotlin.time.Duration.Companion.milliseconds
import mockwebserver3.SocketPolicy.DisconnectAfterRequest
import mockwebserver3.SocketPolicy.DisconnectAtEnd
import mockwebserver3.SocketPolicy.DisconnectAtStart
Expand Down Expand Up @@ -377,7 +378,7 @@ class MockWebServer : Closeable {
val socketPolicy = dispatcher.peek().socketPolicy

if (socketPolicy is SocketPolicy.DelayAccept) {
Thread.sleep(socketPolicy.delay.inWholeMilliseconds)
Thread.sleep(100.milliseconds.inWholeMilliseconds)
}

val socket: Socket
Expand Down
29 changes: 29 additions & 0 deletions okhttp/src/test/java/okhttp3/SlowNetworkTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
*/
package okhttp3

import java.net.Socket
import java.net.SocketAddress
import java.util.concurrent.CountDownLatch
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
import mockwebserver3.MockResponse
import mockwebserver3.MockWebServer
import mockwebserver3.SocketPolicy
import okhttp3.internal.connection.RealConnection
import okhttp3.testing.PlatformRule
import okio.IOException
import org.junit.jupiter.api.BeforeEach
Expand Down Expand Up @@ -50,8 +53,33 @@ class SlowNetworkTest {
handshakeCertificates.sslSocketFactory(),
handshakeCertificates.trustManager,
)
.socketFactory(object : DelegatingSocketFactory(getDefault()) {
override fun createSocket(): Socket {
return object : Socket() {
override fun connect(endpoint: SocketAddress?) {
Thread.sleep(100)
super.connect(endpoint)
}

override fun connect(endpoint: SocketAddress?, timeout: Int) {
Thread.sleep(100)
super.connect(endpoint, timeout)
}

override fun close() {
Thread.sleep(100)
super.close()
}
}
}
})
.callTimeout(15.seconds)
.connectTimeout(15.seconds)
.eventListener(object : EventListener() {
override fun connectionAcquired(call: Call, connection: Connection) {
(connection as RealConnection).noNewExchanges()
}
})
.build()

server.useHttps(handshakeCertificates.sslSocketFactory())
Expand Down Expand Up @@ -84,6 +112,7 @@ class SlowNetworkTest {
call: Call,
response: Response,
) {
// println("response")
response.body.string()
latch.countDown()
}
Expand Down

0 comments on commit 6338814

Please sign in to comment.