statsd-instrument 2.5.1 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop-https---shopify-github-io-ruby-style-guide-rubocop-yml +1 -1
  3. data/.rubocop.yml +11 -6
  4. data/.yardopts +5 -0
  5. data/CHANGELOG.md +75 -6
  6. data/README.md +54 -46
  7. data/benchmark/datagram-client +41 -0
  8. data/lib/statsd/instrument/assertions.rb +168 -57
  9. data/lib/statsd/instrument/backends/udp_backend.rb +20 -29
  10. data/lib/statsd/instrument/capture_sink.rb +27 -0
  11. data/lib/statsd/instrument/client.rb +313 -0
  12. data/lib/statsd/instrument/datagram.rb +75 -0
  13. data/lib/statsd/instrument/datagram_builder.rb +101 -0
  14. data/lib/statsd/instrument/dogstatsd_datagram_builder.rb +71 -0
  15. data/lib/statsd/instrument/environment.rb +106 -29
  16. data/lib/statsd/instrument/log_sink.rb +24 -0
  17. data/lib/statsd/instrument/null_sink.rb +13 -0
  18. data/lib/statsd/instrument/rubocop/measure_as_dist_argument.rb +39 -0
  19. data/lib/statsd/instrument/rubocop/metaprogramming_positional_arguments.rb +6 -10
  20. data/lib/statsd/instrument/rubocop/metric_prefix_argument.rb +37 -0
  21. data/lib/statsd/instrument/rubocop/metric_return_value.rb +7 -6
  22. data/lib/statsd/instrument/rubocop/metric_value_keyword_argument.rb +11 -20
  23. data/lib/statsd/instrument/rubocop/positional_arguments.rb +13 -13
  24. data/lib/statsd/instrument/rubocop/splat_arguments.rb +8 -14
  25. data/lib/statsd/instrument/rubocop.rb +64 -0
  26. data/lib/statsd/instrument/statsd_datagram_builder.rb +14 -0
  27. data/lib/statsd/instrument/strict.rb +112 -22
  28. data/lib/statsd/instrument/udp_sink.rb +62 -0
  29. data/lib/statsd/instrument/version.rb +1 -1
  30. data/lib/statsd/instrument.rb +191 -100
  31. data/test/assertions_test.rb +139 -176
  32. data/test/capture_sink_test.rb +44 -0
  33. data/test/client_test.rb +164 -0
  34. data/test/compatibility/dogstatsd_datagram_compatibility_test.rb +162 -0
  35. data/test/datagram_builder_test.rb +120 -0
  36. data/test/deprecations_test.rb +56 -10
  37. data/test/dogstatsd_datagram_builder_test.rb +32 -0
  38. data/test/environment_test.rb +73 -7
  39. data/test/log_sink_test.rb +37 -0
  40. data/test/null_sink_test.rb +13 -0
  41. data/test/rubocop/measure_as_dist_argument_test.rb +44 -0
  42. data/test/rubocop/metaprogramming_positional_arguments_test.rb +1 -1
  43. data/test/rubocop/metric_prefix_argument_test.rb +38 -0
  44. data/test/rubocop/metric_return_value_test.rb +1 -1
  45. data/test/rubocop/metric_value_keyword_argument_test.rb +1 -1
  46. data/test/rubocop/positional_arguments_test.rb +1 -1
  47. data/test/rubocop/splat_arguments_test.rb +1 -1
  48. data/test/statsd_datagram_builder_test.rb +22 -0
  49. data/test/statsd_instrumentation_test.rb +0 -24
  50. data/test/statsd_test.rb +0 -23
  51. data/test/test_helper.rb +0 -2
  52. data/test/udp_backend_test.rb +25 -8
  53. data/test/udp_sink_test.rb +85 -0
  54. metadata +38 -2
@@ -10,141 +10,120 @@ class AssertionsTest < Minitest::Test
10
10
  end
11
11
 
12
12
  def test_assert_no_statsd_calls
13
- assert_no_assertion_triggered do
14
- @test_case.assert_no_statsd_calls('counter') do
15
- # noop
16
- end
13
+ @test_case.assert_no_statsd_calls('counter') do
14
+ # noop
17
15
  end
18
16
 
19
- assert_no_assertion_triggered do
20
- @test_case.assert_no_statsd_calls('counter') do
21
- StatsD.increment('other')
22
- end
17
+ @test_case.assert_no_statsd_calls('counter') do
18
+ StatsD.increment('other')
23
19
  end
24
20
 
25
- assert_assertion_triggered("No StatsD calls for metric counter expected.") do
21
+ assertion = assert_raises(Minitest::Assertion) do
26
22
  @test_case.assert_no_statsd_calls('counter') do
27
23
  StatsD.increment('counter')
28
24
  end
29
25
  end
26
+ assert_equal assertion.message, "No StatsD calls for metric counter expected."
30
27
 
31
- assert_assertion_triggered("No StatsD calls for metric other expected.") do
28
+ assertion = assert_raises(Minitest::Assertion) do
32
29
  @test_case.assert_no_statsd_calls do
33
30
  StatsD.increment('other')
34
31
  end
35
32
  end
33
+ assert_equal assertion.message, "No StatsD calls for metric other expected."
36
34
 
37
- assert_assertion_triggered("No StatsD calls for metric other, another expected.") do
35
+ assertion = assert_raises(Minitest::Assertion) do
38
36
  @test_case.assert_no_statsd_calls do
39
37
  StatsD.increment('other')
40
38
  StatsD.increment('another')
41
39
  end
42
40
  end
41
+ assert_equal assertion.message, "No StatsD calls for metric other, another expected."
43
42
  end
44
43
 
45
44
  def test_assert_statsd_call
46
- assert_no_assertion_triggered do
47
- @test_case.assert_statsd_increment('counter') do
48
- StatsD.increment('counter')
49
- end
45
+ @test_case.assert_statsd_increment('counter') do
46
+ StatsD.increment('counter')
50
47
  end
51
48
 
52
- assert_no_assertion_triggered do
53
- @test_case.assert_statsd_increment('counter') do
54
- StatsD.increment('counter')
55
- StatsD.increment('other')
56
- end
49
+ @test_case.assert_statsd_increment('counter') do
50
+ StatsD.increment('counter')
51
+ StatsD.increment('other')
57
52
  end
58
53
 
59
- assert_assertion_triggered do
54
+ assert_raises(Minitest::Assertion) do
60
55
  @test_case.assert_statsd_increment('counter') do
61
56
  StatsD.increment('other')
62
57
  end
63
58
  end
64
59
 
65
- assert_assertion_triggered do
60
+ assert_raises(Minitest::Assertion) do
66
61
  @test_case.assert_statsd_increment('counter') do
67
62
  StatsD.gauge('counter', 42)
68
63
  end
69
64
  end
70
65
 
71
- assert_assertion_triggered do
66
+ assert_raises(Minitest::Assertion) do
72
67
  @test_case.assert_statsd_increment('counter') do
73
68
  StatsD.increment('counter')
74
69
  StatsD.increment('counter')
75
70
  end
76
71
  end
77
72
 
78
- assert_no_assertion_triggered do
79
- @test_case.assert_statsd_increment('counter', times: 2) do
80
- StatsD.increment('counter')
81
- StatsD.increment('counter')
82
- end
73
+ @test_case.assert_statsd_increment('counter', times: 2) do
74
+ StatsD.increment('counter')
75
+ StatsD.increment('counter')
83
76
  end
84
77
 
85
- assert_no_assertion_triggered do
86
- @test_case.assert_statsd_increment('counter', times: 2, tags: ['foo:1']) do
87
- StatsD.increment('counter', tags: { foo: 1 })
88
- StatsD.increment('counter', tags: { foo: 1 })
89
- end
78
+ @test_case.assert_statsd_increment('counter', times: 2, tags: ['foo:1']) do
79
+ StatsD.increment('counter', tags: { foo: 1 })
80
+ StatsD.increment('counter', tags: { foo: 1 })
90
81
  end
91
82
 
92
- assert_assertion_triggered do
83
+ assert_raises(Minitest::Assertion) do
93
84
  @test_case.assert_statsd_increment('counter', times: 2, tags: ['foo:1']) do
94
85
  StatsD.increment('counter', tags: { foo: 1 })
95
86
  StatsD.increment('counter', tags: { foo: 2 })
96
87
  end
97
88
  end
98
89
 
99
- assert_no_assertion_triggered do
100
- @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a', 'b']) do
101
- StatsD.increment('counter', sample_rate: 0.5, tags: ['a', 'b'])
102
- end
90
+ @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a', 'b']) do
91
+ StatsD.increment('counter', sample_rate: 0.5, tags: ['a', 'b'])
103
92
  end
104
93
 
105
- assert_assertion_triggered do
94
+ assert_raises(Minitest::Assertion) do
106
95
  @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a', 'b'], ignore_tags: ['b']) do
107
96
  StatsD.increment('counter', sample_rate: 0.5, tags: ['a'])
108
97
  end
109
98
  end
110
99
 
111
- assert_no_assertion_triggered do
112
- @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a'], ignore_tags: ['b']) do
113
- StatsD.increment('counter', sample_rate: 0.5, tags: ['a', 'b'])
114
- end
100
+ @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a'], ignore_tags: ['b']) do
101
+ StatsD.increment('counter', sample_rate: 0.5, tags: ['a', 'b'])
115
102
  end
116
103
 
117
- assert_no_assertion_triggered do
118
- @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a'], ignore_tags: ['b']) do
119
- StatsD.increment('counter', sample_rate: 0.5, tags: ['a'])
120
- end
104
+ @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a'], ignore_tags: ['b']) do
105
+ StatsD.increment('counter', sample_rate: 0.5, tags: ['a'])
121
106
  end
122
107
 
123
- assert_no_assertion_triggered do
124
- @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1 }, ignore_tags: { b: 2 }) do
125
- StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 2 })
126
- end
108
+ @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1 }, ignore_tags: { b: 2 }) do
109
+ StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 2 })
127
110
  end
128
111
 
129
- assert_no_assertion_triggered do
130
- @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1 }, ignore_tags: { b: 2 }) do
131
- StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 3 })
132
- end
112
+ @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1 }, ignore_tags: { b: 2 }) do
113
+ StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 3 })
133
114
  end
134
115
 
135
- assert_assertion_triggered do
116
+ assert_raises(Minitest::Assertion) do
136
117
  @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1, b: 3 }, ignore_tags: ['b']) do
137
118
  StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 2 })
138
119
  end
139
120
  end
140
121
 
141
- assert_no_assertion_triggered do
142
- @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1 }, ignore_tags: ['b']) do
143
- StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 2 })
144
- end
122
+ @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1 }, ignore_tags: ['b']) do
123
+ StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 2 })
145
124
  end
146
125
 
147
- assert_assertion_triggered do
126
+ assert_raises(Minitest::Assertion) do
148
127
  @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a', 'b']) do
149
128
  StatsD.increment('counter', sample_rate: 0.2, tags: ['c'])
150
129
  end
@@ -152,13 +131,11 @@ class AssertionsTest < Minitest::Test
152
131
  end
153
132
 
154
133
  def test_tags_will_match_subsets
155
- assert_no_assertion_triggered do
156
- @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1 }) do
157
- StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 2 })
158
- end
134
+ @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1 }) do
135
+ StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 2 })
159
136
  end
160
137
 
161
- assert_assertion_triggered do
138
+ assert_raises(Minitest::Assertion) do
162
139
  @test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1, b: 3 }) do
163
140
  StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 2, c: 4 })
164
141
  end
@@ -166,66 +143,58 @@ class AssertionsTest < Minitest::Test
166
143
  end
167
144
 
168
145
  def test_tags_friendly_error
169
- @test_case.assert_statsd_increment('counter', tags: { class: "AnotherJob" }) do
170
- StatsD.increment('counter', tags: { class: "MyJob" })
146
+ assertion = assert_raises(Minitest::Assertion) do
147
+ @test_case.assert_statsd_increment('counter', tags: { class: "AnotherJob" }) do
148
+ StatsD.increment('counter', tags: { class: "MyJob" })
149
+ end
171
150
  end
172
- rescue MiniTest::Assertion => assertion
173
- assert_match(/Captured metrics with the same key/, assertion.message)
174
- assert_match(/MyJob/, assertion.message)
151
+
152
+ assert_includes assertion.message, "Captured metrics with the same key"
153
+ assert_includes assertion.message, "MyJob"
175
154
  end
176
155
 
177
156
  def test_multiple_metrics_are_not_order_dependent
178
- assert_no_assertion_triggered do
179
- foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:1'])
180
- foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
181
- @test_case.assert_statsd_calls([foo_1_metric, foo_2_metric]) do
182
- StatsD.increment('counter', tags: { foo: 1 })
183
- StatsD.increment('counter', tags: { foo: 2 })
184
- end
157
+ foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:1'])
158
+ foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
159
+ @test_case.assert_statsd_calls([foo_1_metric, foo_2_metric]) do
160
+ StatsD.increment('counter', tags: { foo: 1 })
161
+ StatsD.increment('counter', tags: { foo: 2 })
185
162
  end
186
163
 
187
- assert_no_assertion_triggered do
188
- foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:1'])
189
- foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
190
- @test_case.assert_statsd_calls([foo_2_metric, foo_1_metric]) do
191
- StatsD.increment('counter', tags: { foo: 1 })
192
- StatsD.increment('counter', tags: { foo: 2 })
193
- end
164
+ foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:1'])
165
+ foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
166
+ @test_case.assert_statsd_calls([foo_2_metric, foo_1_metric]) do
167
+ StatsD.increment('counter', tags: { foo: 1 })
168
+ StatsD.increment('counter', tags: { foo: 2 })
194
169
  end
195
170
 
196
- assert_no_assertion_triggered do
197
- foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
198
- foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
199
- @test_case.assert_statsd_calls([foo_1_metric, foo_2_metric]) do
200
- StatsD.increment('counter', tags: { foo: 1 })
201
- StatsD.increment('counter', tags: { foo: 1 })
202
- StatsD.increment('counter', tags: { foo: 2 })
203
- end
171
+ foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
172
+ foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
173
+ @test_case.assert_statsd_calls([foo_1_metric, foo_2_metric]) do
174
+ StatsD.increment('counter', tags: { foo: 1 })
175
+ StatsD.increment('counter', tags: { foo: 1 })
176
+ StatsD.increment('counter', tags: { foo: 2 })
204
177
  end
205
178
 
206
- assert_no_assertion_triggered do
207
- foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
208
- foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
209
- @test_case.assert_statsd_calls([foo_2_metric, foo_1_metric]) do
210
- StatsD.increment('counter', tags: { foo: 1 })
211
- StatsD.increment('counter', tags: { foo: 1 })
212
- StatsD.increment('counter', tags: { foo: 2 })
213
- end
179
+ foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
180
+ foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
181
+ @test_case.assert_statsd_calls([foo_2_metric, foo_1_metric]) do
182
+ StatsD.increment('counter', tags: { foo: 1 })
183
+ StatsD.increment('counter', tags: { foo: 1 })
184
+ StatsD.increment('counter', tags: { foo: 2 })
214
185
  end
215
186
 
216
- assert_no_assertion_triggered do
217
- foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
218
- foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
219
- @test_case.assert_statsd_calls([foo_2_metric, foo_1_metric]) do
220
- StatsD.increment('counter', tags: { foo: 1 })
221
- StatsD.increment('counter', tags: { foo: 2 })
222
- StatsD.increment('counter', tags: { foo: 1 })
223
- end
187
+ foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
188
+ foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
189
+ @test_case.assert_statsd_calls([foo_2_metric, foo_1_metric]) do
190
+ StatsD.increment('counter', tags: { foo: 1 })
191
+ StatsD.increment('counter', tags: { foo: 2 })
192
+ StatsD.increment('counter', tags: { foo: 1 })
224
193
  end
225
194
  end
226
195
 
227
196
  def test_assert_multiple_statsd_calls
228
- assert_assertion_triggered do
197
+ assert_raises(Minitest::Assertion) do
229
198
  foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
230
199
  foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
231
200
  @test_case.assert_statsd_calls([foo_1_metric, foo_2_metric]) do
@@ -234,7 +203,7 @@ class AssertionsTest < Minitest::Test
234
203
  end
235
204
  end
236
205
 
237
- assert_assertion_triggered do
206
+ assert_raises(Minitest::Assertion) do
238
207
  foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
239
208
  foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
240
209
  @test_case.assert_statsd_calls([foo_1_metric, foo_2_metric]) do
@@ -245,60 +214,52 @@ class AssertionsTest < Minitest::Test
245
214
  end
246
215
  end
247
216
 
248
- assert_no_assertion_triggered do
249
- foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
250
- foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
251
- @test_case.assert_statsd_calls([foo_1_metric, foo_2_metric]) do
252
- StatsD.increment('counter', tags: { foo: 1 })
253
- StatsD.increment('counter', tags: { foo: 1 })
254
- StatsD.increment('counter', tags: { foo: 2 })
255
- end
217
+ foo_1_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 2, tags: ['foo:1'])
218
+ foo_2_metric = StatsD::Instrument::MetricExpectation.new(type: :c, name: 'counter', times: 1, tags: ['foo:2'])
219
+ @test_case.assert_statsd_calls([foo_1_metric, foo_2_metric]) do
220
+ StatsD.increment('counter', tags: { foo: 1 })
221
+ StatsD.increment('counter', tags: { foo: 1 })
222
+ StatsD.increment('counter', tags: { foo: 2 })
256
223
  end
257
224
  end
258
225
 
259
226
  def test_assert_statsd_call_with_tags
260
- assert_no_assertion_triggered do
261
- @test_case.assert_statsd_increment('counter', tags: ['a:b', 'c:d']) do
262
- StatsD.increment('counter', tags: { a: 'b', c: 'd' })
263
- end
227
+ @test_case.assert_statsd_increment('counter', tags: ['a:b', 'c:d']) do
228
+ StatsD.increment('counter', tags: { a: 'b', c: 'd' })
264
229
  end
265
230
 
266
- assert_no_assertion_triggered do
267
- @test_case.assert_statsd_increment('counter', tags: { a: 'b', c: 'd' }) do
268
- StatsD.increment('counter', tags: ['a:b', 'c:d'])
269
- end
231
+ @test_case.assert_statsd_increment('counter', tags: { a: 'b', c: 'd' }) do
232
+ StatsD.increment('counter', tags: ['a:b', 'c:d'])
270
233
  end
271
234
  end
272
235
 
273
236
  def test_assert_statsd_call_with_wrong_sample_rate_type
274
237
  skip("In Strict mode, the StatsD.increment call will raise") if StatsD::Instrument.strict_mode_enabled?
275
- assert_assertion_triggered "Unexpected sample rate type for metric counter, must be numeric" do
238
+
239
+ assertion = assert_raises(Minitest::Assertion) do
276
240
  @test_case.assert_statsd_increment('counter', tags: ['a', 'b']) do
277
241
  StatsD.increment('counter', sample_rate: 'abc', tags: ['a', 'b'])
278
242
  end
279
243
  end
244
+ assert_equal "Unexpected sample rate type for metric counter, must be numeric", assertion.message
280
245
  end
281
246
 
282
247
  def test_nested_assertions
283
- assert_no_assertion_triggered do
284
- @test_case.assert_statsd_increment('counter1') do
285
- @test_case.assert_statsd_increment('counter2') do
286
- StatsD.increment('counter1')
287
- StatsD.increment('counter2')
288
- end
248
+ @test_case.assert_statsd_increment('counter1') do
249
+ @test_case.assert_statsd_increment('counter2') do
250
+ StatsD.increment('counter1')
251
+ StatsD.increment('counter2')
289
252
  end
290
253
  end
291
254
 
292
- assert_no_assertion_triggered do
293
- @test_case.assert_statsd_increment('counter1') do
294
- StatsD.increment('counter1')
295
- @test_case.assert_statsd_increment('counter2') do
296
- StatsD.increment('counter2')
297
- end
255
+ @test_case.assert_statsd_increment('counter1') do
256
+ StatsD.increment('counter1')
257
+ @test_case.assert_statsd_increment('counter2') do
258
+ StatsD.increment('counter2')
298
259
  end
299
260
  end
300
261
 
301
- assert_assertion_triggered do
262
+ assert_raises(Minitest::Assertion) do
302
263
  @test_case.assert_statsd_increment('counter1') do
303
264
  @test_case.assert_statsd_increment('counter2') do
304
265
  StatsD.increment('counter2')
@@ -306,7 +267,7 @@ class AssertionsTest < Minitest::Test
306
267
  end
307
268
  end
308
269
 
309
- assert_assertion_triggered do
270
+ assert_raises(Minitest::Assertion) do
310
271
  @test_case.assert_statsd_increment('counter1') do
311
272
  @test_case.assert_statsd_increment('counter2') do
312
273
  StatsD.increment('counter1')
@@ -316,62 +277,64 @@ class AssertionsTest < Minitest::Test
316
277
  end
317
278
  end
318
279
 
319
- def test_assertion_with_exceptions
320
- assert_no_assertion_triggered do
280
+ def test_assertion_block_with_expected_exceptions
281
+ @test_case.assert_statsd_increment('expected_happened') do
321
282
  @test_case.assert_raises(RuntimeError) do
322
- @test_case.assert_statsd_increment('counter') do
323
- StatsD.increment('counter')
324
- raise "foo"
283
+ begin
284
+ raise "expected"
285
+ rescue
286
+ StatsD.increment('expected_happened')
287
+ raise
325
288
  end
326
289
  end
327
290
  end
328
291
 
329
- assert_no_assertion_triggered do
292
+ assertion = assert_raises(Minitest::Assertion) do
330
293
  @test_case.assert_statsd_increment('counter') do
331
294
  @test_case.assert_raises(RuntimeError) do
332
- StatsD.increment('counter')
333
- raise "foo"
295
+ raise "expected"
334
296
  end
335
297
  end
336
298
  end
299
+ assert_includes assertion.message, "No StatsD calls for metric counter of type c were made"
300
+ end
337
301
 
338
- assert_assertion_triggered do
302
+ def test_assertion_block_with_unexpected_exceptions
303
+ assertion = assert_raises(Minitest::Assertion) do
339
304
  @test_case.assert_statsd_increment('counter') do
340
- @test_case.assert_raises(RuntimeError) do
341
- raise "foo"
342
- end
305
+ StatsD.increment('counter')
306
+ raise "unexpected"
343
307
  end
344
308
  end
309
+ assert_includes assertion.message, "An exception occurred in the block provided to the StatsD assertion"
345
310
 
346
- assert_assertion_triggered do
311
+ assertion = assert_raises(Minitest::Assertion) do
347
312
  @test_case.assert_raises(RuntimeError) do
348
313
  @test_case.assert_statsd_increment('counter') do
349
- raise "foo"
314
+ StatsD.increment('counter')
315
+ raise "unexpected"
350
316
  end
351
317
  end
352
318
  end
353
- end
354
-
355
- private
319
+ assert_includes assertion.message, "An exception occurred in the block provided to the StatsD assertion"
356
320
 
357
- def assert_no_assertion_triggered(&block)
358
- block.call
359
- rescue MiniTest::Assertion => assertion
360
- flunk("No assertion trigger expected, but one was triggered with message #{assertion.message}.")
361
- else
362
- pass
321
+ assertion = assert_raises(Minitest::Assertion) do
322
+ @test_case.assert_raises(RuntimeError) do
323
+ @test_case.assert_no_statsd_calls do
324
+ raise "unexpected"
325
+ end
326
+ end
327
+ end
328
+ assert_includes assertion.message, "An exception occurred in the block provided to the StatsD assertion"
363
329
  end
364
330
 
365
- def assert_assertion_triggered(message = nil, &block)
366
- block.call
367
- rescue MiniTest::Assertion => assertion
368
- if message
369
- assert_equal(message, assertion.message, "Assertion triggered, but message was not what was expected.")
370
- else
371
- pass
372
- end
373
- assertion
374
- else
375
- flunk("No assertion was triggered, but one was expected.")
331
+ def test_assertion_block_with_other_assertion_failures
332
+ # If another assertion failure happens inside the block, that failrue should have priority
333
+ assertion = assert_raises(Minitest::Assertion) do
334
+ @test_case.assert_statsd_increment('counter') do
335
+ @test_case.flunk('other assertion failure')
336
+ end
337
+ end
338
+ assert_equal "other assertion failure", assertion.message
376
339
  end
377
340
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ require 'statsd/instrument/client'
6
+
7
+ class CaptureSinktest < Minitest::Test
8
+ def test_capture_sink_captures_datagram_instances
9
+ capture_sink = StatsD::Instrument::CaptureSink.new(parent: [])
10
+ capture_sink << 'foo:1|c'
11
+
12
+ assert_equal 1, capture_sink.datagrams.length
13
+ assert_kind_of StatsD::Instrument::Datagram, capture_sink.datagrams.first
14
+ assert_equal 'foo:1|c', capture_sink.datagrams.first.source
15
+ end
16
+
17
+ def test_capture_sink_sends_datagrams_to_parent
18
+ parent = []
19
+ capture_sink = StatsD::Instrument::CaptureSink.new(parent: parent)
20
+ capture_sink << 'foo:1|c' << 'bar:1|c'
21
+
22
+ assert_equal ['foo:1|c', 'bar:1|c'], parent
23
+ end
24
+
25
+ def test_nesting_capture_sink_instances
26
+ null_sink = StatsD::Instrument::NullSink.new
27
+ outer_capture_sink = StatsD::Instrument::CaptureSink.new(parent: null_sink)
28
+ inner_capture_sink = StatsD::Instrument::CaptureSink.new(parent: outer_capture_sink)
29
+
30
+ outer_capture_sink << 'foo:1|c'
31
+ inner_capture_sink << 'bar:1|c'
32
+
33
+ assert_equal ['foo:1|c', 'bar:1|c'], outer_capture_sink.datagrams.map(&:source)
34
+ assert_equal ['bar:1|c'], inner_capture_sink.datagrams.map(&:source)
35
+ end
36
+
37
+ def test_using_a_different_datagram_class
38
+ sink = StatsD::Instrument::CaptureSink.new(parent: [], datagram_class: String)
39
+ sink << 'foo:1|c'
40
+
41
+ assert sink.datagrams.all? { |datagram| datagram.is_a?(String) }
42
+ assert_equal ['foo:1|c'], sink.datagrams
43
+ end
44
+ end