private void createPubscribers(int amount) { IntStream.range(0, amount).forEach(i -> commonPool.add(new DRPCQueryResultPubscriber(config))); } }
private Consumer<Response> createResponseConsumer(String id) { // Create a closure with id return response -> handleResponse(id, response); }
@Test(timeOut = 5000L) public void testReadingNullResponse() throws Exception { CompletableFuture<Response> response = getOkFuture(null); AsyncHttpClient mockClient = mockClientWith(response); pubscriber.setClient(mockClient); pubscriber.send(new PubSubMessage("foo", "bar")); // This is async (but practically still very fast since we mocked the response), so need a timeout. PubSubMessage actual = fetchAsync().get(); Assert.assertNotNull(actual); Assert.assertEquals(actual.getId(), "foo"); Assert.assertEquals(actual.getContent(), CANNOT_REACH_DRPC.asJSONClip()); }
@Override public void send(PubSubMessage message) throws PubSubException { String url = urls.get(); String id = message.getId(); String json = message.asJSON(); log.info("Posting to {} for id {}", url, id); log.debug("Posting to {} with body {}", url, json); client.preparePost(url).setBody(json).execute().toCompletableFuture() .exceptionally(this::handleException) .thenAcceptAsync(createResponseConsumer(id)); }
private PubSubMessage fetch() { try { PubSubMessage message; do { message = pubscriber.receive(); Thread.sleep(1); } while (message == null); return message; } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void testFailing() { AsyncHttpClient mockClient = mock(AsyncHttpClient.class); pubscriber.fail("foo", -1); verifyZeroInteractions(mockClient); }
@Test public void testCommiting() { AsyncHttpClient mockClient = mock(AsyncHttpClient.class); pubscriber.commit("foo", -1); verifyZeroInteractions(mockClient); }
@Test(timeOut = 5000L) public void testException() throws Exception { // This will hit a non-existent url and fail, testing our exceptions. Our connect and retry is low so even if // block the full amount, it's still fast. pubscriber.send(new PubSubMessage("foo", "bar")); PubSubMessage actual = fetchAsync().get(); Assert.assertNotNull(actual); Assert.assertEquals(actual.getId(), "foo"); Assert.assertEquals(actual.getContent(), CANNOT_REACH_DRPC.asJSONClip()); } }
@Test(timeOut = 5000L) public void testReadingNotOkResponse() throws Exception { CompletableFuture<Response> response = getOkFuture(getNotOkResponse(500)); AsyncHttpClient mockClient = mockClientWith(response); pubscriber.setClient(mockClient); pubscriber.send(new PubSubMessage("foo", "bar")); // This is async (but practically still very fast since we mocked the response), so need a timeout. PubSubMessage actual = fetchAsync().get(); Assert.assertNotNull(actual); Assert.assertEquals(actual.getId(), "foo"); Assert.assertEquals(actual.getContent(), CANNOT_REACH_DRPC.asJSONClip()); }
@Override public void send(PubSubMessage message) throws PubSubException { String url = urls.get(); String id = message.getId(); String json = message.asJSON(); log.info("Posting to {} for id {}", url, id); log.debug("Posting to {} with body {}", url, json); client.preparePost(url).setBody(json).execute().toCompletableFuture() .exceptionally(this::handleException) .thenAcceptAsync(createResponseConsumer(id)); }
private void createPubscribers(int amount) { IntStream.range(0, amount).forEach(i -> commonPool.add(new DRPCQueryResultPubscriber(config))); } }
@Test(timeOut = 5000L) public void testReadingOkResponse() throws Exception { PubSubMessage expected = new PubSubMessage("foo", "response"); CompletableFuture<Response> response = getOkFuture(getOkResponse(expected.asJSON())); AsyncHttpClient mockClient = mockClientWith(response); pubscriber.setClient(mockClient); pubscriber.send(new PubSubMessage("foo", "bar")); // This is async (but practically still very fast since we mocked the response), so need a timeout. PubSubMessage actual = fetchAsync().get(); Assert.assertNotNull(actual); Assert.assertEquals(actual.getId(), expected.getId()); Assert.assertEquals(actual.getContent(), expected.getContent()); }
private Consumer<Response> createResponseConsumer(String id) { // Create a closure with id return response -> handleResponse(id, response); }
@BeforeMethod public void setup() { config = new DRPCConfig("src/test/resources/test_drpc_config.yaml"); config.set(DRPCConfig.DRPC_SERVERS, Collections.singletonList("foo.bar.bullet.drpc.com")); pubscriber = new DRPCQueryResultPubscriber(config); }