OfframpServiceV2 implements OfframpService { @VisibleForTesting String encodeEvent(Event event) { String raw = metrics.record(ENCODE, () -> encoder.encode(event)); if (event instanceof Message) { metrics.markMessage(raw.getBytes().length); } return raw; }  @Override void run(); @Override void close(); @Override void onEvent(String event);  }
@Test public void encodeMessage() throws Exception { Event event = new Message<>(0, 1L, 2, 3L, mapper.createObjectNode()); String raw = ""; doReturn(raw).when(encoder).encode(event); doReturn(raw).when(metrics).record(eq(ENCODE), argThat(new ArgMatcher<Supplier<String>>() {})); String result = underTest.encodeEvent(event); assertThat(result, is(raw)); verify(metrics).markMessage(raw.length()); @SuppressWarnings("unchecked") ArgumentCaptor<Supplier<String>> captor = ArgumentCaptor.forClass(Supplier.class); verify(metrics).record(eq(ENCODE), captor.capture()); assertThat(captor.getValue().get(), is(raw)); }
@Test public void encodeNonMessage() throws Exception { Event event = new Rebalance(singleton(0)); String raw = ""; doReturn(raw).when(encoder).encode(event); doReturn(raw).when(metrics).record(eq(ENCODE), argThat(new ArgMatcher<Supplier<String>>() {})); String result = underTest.encodeEvent(event); assertThat(result, is(raw)); verify(metrics, never()).markMessage(anyLong()); @SuppressWarnings("unchecked") ArgumentCaptor<Supplier<String>> captor = ArgumentCaptor.forClass(Supplier.class); verify(metrics).record(eq(ENCODE), captor.capture()); assertThat(captor.getValue().get(), is(raw)); }