use of zipkin2.Call in project okhttp by square.
the class OkHttpAsync method prepare.
@Override
public void prepare(final Benchmark benchmark) {
concurrencyLevel = benchmark.concurrencyLevel;
targetBacklog = benchmark.targetBacklog;
client = new OkHttpClient.Builder().protocols(benchmark.protocols).dispatcher(new Dispatcher(new ThreadPoolExecutor(benchmark.concurrencyLevel, benchmark.concurrencyLevel, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()))).build();
if (benchmark.tls) {
SslClient sslClient = SslClient.localhost();
SSLSocketFactory socketFactory = sslClient.socketFactory;
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession session) {
return true;
}
};
client = client.newBuilder().sslSocketFactory(socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
}
callback = new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("Failed: " + e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
ResponseBody body = response.body();
long total = SynchronousHttpClient.readAllAndClose(body.byteStream());
long finish = System.nanoTime();
if (VERBOSE) {
long start = (Long) response.request().tag();
System.out.printf("Transferred % 8d bytes in %4d ms%n", total, TimeUnit.NANOSECONDS.toMillis(finish - start));
}
requestsInFlight.decrementAndGet();
}
};
}
use of zipkin2.Call in project okhttp by square.
the class HttpOverHttp2Test method receiveResponseCookies.
@Test
public void receiveResponseCookies() throws Exception {
RecordingCookieJar cookieJar = new RecordingCookieJar();
client = client.newBuilder().cookieJar(cookieJar).build();
server.enqueue(new MockResponse().addHeader("set-cookie: a=b"));
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertEquals("", response.body().string());
cookieJar.assertResponseCookies("a=b; path=/");
}
use of zipkin2.Call in project okhttp by square.
the class HttpOverHttp2Test method connectionTimeout.
@Test
public void connectionTimeout() throws Exception {
server.enqueue(new MockResponse().setBody("A").setBodyDelay(1, SECONDS));
OkHttpClient client1 = client.newBuilder().readTimeout(2000, MILLISECONDS).build();
Call call1 = client1.newCall(new Request.Builder().url(server.url("/")).build());
OkHttpClient client2 = client.newBuilder().readTimeout(200, MILLISECONDS).build();
Call call2 = client2.newCall(new Request.Builder().url(server.url("/")).build());
Response response1 = call1.execute();
assertEquals("A", response1.body().string());
try {
call2.execute();
fail();
} catch (IOException expected) {
}
// Confirm that the connection was reused.
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(1, server.takeRequest().getSequenceNumber());
}
use of zipkin2.Call in project okhttp by square.
the class HttpOverHttp2Test method redirect.
@Test
public void redirect() throws Exception {
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: /foo").setBody("This page has moved!"));
server.enqueue(new MockResponse().setBody("This is the new location!"));
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertEquals("This is the new location!", response.body().string());
RecordedRequest request1 = server.takeRequest();
assertEquals("/", request1.getPath());
RecordedRequest request2 = server.takeRequest();
assertEquals("/foo", request2.getPath());
}
use of zipkin2.Call in project okhttp by square.
the class HttpOverHttp2Test method closeAfterFlush.
@Test
public void closeAfterFlush() throws Exception {
final byte[] postBytes = "FGHIJ".getBytes(Util.UTF_8);
server.enqueue(new MockResponse().setBody("ABCDE"));
Call call = client.newCall(new Request.Builder().url(server.url("/foo")).post(new RequestBody() {
@Override
public MediaType contentType() {
return MediaType.parse("text/plain; charset=utf-8");
}
@Override
public long contentLength() throws IOException {
return postBytes.length;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
// push bytes into the stream's buffer
sink.write(postBytes);
// Http2Connection.writeData subject to write window
sink.flush();
// Http2Connection.writeData empty frame
sink.close();
}
}).build());
Response response = call.execute();
assertEquals("ABCDE", response.body().string());
RecordedRequest request = server.takeRequest();
assertEquals("POST /foo HTTP/1.1", request.getRequestLine());
assertArrayEquals(postBytes, request.getBody().readByteArray());
assertEquals(postBytes.length, Integer.parseInt(request.getHeader("Content-Length")));
}
Aggregations