-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathNonBlockingStatsDClientTest.java
More file actions
243 lines (193 loc) · 8.57 KB
/
NonBlockingStatsDClientTest.java
File metadata and controls
243 lines (193 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.timgroup.statsd;
import static java.lang.Long.valueOf;
import static junit.framework.Assert.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.startsWith;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Test;
public final class NonBlockingStatsDClientTest {
private static final int STATSD_SERVER_PORT = 17254;
private final NonBlockingStatsDClient client = new NonBlockingStatsDClient("my.prefix", "localhost", STATSD_SERVER_PORT);
private final DummyStatsDServer server = new DummyStatsDServer(STATSD_SERVER_PORT);
@After
public void stop() throws Exception {
client.stop();
server.stop();
}
@Test(timeout=5000L) public void
sends_counter_value_to_statsd() throws Exception {
client.count("mycount", Long.MAX_VALUE);
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.mycount:9223372036854775807|c"));
}
@Test(timeout=5000L) public void
sends_counter_value_with_rate_to_statsd() throws Exception {
client.count("mycount", Long.MAX_VALUE, 0.999999);
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.mycount:9223372036854775807|c|@0.999999"));
}
@Test(timeout=5000L) public void
sends_counter_increment_to_statsd() throws Exception {
client.incrementCounter("myinc");
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.myinc:1|c"));
}
@Test(timeout=5000L) public void
sends_counter_decrement_to_statsd() throws Exception {
client.decrementCounter("mydec");
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.mydec:-1|c"));
}
@Test(timeout=5000L) public void
sends_gauge_to_statsd() throws Exception {
client.recordGaugeValue("mygauge", Long.MAX_VALUE);
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.mygauge:9223372036854775807|g"));
}
@Test(timeout=5000L) public void
sends_fractional_gauge_to_statsd() throws Exception {
client.recordGaugeValue("mygauge", 423.123456789d);
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.mygauge:423.123456789|g"));
}
@Test(timeout=5000L) public void
sends_large_fractional_gauge_to_statsd() throws Exception {
client.recordGaugeValue("mygauge", 423423423.9d);
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.mygauge:423423423.9|g"));
}
@Test(timeout=5000L) public void
sends_zero_gauge_to_statsd() throws Exception {
client.recordGaugeValue("mygauge", 0L);
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.mygauge:0|g"));
}
@Test(timeout=5000L) public void
sends_negagive_gauge_to_statsd_by_resetting_to_zero_first() throws Exception {
client.recordGaugeValue("mygauge", -423L);
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.mygauge:0|g\nmy.prefix.mygauge:-423|g"));
}
@Test(timeout=5000L) public void
sends_gauge_positive_delta_to_statsd() throws Exception {
client.recordGaugeDelta("mygauge", 423L);
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.mygauge:+423|g"));
}
@Test(timeout=5000L) public void
sends_gauge_negative_delta_to_statsd() throws Exception {
client.recordGaugeDelta("mygauge", -423L);
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.mygauge:-423|g"));
}
@Test(timeout=5000L) public void
sends_gauge_zero_delta_to_statsd() throws Exception {
client.recordGaugeDelta("mygauge", 0L);
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.mygauge:+0|g"));
}
@Test(timeout=5000L) public void
sends_set_to_statsd() throws Exception {
client.recordSetEvent("myset", "test");
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.myset:test|s"));
}
@Test(timeout=5000L) public void
sends_timer_to_statsd() throws Exception {
client.recordExecutionTime("mytime", 123L);
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.mytime:123|ms"));
}
@Test(timeout=5000L) public void
sends_timer_with_rate_to_statsd() throws Exception {
client.recordExecutionTime("mytime", 123L, 0.999999);
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.mytime:123|ms|@0.999999"));
}
@Test(timeout=5000L) public void
sends_timer_to_statsd_based_on_specified_start_time_to_now() throws Exception {
final long startTime = System.currentTimeMillis() - 1000L;
final long beforeCompletionTime = System.currentTimeMillis();
client.recordExecutionTimeToNow("mytime", startTime);
final long afterCompletionTime = System.currentTimeMillis();
server.waitForMessage();
long maxExpectedValue = afterCompletionTime - startTime;
long minExpectedValue = beforeCompletionTime - startTime;
final String messageReceived = server.messagesReceived().get(0);
final Matcher resultMatcher = Pattern.compile(".*:(\\d+)\\|ms").matcher(messageReceived);
assertTrue(messageReceived, resultMatcher.matches());
assertThat(valueOf(resultMatcher.group(1)), Matchers.greaterThanOrEqualTo(minExpectedValue));
assertThat(valueOf(resultMatcher.group(1)), Matchers.lessThanOrEqualTo(maxExpectedValue));
}
@Test(timeout=5000L) public void
sends_timer_of_zero_to_statsd_based_on_specified_start_time_in_the_future() throws Exception {
client.recordExecutionTimeToNow("mytime", System.currentTimeMillis() + 100000L);
server.waitForMessage();
assertThat(server.messagesReceived(), contains("my.prefix.mytime:0|ms"));
}
@Test(timeout=5000L) public void
allows_empty_prefix() {
final NonBlockingStatsDClient emptyPrefixClient = new NonBlockingStatsDClient(" ", "localhost", STATSD_SERVER_PORT);
try {
emptyPrefixClient.count("mycount", 24L);
server.waitForMessage();
} finally {
emptyPrefixClient.stop();
}
assertThat(server.messagesReceived(), contains(startsWith("mycount:")));
}
@Test(timeout=5000L) public void
allows_null_prefix() {
final NonBlockingStatsDClient nullPrefixClient = new NonBlockingStatsDClient(null, "localhost", STATSD_SERVER_PORT);
try {
nullPrefixClient.count("mycount", 24L);
server.waitForMessage();
} finally {
nullPrefixClient.stop();
}
assertThat(server.messagesReceived(), contains(startsWith("mycount:")));
}
private static final class DummyStatsDServer {
private final List<String> messagesReceived = new ArrayList<String>();
private final DatagramSocket server;
public DummyStatsDServer(int port) {
try {
server = new DatagramSocket(port);
} catch (SocketException e) {
throw new IllegalStateException(e);
}
new Thread(new Runnable() {
@Override public void run() {
try {
final DatagramPacket packet = new DatagramPacket(new byte[256], 256);
server.receive(packet);
messagesReceived.add(new String(packet.getData(), Charset.forName("UTF-8")).trim());
} catch (Exception e) { }
}
}).start();
}
public void stop() {
server.close();
}
public void waitForMessage() {
while (messagesReceived.isEmpty()) {
try {
Thread.sleep(50L);
} catch (InterruptedException e) {}
}
}
public List<String> messagesReceived() {
return new ArrayList<String>(messagesReceived);
}
}
}