use of okhttp3.mockwebserver.SocketPolicy in project okhttp by square.
the class URLConnectionTest method testServerClosesOutput.
private void testServerClosesOutput(SocketPolicy socketPolicy) throws Exception {
server.enqueue(new MockResponse().setBody("This connection won't pool properly").setSocketPolicy(socketPolicy));
MockResponse responseAfter = new MockResponse().setBody("This comes after a busted connection");
server.enqueue(responseAfter);
// Enqueue 2x because the broken connection may be reused.
server.enqueue(responseAfter);
HttpURLConnection connection1 = urlFactory.open(server.url("/a").url());
connection1.setReadTimeout(100);
assertContent("This connection won't pool properly", connection1);
assertEquals(0, server.takeRequest().getSequenceNumber());
// Give the server time to enact the socket policy if it's one that could happen after the
// client has received the response.
Thread.sleep(500);
HttpURLConnection connection2 = urlFactory.open(server.url("/b").url());
connection2.setReadTimeout(100);
assertContent("This comes after a busted connection", connection2);
// Check that a fresh connection was created, either immediately or after attempting reuse.
RecordedRequest requestAfter = server.takeRequest();
if (server.getRequestCount() == 3) {
// The failure consumed a response.
requestAfter = server.takeRequest();
}
// sequence number 0 means the HTTP socket connection was not reused
assertEquals(0, requestAfter.getSequenceNumber());
}
use of okhttp3.mockwebserver.SocketPolicy in project okhttp by square.
the class MockWebServer method start.
/**
* Starts the server and binds to the given socket address.
*
* @param inetSocketAddress the socket address to bind the server on
*/
private synchronized void start(InetSocketAddress inetSocketAddress) throws IOException {
if (started)
throw new IllegalStateException("start() already called");
started = true;
executor = Executors.newCachedThreadPool(Util.threadFactory("MockWebServer", false));
this.inetSocketAddress = inetSocketAddress;
serverSocket = serverSocketFactory.createServerSocket();
// Reuse if the user specified a port
serverSocket.setReuseAddress(inetSocketAddress.getPort() != 0);
serverSocket.bind(inetSocketAddress, 50);
port = serverSocket.getLocalPort();
executor.execute(new NamedRunnable("MockWebServer %s", port) {
@Override
protected void execute() {
try {
logger.info(MockWebServer.this + " starting to accept connections");
acceptConnections();
} catch (Throwable e) {
logger.log(Level.WARNING, MockWebServer.this + " failed unexpectedly", e);
}
// Release all sockets and all threads, even if any close fails.
closeQuietly(serverSocket);
for (Iterator<Socket> s = openClientSockets.iterator(); s.hasNext(); ) {
closeQuietly(s.next());
s.remove();
}
for (Iterator<Http2Connection> s = openConnections.iterator(); s.hasNext(); ) {
closeQuietly(s.next());
s.remove();
}
dispatcher.shutdown();
executor.shutdown();
}
private void acceptConnections() throws Exception {
while (true) {
Socket socket;
try {
socket = serverSocket.accept();
} catch (SocketException e) {
logger.info(MockWebServer.this + " done accepting connections: " + e.getMessage());
return;
}
SocketPolicy socketPolicy = dispatcher.peek().getSocketPolicy();
if (socketPolicy == DISCONNECT_AT_START) {
dispatchBookkeepingRequest(0, socket);
socket.close();
} else {
openClientSockets.add(socket);
serveConnection(socket);
}
}
}
});
}
Aggregations