2626import org .jruby .RubyHash ;
2727import org .jruby .runtime .ThreadContext ;
2828import org .junit .Test ;
29+ import org .logstash .Event ;
2930import org .logstash .RubyTestBase ;
31+ import org .logstash .RubyUtil ;
3032import org .logstash .execution .QueueBatch ;
3133import org .logstash .instrument .metrics .AbstractNamespacedMetricExt ;
34+ import org .logstash .instrument .metrics .MetricKeys ;
3235import org .logstash .instrument .metrics .MockNamespacedMetric ;
36+ import org .logstash .instrument .metrics .histogram .HistogramMetric ;
37+ import org .logstash .instrument .metrics .histogram .HistogramSnapshot ;
3338
3439import static org .hamcrest .CoreMatchers .is ;
3540import static org .hamcrest .MatcherAssert .assertThat ;
41+ import static org .junit .Assert .assertEquals ;
3642
3743/**
3844 * Tests for {@link JrubyMemoryReadClientExt}.
@@ -58,4 +64,24 @@ public void testInflightBatchesTracking() throws InterruptedException, IOExcepti
5864 client .closeBatch (batch );
5965 assertThat (client .rubyGetInflightBatches (context ).size (), is (0 ));
6066 }
67+
68+ @ Test
69+ public void givenNonEmptyQueueAndEnabledBatchSizeMetricThenHistogramContainsData () throws InterruptedException {
70+ final JrubyEventExtLibrary .RubyEvent testEvent = JrubyEventExtLibrary .RubyEvent .newRubyEvent (RubyUtil .RUBY , new Event ());
71+ final BlockingQueue <JrubyEventExtLibrary .RubyEvent > queue = new ArrayBlockingQueue <>(10 );
72+ queue .add (testEvent );
73+
74+ final JrubyMemoryReadClientExt client = JrubyMemoryReadClientExt .create (queue , 5 , 50 );
75+
76+ AbstractNamespacedMetricExt metric = MockNamespacedMetric .create ();
77+ client .setPipelineMetric (metric );
78+
79+ final QueueBatch batch = client .readBatch ();
80+ assertEquals (1 , batch .filteredSize ());
81+
82+ HistogramMetric histogram = HistogramMetric .fromRubyBase (metric , MetricKeys .BATCH_SIZE_KEY );
83+ HistogramSnapshot metricSnapshot = histogram .getValue ();
84+ assertEquals (1.0 , metricSnapshot .get75Percentile (), 0.0001 );
85+ assertEquals (1.0 , metricSnapshot .get90Percentile (), 0.0001 );
86+ }
6187}
0 commit comments