Skip to content

Commit a199631

Browse files
committed
Created a test mocking class for namespaced metric to be used to create metrics inside, for example, the queue reader client
1 parent d180fb3 commit a199631

File tree

3 files changed

+119
-5
lines changed

3 files changed

+119
-5
lines changed

logstash-core/src/test/java/org/logstash/ext/JrubyMemoryReadClientExtTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.junit.Test;
2929
import org.logstash.RubyTestBase;
3030
import org.logstash.execution.QueueBatch;
31+
import org.logstash.instrument.metrics.AbstractNamespacedMetricExt;
32+
import org.logstash.instrument.metrics.MockNamespacedMetric;
3133

3234
import static org.hamcrest.CoreMatchers.is;
3335
import static org.hamcrest.MatcherAssert.assertThat;
@@ -40,11 +42,14 @@ public final class JrubyMemoryReadClientExtTest extends RubyTestBase {
4042
@Test
4143
@SuppressWarnings("deprecation")
4244
public void testInflightBatchesTracking() throws InterruptedException, IOException {
43-
final BlockingQueue<JrubyEventExtLibrary.RubyEvent> queue =
44-
new ArrayBlockingQueue<>(10);
45-
final JrubyMemoryReadClientExt client =
46-
JrubyMemoryReadClientExt.create(queue, 5, 50);
45+
final BlockingQueue<JrubyEventExtLibrary.RubyEvent> queue = new ArrayBlockingQueue<>(10);
46+
final JrubyMemoryReadClientExt client = JrubyMemoryReadClientExt.create(queue, 5, 50);
47+
4748
final ThreadContext context = client.getRuntime().getCurrentContext();
49+
50+
AbstractNamespacedMetricExt metric = MockNamespacedMetric.create();
51+
client.setPipelineMetric(metric);
52+
4853
final QueueBatch batch = client.readBatch();
4954
final RubyHash inflight = client.rubyGetInflightBatches(context);
5055
assertThat(inflight.size(), is(1));

logstash-core/src/test/java/org/logstash/instrument/metrics/MetricTypeTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class MetricTypeTest {
3939
* just duplicates the code, but should cause a developer to think twice if they are changing the public contract.
4040
*/
4141
@Test
42-
public void ensurePassivity(){
42+
public void ensurePassivity() {
4343
Map<MetricType, String> nameMap = new HashMap<>(EnumSet.allOf(MetricType.class).size());
4444
nameMap.put(MetricType.COUNTER_LONG, "counter/long");
4545
nameMap.put(MetricType.COUNTER_DECIMAL, "counter/decimal");
@@ -51,6 +51,7 @@ public void ensurePassivity(){
5151
nameMap.put(MetricType.GAUGE_UNKNOWN, "gauge/unknown");
5252
nameMap.put(MetricType.GAUGE_RUBYHASH, "gauge/rubyhash");
5353
nameMap.put(MetricType.GAUGE_RUBYTIMESTAMP, "gauge/rubytimestamp");
54+
nameMap.put(MetricType.HISTOGRAM_LONG, "histogram/long");
5455
nameMap.put(MetricType.FLOW_RATE, "flow/rate");
5556

5657
//ensure we are testing all of the enumerations
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package org.logstash.instrument.metrics;
2+
3+
import org.jruby.Ruby;
4+
import org.jruby.RubyArray;
5+
import org.jruby.RubyClass;
6+
import org.jruby.RubySymbol;
7+
import org.jruby.runtime.Block;
8+
import org.jruby.runtime.ThreadContext;
9+
import org.jruby.runtime.builtin.IRubyObject;
10+
import org.logstash.RubyUtil;
11+
import org.logstash.instrument.metrics.counter.LongCounter;
12+
import org.logstash.instrument.metrics.histogram.HistogramMetric;
13+
import org.logstash.instrument.metrics.timer.TimerMetric;
14+
15+
import java.util.Objects;
16+
17+
/**
18+
* Trivial implementation of AbstractNamespacedMetricExt where each abstract creation
19+
* metric is implemented by instantiating a newly fresh metric object.
20+
* */
21+
@SuppressWarnings({"rawtypes", "serializable"})
22+
public class MockNamespacedMetric extends AbstractNamespacedMetricExt {
23+
24+
private static final long serialVersionUID = -6507123659910450215L;
25+
26+
public static MockNamespacedMetric create() {
27+
return new MockNamespacedMetric(RubyUtil.RUBY, RubyUtil.NAMESPACED_METRIC_CLASS);
28+
}
29+
30+
MockNamespacedMetric(final Ruby runtime, final RubyClass metaClass) {
31+
super(runtime, metaClass);
32+
}
33+
34+
@Override
35+
protected IRubyObject getGauge(ThreadContext context, IRubyObject key, IRubyObject value) {
36+
return null;
37+
}
38+
39+
@Override
40+
protected RubyArray getNamespaceName(ThreadContext context) {
41+
return null;
42+
}
43+
44+
@Override
45+
protected IRubyObject getCounter(ThreadContext context, IRubyObject key) {
46+
Objects.requireNonNull(key);
47+
requireRubySymbol(key, "key");
48+
return RubyUtil.toRubyObject(new LongCounter(key.asJavaString()));
49+
}
50+
51+
@Override
52+
protected IRubyObject getTimer(ThreadContext context, IRubyObject key) {
53+
Objects.requireNonNull(key);
54+
requireRubySymbol(key, "key");
55+
// return RubyUtil.toRubyObject(TimerMetric.create("test_timer"));
56+
return RubyUtil.toRubyObject(TimerMetric.create(key.asJavaString()));
57+
}
58+
59+
@Override
60+
protected IRubyObject getHistogram(ThreadContext context, IRubyObject key) {
61+
Objects.requireNonNull(key);
62+
requireRubySymbol(key, "key");
63+
64+
// HistogramMetric metric = new HistogramMetric("test_batch_metric");
65+
return RubyUtil.toRubyObject(new HistogramMetric(key.asJavaString()));
66+
}
67+
68+
@Override
69+
protected IRubyObject doTime(ThreadContext context, IRubyObject key, Block block) {
70+
return null;
71+
}
72+
73+
@Override
74+
protected IRubyObject doReportTime(ThreadContext context, IRubyObject key, IRubyObject duration) {
75+
return null;
76+
}
77+
78+
@Override
79+
protected IRubyObject doIncrement(ThreadContext context, IRubyObject[] args) {
80+
return null;
81+
}
82+
83+
@Override
84+
protected IRubyObject doDecrement(ThreadContext context, IRubyObject[] args) {
85+
return null;
86+
}
87+
88+
@Override
89+
public AbstractMetricExt getMetric() {
90+
return NullMetricExt.create();
91+
}
92+
93+
@Override
94+
protected AbstractNamespacedMetricExt createNamespaced(ThreadContext context, IRubyObject name) {
95+
return null;
96+
}
97+
98+
@Override
99+
protected IRubyObject getCollector(ThreadContext context) {
100+
return null;
101+
}
102+
103+
private static void requireRubySymbol(IRubyObject value, String paramName) {
104+
if (!(value instanceof RubySymbol)) {
105+
throw new IllegalArgumentException(paramName + " must be a RubySymbol instead was: " + value.getClass());
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)