OkHttpClients { public static OkHttpClient create( ClientConfiguration config, UserAgent userAgent, HostEventsSink hostEventsSink, Class<?> serviceClass) { return create(new OkHttpClient.Builder(), config, userAgent, hostEventsSink, serviceClass); } private  OkHttpClients(); static OkHttpClient create(
            ClientConfiguration config, UserAgent userAgent, HostEventsSink hostEventsSink, Class<?> serviceClass); static OkHttpClient create(
            OkHttpClient.Builder client,
            ClientConfiguration config,
            UserAgent userAgent,
            HostEventsSink hostEventsSink,
            Class<?> serviceClass);  }
@Test public void verifyGlobalMetricsAreRegistered() { TaggedMetricRegistry registry = DefaultTaggedMetricRegistry.getDefault(); ClientConfiguration clientConfiguration = ClientConfiguration.builder() .from(createTestConfig(url)) .taggedMetricRegistry(registry) .build(); OkHttpClients.create(clientConfiguration, AGENT, hostEventsSink, OkHttpClientsTest.class); assertThat(Collections2.transform(registry.getMetrics().keySet(), MetricName::safeName)) .contains( "com.palantir.conjure.java.connection-pool.connections.idle", "com.palantir.conjure.java.connection-pool.connections.total", "com.palantir.conjure.java.dispatcher.calls.queued", "com.palantir.conjure.java.dispatcher.calls.running"); }
@Test(timeout = 10_000) public void randomizesUrls() throws IOException { boolean server2WasHit = false; server.shutdown(); server2.enqueue(new MockResponse().setResponseCode(200).setBody("foo")); while (!server2WasHit) { OkHttpClient client = OkHttpClients.create( ClientConfiguration.builder() .from(createTestConfig(url, url2)) .maxNumRetries(0) .build(), AGENT, hostEventsSink, OkHttpClientsTest.class); Call call = client.newCall(new Request.Builder().url(url).build()); String response = null; try { response = call.execute().body().string(); } catch (Exception e) { System.out.println("Failed to talk to shutdown server (this is expected with p=1/2)"); assertThat(e.getCause().toString()).contains(Integer.toString(server.getPort())); } if (response != null) { assertThat(response).isEqualTo("foo"); server2WasHit = true; } } }
@Test public void meshProxyClientChangesTargetAndInjectHostHeader() throws Exception { server.enqueue(new MockResponse().setBody("foo")); String serviceUrl = "http: ClientConfiguration proxiedConfig = ClientConfiguration.builder() .from(createTestConfig(serviceUrl)) .meshProxy(HostAndPort.fromParts("localhost", server.getPort())) .maxNumRetries(0) .build(); OkHttpClient client = OkHttpClients.create(proxiedConfig, AGENT, hostEventsSink, OkHttpClientsTest.class); assertThat(client.newCall(new Request.Builder().url(serviceUrl).build()) .execute() .body() .string()) .isEqualTo("foo"); assertThat(server.takeRequest().getHeader(HttpHeaders.HOST)).isEqualTo("foo.com"); }
@Test(timeout = 1000) public void non_ioexceptions_dont_break_the_world() throws IOException { server.enqueue(new MockResponse().setBody("foo")); HostEventsSink throwingSink = new HostEventsSink() { @Override public void record(String _serviceName, String _hostname, int _port, int _statusCode, long _micros) { throw new IllegalStateException("I am not an IOException"); } @Override public void recordIoException(String _serviceName, String _hostname, int _port) { } }; OkHttpClient client = OkHttpClients.create( ClientConfiguration.builder() .from(createTestConfig(url)) .maxNumRetries(0) .build(), AGENT, throwingSink, OkHttpClientsTest.class); assertThatThrownBy(() -> client.newCall(new Request.Builder().url(url).build()).execute()) .hasStackTraceContaining("Caught a non-IOException. This is a serious bug and requires investigation"); }
@Test public void clientMadeFromNewBuilderShouldntThrowOnExecute() throws IOException { server.enqueue(new MockResponse().setBody("foo")); ClientConfiguration clientConfiguration = createTestConfig(url); OkHttpClient client = OkHttpClients.create(clientConfiguration, AGENT, hostEventsSink, OkHttpClientsTest.class); client = client.newBuilder().build(); client.newCall(new Request.Builder().url(url).build()).execute(); }