當前位置: 首頁>>代碼示例>>Java>>正文


Java MockWebServer.setServerSocketFactory方法代碼示例

本文整理匯總了Java中okhttp3.mockwebserver.MockWebServer.setServerSocketFactory方法的典型用法代碼示例。如果您正苦於以下問題:Java MockWebServer.setServerSocketFactory方法的具體用法?Java MockWebServer.setServerSocketFactory怎麽用?Java MockWebServer.setServerSocketFactory使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在okhttp3.mockwebserver.MockWebServer的用法示例。


在下文中一共展示了MockWebServer.setServerSocketFactory方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: setUp

import okhttp3.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@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();
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:23,代碼來源:DisconnectTest.java

示例2: setUp

import okhttp3.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@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();
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:25,代碼來源:ThreadInterruptTest.java

示例3: writeTimeouts

import okhttp3.mockwebserver.MockWebServer; //導入方法依賴的package包/類
/** 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, TimeUnit.SECONDS)); // Prevent the server from reading!

  connection = urlFactory.open(server.url("/").url());
  connection.setDoOutput(true);
  connection.setChunkedStreamingMode(0);
  OutputStream out = connection.getOutputStream();
  try {
    byte[] data = new byte[2 * 1024 * 1024]; // 2 MiB.
    out.write(data);
    fail();
  } catch (SocketTimeoutException expected) {
  }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:42,代碼來源:URLConnectionTest.java


注:本文中的okhttp3.mockwebserver.MockWebServer.setServerSocketFactory方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。