AsynchronousAgentDispatcher { public void addWriteTask(MonitoringEvent event, DatagramChannel channel, int maxSize) { if (!initialized) { throw new IllegalStateException("Dispatcher is not initialized!"); } tasks.add(new WriteTask(event, channel, maxSize)); } private  AsynchronousAgentDispatcher(); @SdkTestInternalApi  AsynchronousAgentDispatcher(ObjectWriter writer); void addWriteTask(MonitoringEvent event, DatagramChannel channel, int maxSize); synchronized void init(); synchronized void release(); synchronized static AsynchronousAgentDispatcher getInstance();  }
@Test public void testIgnoresIOExceptionFromChannelWrite() throws IOException, InterruptedException { when(channel.write(any(ByteBuffer.class))).thenThrow(new IOException("buffer is full")); dispatcher.addWriteTask(new ApiCallAttemptMonitoringEvent().withApi("foo"), channel, 8192); dispatcher.addWriteTask(new ApiCallAttemptMonitoringEvent().withApi("foo"), channel, 8192); Thread.sleep(100); verify(channel, times(2)).write((any(ByteBuffer.class))); }
@Test public void testDropsEventWhenTooLarge() throws InterruptedException, IOException { ApiCallAttemptMonitoringEvent tooLarge = new ApiCallAttemptMonitoringEvent() .withApi("SomeApi"); dispatcher.addWriteTask(tooLarge, channel, 1); Thread.sleep(100); verify(channel, times(0)).write(any(ByteBuffer.class)); }
@Test public void testWritesEventIfUnderMaxSize() throws InterruptedException, IOException { ApiCallAttemptMonitoringEvent okay = new ApiCallAttemptMonitoringEvent() .withApi("SomeApi"); dispatcher.addWriteTask(okay, channel, 8192); Thread.sleep(100); verify(channel, times(1)).write(any(ByteBuffer.class)); }