MetadataCountInterceptor extends MetadataMetricsInterceptorImpl { @Override public void init() throws CoreException { if (isEmpty(getMetadataKey())) throw new CoreException("metadata-key is empty/null"); super.init(); } MetadataCountInterceptor();  MetadataCountInterceptor(String key); @Override void init(); @Override synchronized void workflowStart(AdaptrisMessage inputMsg); @Override synchronized void workflowEnd(AdaptrisMessage inputMsg, final AdaptrisMessage outputMsg); String getMetadataKey(); void setMetadataKey(String key);  }
@Test public void testInit_NoMetadataKey() throws Exception { metricsInterceptor = new MetadataCountInterceptor(); try { LifecycleHelper.init(metricsInterceptor); fail(); } catch (CoreException expected) { } }
@Test public void testInterceptor() throws Exception { LifecycleHelper.init(metricsInterceptor); LifecycleHelper.start(metricsInterceptor); AdaptrisMessage message = createMessage(COUNTER_1); assertEquals(0, metricsInterceptor.getStats().size()); submitMessage(message); assertEquals(1, metricsInterceptor.getStats().size()); assertEquals(1, ((MetadataStatistic) metricsInterceptor.getStats().get(0)).getValue(COUNTER_1)); assertEquals(0, ((MetadataStatistic) metricsInterceptor.getStats().get(0)).getValue(COUNTER_2)); }
@Test public void testInterceptor_NoMetadataValue() throws Exception { LifecycleHelper.init(metricsInterceptor); LifecycleHelper.start(metricsInterceptor); assertEquals(0, metricsInterceptor.getStats().size()); submitMessage(AdaptrisMessageFactory.getDefaultInstance().newMessage()); assertEquals(1, metricsInterceptor.getStats().size()); assertEquals(0, ((MetadataStatistic) metricsInterceptor.getStats().get(0)).getValue(COUNTER_1)); assertEquals(0, ((MetadataStatistic) metricsInterceptor.getStats().get(0)).getValue(COUNTER_2)); }
@Test public void testNoProduceBeforeNewTimeSlice() throws Exception { ProducingStatisticManager producingStatisticManager = new ProducingStatisticManager(); producingStatisticManager.setMarshaller(mockMarshaller); producingStatisticManager.setProducer(mockStandaloneProducer); metricsInterceptor.setStatisticManager(producingStatisticManager); LifecycleHelper.init(metricsInterceptor); LifecycleHelper.start(metricsInterceptor); AdaptrisMessage message = DefaultMessageFactory.getDefaultInstance().newMessage(); metricsInterceptor.setTimesliceDuration(new TimeInterval(-1L, TimeUnit.SECONDS)); assertEquals(0, metricsInterceptor.getStats().size()); submitMessage(message); assertEquals(1, metricsInterceptor.getStats().size()); verify(mockMarshaller, times(0)).marshal(any()); verify(mockStandaloneProducer, times(0)).produce(any()); }
@Test public void testProduceAfterNewTimeSlice() throws Exception { ProducingStatisticManager producingStatisticManager = new ProducingStatisticManager(); producingStatisticManager.setMarshaller(mockMarshaller); producingStatisticManager.setProducer(mockStandaloneProducer); metricsInterceptor.setStatisticManager(producingStatisticManager); LifecycleHelper.init(metricsInterceptor); LifecycleHelper.start(metricsInterceptor); AdaptrisMessage message = DefaultMessageFactory.getDefaultInstance().newMessage(); metricsInterceptor.setTimesliceDuration(new TimeInterval(-1L, TimeUnit.SECONDS)); assertEquals(0, metricsInterceptor.getStats().size()); submitMessage(message); assertEquals(1, metricsInterceptor.getStats().size()); submitMessage(message); verify(mockMarshaller).marshal(any()); verify(mockStandaloneProducer).produce(any()); }
@Test public void testCreatesNewTimeSliceAfterTimeDelay() throws Exception { LifecycleHelper.init(metricsInterceptor); LifecycleHelper.start(metricsInterceptor); metricsInterceptor.setTimesliceDuration(new TimeInterval(-1L, TimeUnit.SECONDS)); AdaptrisMessage message = createMessage(COUNTER_1); assertEquals(0, metricsInterceptor.getStats().size()); submitMessage(message); assertEquals(1, metricsInterceptor.getStats().size()); assertEquals(1, ((MetadataStatistic) metricsInterceptor.getStats().get(0)).getValue(COUNTER_1)); assertEquals(0, ((MetadataStatistic) metricsInterceptor.getStats().get(0)).getValue(COUNTER_2)); metricsInterceptor.setTimesliceDuration(new TimeInterval(1L, TimeUnit.SECONDS)); submitMessage(message); submitMessage(message); assertEquals(2, metricsInterceptor.getStats().size()); assertEquals(1, ((MetadataStatistic) metricsInterceptor.getStats().get(0)).getValue(COUNTER_1)); assertEquals(0, ((MetadataStatistic) metricsInterceptor.getStats().get(0)).getValue(COUNTER_2)); assertEquals(2, ((MetadataStatistic) metricsInterceptor.getStats().get(1)).getValue(COUNTER_1)); assertEquals(0, ((MetadataStatistic) metricsInterceptor.getStats().get(1)).getValue(COUNTER_2)); }
@Test public void testDoesNotCreateMoreHistoryThanSpecified() throws Exception { LifecycleHelper.init(metricsInterceptor); LifecycleHelper.start(metricsInterceptor); metricsInterceptor.setTimesliceDuration(new TimeInterval(-1L, TimeUnit.SECONDS)); AdaptrisMessage message = createMessage(COUNTER_1); assertEquals(0, metricsInterceptor.getStats().size()); submitMessage(message); assertEquals(1, metricsInterceptor.getStats().size()); submitMessage(message); assertEquals(2, metricsInterceptor.getStats().size()); submitMessage(message); assertEquals(2, metricsInterceptor.getStats().size()); }
@Test public void testMultiThreadedSingleMetricsInterceptorInstance() throws Exception { LifecycleHelper.init(metricsInterceptor); LifecycleHelper.start(metricsInterceptor); new MetricsInserterThread(20, COUNTER_1).run(); new MetricsInserterThread(20, COUNTER_2).run(); new MetricsInserterThread(20, COUNTER_1).run(); new MetricsInserterThread(20, COUNTER_2).run(); new MetricsInserterThread(20, COUNTER_1).run(); Thread.sleep(200); assertEquals(60, ((MetadataStatistic) metricsInterceptor.getStats().get(0)).getValue(COUNTER_1)); assertEquals(40, ((MetadataStatistic) metricsInterceptor.getStats().get(0)).getValue(COUNTER_2)); }