HttpEventClient implements EventClient { @SafeVarargs @Override public final <T> ListenableFuture<Void> post(T... event) throws IllegalArgumentException { requireNonNull(event, "event is null"); return post(Arrays.asList(event)); } @Inject  HttpEventClient(
            JsonEventWriter eventWriter,
            NodeInfo nodeInfo,
            @ForEventClient HttpClient httpClient); @Flatten RequestStats getStats(); @SafeVarargs @Override final ListenableFuture<Void> post(T... event); @Override ListenableFuture<Void> post(final Iterable<T> events);  }
@Test(expectedExceptions = ServiceUnavailableException.class, expectedExceptionsMessageRegExp = ".*has no instances.*") public void testFutureFailsWhenServiceUnavailable() throws InterruptedException { client = newEventClient(ImmutableSet.of()); try { client.post(new FixedDummyEventClass("host", new DateTime(), UUID.randomUUID(), 1, "foo")).get(); } catch (ExecutionException e) { throwIfUnchecked(e.getCause()); throw new RuntimeException(e.getCause()); } }
@Test public void testCallSucceedsWhenServiceUnavailable() { client = newEventClient(ImmutableSet.of()); client.post(new FixedDummyEventClass("host", new DateTime(), UUID.randomUUID(), 1, "foo")); assertNull(servlet.lastPath); assertNull(servlet.lastBody); }
@Test public void testServerFails() throws InterruptedException { responseCode = 503; client = newEventClient(ImmutableSet.of(baseUri)); ListenableFuture<Void> future = client.post(TestingUtils.getEvents()); try { future.get(); fail("expected exception"); } catch (ExecutionException e) { assertInstanceOf(e.getCause(), UnexpectedResponseException.class); } assertEquals(servlet.lastPath, "/v2/event"); }
@Test public void testReceivesEvent() throws ExecutionException, InterruptedException, IOException { client = newEventClient(ImmutableSet.of(baseUri)); client.post(TestingUtils.getEvents()).get(); assertEquals(servlet.lastPath, "/v2/event"); assertEquals(new ObjectMapper().readValue(servlet.lastBody, Object.class), TestingUtils.getExpectedJson(), "JSON encoding " + servlet.lastBody); }
@Test public void loadTest() throws ExecutionException, InterruptedException, IOException { client = newEventClient(ImmutableSet.of(baseUri)); List<Future<Void>> futures = new ArrayList<>(); for (int i = 0; i < 100; i++) { futures.add(client.post(TestingUtils.getEvents())); } for (Future<Void> future : futures) { future.get(); System.out.println("future " + future); } assertEquals(servlet.lastPath, "/v2/event"); assertEquals(new ObjectMapper().readValue(servlet.lastBody, Object.class), TestingUtils.getExpectedJson(), "JSON encoding " + servlet.lastBody); }