BufferedSender implements Sender { @Override public void send(Payload payload) { try { queue.add(payload); } catch (Exception e) { notifyError(payload, new SenderException(e)); } } BufferedSender(Builder builder);   BufferedSender(Builder builder, ScheduledExecutorService executorService); Queue<Payload> queue(); Sender sender(); @Override void send(Payload payload); @Override void addListener(SenderListener listener); @Override List<SenderListener> getListeners(); @Override void close(); @Override void close(boolean wait);  }
@Test public void shouldEnqueuePayloads() { Payload payload = mock(Payload.class); sut.send(payload); verify(queue).add(payload); }
@Test public void sendTaskShouldSendPayloadInBatch() { Payload payload1 = mock(Payload.class); Payload payload2 = mock(Payload.class); when(queue.poll()).thenReturn(payload1, payload2); SendTask sut = new SendTask(2, queue, sender); sut.run(); verify(sender).send(payload1); verify(sender).send(payload2); }
@Test public void sendTaskShouldNotThrowErrorIfSenderFails() { Payload payload1 = mock(Payload.class); Payload payload2 = mock(Payload.class); when(queue.poll()).thenReturn(payload1, payload2); doThrow(new RuntimeException("Error sending")).when(sender).send(payload1); SendTask sut = new SendTask(2, queue, sender); sut.run(); verify(sender).send(payload1); verify(sender).send(payload2); }