17
17
18
18
import com .rabbitmq .client .Channel ;
19
19
import com .rabbitmq .client .Connection ;
20
+ import com .rabbitmq .client .MetricsCollector ;
21
+ import com .rabbitmq .client .impl .AbstractMetricsCollector ;
22
+ import com .rabbitmq .client .impl .MicrometerMetricsCollector ;
20
23
import com .rabbitmq .client .impl .StandardMetricsCollector ;
24
+ import io .micrometer .core .instrument .simple .SimpleMeterRegistry ;
21
25
import org .junit .Test ;
26
+ import org .junit .runner .RunWith ;
27
+ import org .junit .runners .Parameterized ;
22
28
23
29
import static org .hamcrest .Matchers .*;
24
30
import static org .junit .Assert .*;
27
33
/**
28
34
*
29
35
*/
30
- public class StandardMetricsCollectorTest {
36
+ @ RunWith (Parameterized .class )
37
+ public class MetricsCollectorTest {
38
+
39
+ @ Parameterized .Parameters
40
+ public static Object [] data () {
41
+ // need to resort to a factory, as this method is called only once
42
+ // if creating the collector instance, it's reused across the test methods
43
+ // and this doesn't work (it cannot be reset)
44
+ return new Object [] { new StandardMetricsCollectorFactory (), new MicrometerMetricsCollectorFactory () };
45
+ }
46
+
47
+ @ Parameterized .Parameter
48
+ public MetricsCollectorFactory factory ;
31
49
32
50
@ Test
33
51
public void basicGetAndAck () {
34
- StandardMetricsCollector metrics = new StandardMetricsCollector ();
52
+ AbstractMetricsCollector metrics = factory . create ();
35
53
Connection connection = mock (Connection .class );
36
54
when (connection .getId ()).thenReturn ("connection-1" );
37
55
Channel channel = mock (Channel .class );
@@ -49,20 +67,20 @@ public void basicGetAndAck() {
49
67
metrics .consumedMessage (channel , 6 , false );
50
68
51
69
metrics .basicAck (channel , 6 , false );
52
- assertThat (metrics . getAcknowledgedMessages (). getCount ( ), is (1L ));
70
+ assertThat (acknowledgedMessages ( metrics ), is (1L ));
53
71
54
72
metrics .basicAck (channel , 3 , true );
55
- assertThat (metrics . getAcknowledgedMessages (). getCount ( ), is (1L +2L ));
73
+ assertThat (acknowledgedMessages ( metrics ), is (1L +2L ));
56
74
57
75
metrics .basicAck (channel , 6 , true );
58
- assertThat (metrics . getAcknowledgedMessages (). getCount ( ), is (1L +2L +1L ));
76
+ assertThat (acknowledgedMessages ( metrics ), is (1L +2L +1L ));
59
77
60
78
metrics .basicAck (channel , 10 , true );
61
- assertThat (metrics . getAcknowledgedMessages (). getCount ( ), is (1L +2L +1L ));
79
+ assertThat (acknowledgedMessages ( metrics ), is (1L +2L +1L ));
62
80
}
63
81
64
82
@ Test public void basicConsumeAndAck () {
65
- StandardMetricsCollector metrics = new StandardMetricsCollector ();
83
+ AbstractMetricsCollector metrics = factory . create ();
66
84
Connection connection = mock (Connection .class );
67
85
when (connection .getId ()).thenReturn ("connection-1" );
68
86
Channel channel = mock (Channel .class );
@@ -78,8 +96,8 @@ public void basicGetAndAck() {
78
96
metrics .basicConsume (channel , consumerTagWithManualAck , false );
79
97
80
98
metrics .consumedMessage (channel , 1 , consumerTagWithAutoAck );
81
- assertThat (metrics . getConsumedMessages (). getCount ( ), is (1L ));
82
- assertThat (metrics . getAcknowledgedMessages (). getCount ( ), is (0L ));
99
+ assertThat (consumedMessages ( metrics ), is (1L ));
100
+ assertThat (acknowledgedMessages ( metrics ), is (0L ));
83
101
84
102
metrics .consumedMessage (channel , 2 , consumerTagWithManualAck );
85
103
metrics .consumedMessage (channel , 3 , consumerTagWithManualAck );
@@ -88,21 +106,21 @@ public void basicGetAndAck() {
88
106
metrics .consumedMessage (channel , 6 , consumerTagWithManualAck );
89
107
90
108
metrics .basicAck (channel , 6 , false );
91
- assertThat (metrics . getAcknowledgedMessages (). getCount ( ), is (1L ));
109
+ assertThat (acknowledgedMessages ( metrics ), is (1L ));
92
110
93
111
metrics .basicAck (channel , 3 , true );
94
- assertThat (metrics . getAcknowledgedMessages (). getCount ( ), is (1L +2L ));
112
+ assertThat (acknowledgedMessages ( metrics ), is (1L +2L ));
95
113
96
114
metrics .basicAck (channel , 6 , true );
97
- assertThat (metrics . getAcknowledgedMessages (). getCount ( ), is (1L +2L +1L ));
115
+ assertThat (acknowledgedMessages ( metrics ), is (1L +2L +1L ));
98
116
99
117
metrics .basicAck (channel , 10 , true );
100
- assertThat (metrics . getAcknowledgedMessages (). getCount ( ), is (1L +2L +1L ));
118
+ assertThat (acknowledgedMessages ( metrics ), is (1L +2L +1L ));
101
119
102
120
}
103
121
104
122
@ Test public void cleanStaleState () {
105
- StandardMetricsCollector metrics = new StandardMetricsCollector ();
123
+ AbstractMetricsCollector metrics = factory . create ();
106
124
Connection openConnection = mock (Connection .class );
107
125
when (openConnection .getId ()).thenReturn ("connection-1" );
108
126
when (openConnection .isOpen ()).thenReturn (true );
@@ -132,13 +150,63 @@ public void basicGetAndAck() {
132
150
metrics .newChannel (closedChannel );
133
151
metrics .newChannel (openChannelInClosedConnection );
134
152
135
- assertThat (metrics . getConnections (). getCount ( ), is (2L ));
136
- assertThat (metrics . getChannels (). getCount ( ), is (2L +1L ));
153
+ assertThat (connections ( metrics ), is (2L ));
154
+ assertThat (channels ( metrics ), is (2L +1L ));
137
155
138
156
metrics .cleanStaleState ();
139
157
140
- assertThat (metrics .getConnections ().getCount (), is (1L ));
141
- assertThat (metrics .getChannels ().getCount (), is (1L ));
158
+ assertThat (connections (metrics ), is (1L ));
159
+ assertThat (channels (metrics ), is (1L ));
160
+ }
161
+
162
+ long consumedMessages (MetricsCollector metrics ) {
163
+ if (metrics instanceof StandardMetricsCollector ) {
164
+ return ((StandardMetricsCollector ) metrics ).getConsumedMessages ().getCount ();
165
+ } else {
166
+ return (long ) ((MicrometerMetricsCollector ) metrics ).getConsumedMessages ().count ();
167
+ }
168
+ }
169
+
170
+ long acknowledgedMessages (MetricsCollector metrics ) {
171
+ if (metrics instanceof StandardMetricsCollector ) {
172
+ return ((StandardMetricsCollector ) metrics ).getAcknowledgedMessages ().getCount ();
173
+ } else {
174
+ return (long ) ((MicrometerMetricsCollector ) metrics ).getAcknowledgedMessages ().count ();
175
+ }
176
+ }
177
+
178
+ long connections (MetricsCollector metrics ) {
179
+ if (metrics instanceof StandardMetricsCollector ) {
180
+ return ((StandardMetricsCollector ) metrics ).getConnections ().getCount ();
181
+ } else {
182
+ return ((MicrometerMetricsCollector ) metrics ).getConnections ().get ();
183
+ }
184
+ }
185
+
186
+ long channels (MetricsCollector metrics ) {
187
+ if (metrics instanceof StandardMetricsCollector ) {
188
+ return ((StandardMetricsCollector ) metrics ).getChannels ().getCount ();
189
+ } else {
190
+ return ((MicrometerMetricsCollector ) metrics ).getChannels ().get ();
191
+ }
192
+ }
193
+
194
+ interface MetricsCollectorFactory {
195
+ AbstractMetricsCollector create ();
196
+ }
197
+
198
+ static class StandardMetricsCollectorFactory implements MetricsCollectorFactory {
199
+ @ Override
200
+ public AbstractMetricsCollector create () {
201
+ return new StandardMetricsCollector ();
202
+ }
203
+ }
204
+
205
+ static class MicrometerMetricsCollectorFactory implements MetricsCollectorFactory {
206
+ @ Override
207
+ public AbstractMetricsCollector create () {
208
+ return new MicrometerMetricsCollector (new SimpleMeterRegistry ());
209
+ }
142
210
}
143
211
144
212
}
0 commit comments