Search in sources :

Example 1 with DelegatingSocketFactory

use of okhttp3.DelegatingSocketFactory in project okhttp by square.

the class DisconnectTest method setUp.

@Before
public void setUp() throws Exception {
    // Sockets on some platforms can have large buffers that mean writes do not block when
    // required. These socket factories explicitly set the buffer sizes on sockets created.
    server = new MockWebServer();
    server.setServerSocketFactory(new DelegatingServerSocketFactory(ServerSocketFactory.getDefault()) {

        @Override
        protected ServerSocket configureServerSocket(ServerSocket serverSocket) throws IOException {
            serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
            return serverSocket;
        }
    });
    client = defaultClient().newBuilder().socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {

        @Override
        protected Socket configureSocket(Socket socket) throws IOException {
            socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
            socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
            return socket;
        }
    }).build();
}
Also used : DelegatingSocketFactory(okhttp3.DelegatingSocketFactory) DelegatingServerSocketFactory(okhttp3.DelegatingServerSocketFactory) MockWebServer(okhttp3.mockwebserver.MockWebServer) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Before(org.junit.Before)

Example 2 with DelegatingSocketFactory

use of okhttp3.DelegatingSocketFactory in project okhttp by square.

the class URLConnectionTest method writeTimeouts.

/** Confirm that an unacknowledged write times out. */
@Test
public void writeTimeouts() throws IOException {
    MockWebServer server = new MockWebServer();
    // Sockets on some platforms can have large buffers that mean writes do not block when
    // required. These socket factories explicitly set the buffer sizes on sockets created.
    final int SOCKET_BUFFER_SIZE = 4 * 1024;
    server.setServerSocketFactory(new DelegatingServerSocketFactory(ServerSocketFactory.getDefault()) {

        @Override
        protected ServerSocket configureServerSocket(ServerSocket serverSocket) throws IOException {
            serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
            return serverSocket;
        }
    });
    urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {

        @Override
        protected Socket configureSocket(Socket socket) throws IOException {
            socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
            socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
            return socket;
        }
    }).writeTimeout(500, TimeUnit.MILLISECONDS).build());
    server.start();
    server.enqueue(new MockResponse().throttleBody(1, 1, // Prevent the server from reading!
    TimeUnit.SECONDS));
    connection = urlFactory.open(server.url("/").url());
    connection.setDoOutput(true);
    connection.setChunkedStreamingMode(0);
    OutputStream out = connection.getOutputStream();
    try {
        // 2 MiB.
        byte[] data = new byte[2 * 1024 * 1024];
        out.write(data);
        fail();
    } catch (SocketTimeoutException expected) {
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SocketTimeoutException(java.net.SocketTimeoutException) OutputStream(java.io.OutputStream) MockWebServer(okhttp3.mockwebserver.MockWebServer) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) Test(org.junit.Test)

Example 3 with DelegatingSocketFactory

use of okhttp3.DelegatingSocketFactory in project okhttp by square.

the class ThreadInterruptTest method setUp.

@Before
public void setUp() throws Exception {
    // Sockets on some platforms can have large buffers that mean writes do not block when
    // required. These socket factories explicitly set the buffer sizes on sockets created.
    server = new MockWebServer();
    server.setServerSocketFactory(new DelegatingServerSocketFactory(ServerSocketFactory.getDefault()) {

        @Override
        protected ServerSocket configureServerSocket(ServerSocket serverSocket) throws IOException {
            serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
            return serverSocket;
        }
    });
    client = defaultClient().newBuilder().socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {

        @Override
        protected Socket configureSocket(Socket socket) throws IOException {
            socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
            socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
            return socket;
        }
    }).build();
}
Also used : DelegatingSocketFactory(okhttp3.DelegatingSocketFactory) DelegatingServerSocketFactory(okhttp3.DelegatingServerSocketFactory) MockWebServer(okhttp3.mockwebserver.MockWebServer) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Before(org.junit.Before)

Aggregations

IOException (java.io.IOException)3 ServerSocket (java.net.ServerSocket)3 Socket (java.net.Socket)3 MockWebServer (okhttp3.mockwebserver.MockWebServer)3 DelegatingServerSocketFactory (okhttp3.DelegatingServerSocketFactory)2 DelegatingSocketFactory (okhttp3.DelegatingSocketFactory)2 Before (org.junit.Before)2 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1 SocketTimeoutException (java.net.SocketTimeoutException)1 SSLSocket (javax.net.ssl.SSLSocket)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 Test (org.junit.Test)1