private HostMetrics hostMetrics(String hostname, int port) { Collection<HostMetrics> matching = Collections2.filter(hostMetrics.getMetrics(), metrics -> metrics.hostname().equals(hostname) && metrics.port() == port); return Iterables.getOnlyElement(matching); }
@Test public void testIoExceptionRecorded() throws IOException { when(chain.request()).thenReturn(REQUEST_A); when(chain.proceed(any())).thenThrow(IOException.class); assertThat(hostMetrics.getMetrics()).isEmpty(); assertThatExceptionOfType(IOException.class) .isThrownBy(() -> interceptor.intercept(chain)); HostMetrics metrics = Iterables.getOnlyElement(hostMetrics.getMetrics()); assertThat(metrics.getIoExceptions().getCount()).isEqualTo(1); }
@Test public void testIoExceptionsUpdated() { assertThat(hostRegistry.getMetrics()).isEmpty(); hostRegistry.recordIoException("service", "host", 8080); HostMetrics hostMetrics = Iterables.getOnlyElement(hostRegistry.getMetrics()); assertThat(hostMetrics.getIoExceptions().getCount()).isEqualTo(1); } }
@Test public void verifyIoExceptionMetricsAreRegistered() { Call call = createRetryingClient(0, "http://bogus").newCall(new Request.Builder().url("http://bogus").build()); assertThatExceptionOfType(IOException.class) .isThrownBy(call::execute); List<HostMetrics> hostMetrics = hostEventsSink.getMetrics().stream() .filter(metrics -> metrics.hostname().equals("bogus")) .filter(metrics -> metrics.serviceName().equals("OkHttpClientsTest")) .collect(Collectors.toList()); HostMetrics actualMetrics = Iterables.getOnlyElement(hostMetrics); assertThat(actualMetrics.getIoExceptions().getCount()).isEqualTo(1); }
@Test public void verifyResponseMetricsAreRegistered() throws IOException { server.enqueue(new MockResponse().setBody("pong")); createRetryingClient(1).newCall(new Request.Builder().url(url).build()).execute(); List<HostMetrics> hostMetrics = hostEventsSink.getMetrics().stream() .filter(metrics -> metrics.hostname().equals("localhost")) .filter(metrics -> metrics.serviceName().equals("OkHttpClientsTest")) .filter(metrics -> metrics.port() == server.getPort()) .collect(Collectors.toList()); HostMetrics actualMetrics = Iterables.getOnlyElement(hostMetrics); assertThat(actualMetrics.get2xx().getCount()).isGreaterThanOrEqualTo(1); }