statsd-instrument 1.7.2 → 2.0.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.
@@ -47,13 +47,16 @@ end
47
47
 
48
48
  ActiveMerchant::Base.extend StatsD::Instrument
49
49
 
50
- class StatsDInstrumentationTest < Test::Unit::TestCase
50
+ class StatsDInstrumentationTest < Minitest::Test
51
+ include StatsD::Instrument::Assertions
52
+
51
53
  def test_statsd_count_if
52
54
  ActiveMerchant::Gateway.statsd_count_if :ssl_post, 'ActiveMerchant.Gateway.if'
53
55
 
54
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.if').once
55
- ActiveMerchant::Gateway.new.purchase(true)
56
- ActiveMerchant::Gateway.new.purchase(false)
56
+ assert_statsd_increment('ActiveMerchant.Gateway.if') do
57
+ ActiveMerchant::Gateway.new.purchase(true)
58
+ ActiveMerchant::Gateway.new.purchase(false)
59
+ end
57
60
 
58
61
  ActiveMerchant::Gateway.statsd_remove_count_if :ssl_post, 'ActiveMerchant.Gateway.if'
59
62
  end
@@ -63,9 +66,10 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
63
66
  result == 'true'
64
67
  end
65
68
 
66
- StatsD.expects(:collect).with(:c, 'ActiveMerchant.Base.post_with_block', 1, {}).once
67
- assert_equal 'true', ActiveMerchant::Base.new.post_with_block { 'true' }
68
- assert_equal 'false', ActiveMerchant::Base.new.post_with_block { 'false' }
69
+ assert_statsd_increment('ActiveMerchant.Base.post_with_block') do
70
+ assert_equal 'true', ActiveMerchant::Base.new.post_with_block { 'true' }
71
+ assert_equal 'false', ActiveMerchant::Base.new.post_with_block { 'false' }
72
+ end
69
73
 
70
74
  ActiveMerchant::Base.statsd_remove_count_if :post_with_block, 'ActiveMerchant.Base.post_with_block'
71
75
  end
@@ -75,9 +79,10 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
75
79
  result[:success]
76
80
  end
77
81
 
78
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.block').once
79
- ActiveMerchant::UniqueGateway.new.purchase(true)
80
- ActiveMerchant::UniqueGateway.new.purchase(false)
82
+ assert_statsd_increment('ActiveMerchant.Gateway.block', times: 1) do
83
+ ActiveMerchant::UniqueGateway.new.purchase(true)
84
+ ActiveMerchant::UniqueGateway.new.purchase(false)
85
+ end
81
86
 
82
87
  ActiveMerchant::UniqueGateway.statsd_remove_count_if :ssl_post, 'ActiveMerchant.Gateway.block'
83
88
  end
@@ -85,11 +90,15 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
85
90
  def test_statsd_count_success
86
91
  ActiveMerchant::Gateway.statsd_count_success :ssl_post, 'ActiveMerchant.Gateway', 0.5
87
92
 
88
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.success', 1, 0.5).once
89
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.failure', 1, 0.5).once
93
+ assert_statsd_increment('ActiveMerchant.Gateway.success', sample_rate: 0.5, times: 1) do
94
+ ActiveMerchant::Gateway.new.purchase(true)
95
+ ActiveMerchant::Gateway.new.purchase(false)
96
+ end
90
97
 
91
- ActiveMerchant::Gateway.new.purchase(true)
92
- ActiveMerchant::Gateway.new.purchase(false)
98
+ assert_statsd_increment('ActiveMerchant.Gateway.failure', sample_rate: 0.5, times: 1) do
99
+ ActiveMerchant::Gateway.new.purchase(false)
100
+ ActiveMerchant::Gateway.new.purchase(true)
101
+ end
93
102
 
94
103
  ActiveMerchant::Gateway.statsd_remove_count_success :ssl_post, 'ActiveMerchant.Gateway'
95
104
  end
@@ -99,11 +108,15 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
99
108
  result == 'successful'
100
109
  end
101
110
 
102
- StatsD.expects(:collect).with(:c, 'ActiveMerchant.Base.post_with_block.success', 1, {}).once
103
- StatsD.expects(:collect).with(:c, 'ActiveMerchant.Base.post_with_block.failure', 1, {}).once
104
-
105
- assert_equal 'successful', ActiveMerchant::Base.new.post_with_block { 'successful' }
106
- assert_equal 'not so successful', ActiveMerchant::Base.new.post_with_block { 'not so successful' }
111
+ assert_statsd_increment('ActiveMerchant.Base.post_with_block.success', times: 1) do
112
+ assert_equal 'successful', ActiveMerchant::Base.new.post_with_block { 'successful' }
113
+ assert_equal 'not so successful', ActiveMerchant::Base.new.post_with_block { 'not so successful' }
114
+ end
115
+
116
+ assert_statsd_increment('ActiveMerchant.Base.post_with_block.failure', times: 1) do
117
+ assert_equal 'successful', ActiveMerchant::Base.new.post_with_block { 'successful' }
118
+ assert_equal 'not so successful', ActiveMerchant::Base.new.post_with_block { 'not so successful' }
119
+ end
107
120
 
108
121
  ActiveMerchant::Base.statsd_remove_count_success :post_with_block, 'ActiveMerchant.Base.post_with_block'
109
122
  end
@@ -113,11 +126,13 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
113
126
  result[:success]
114
127
  end
115
128
 
116
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.success', 1)
117
- ActiveMerchant::UniqueGateway.new.purchase(true)
129
+ assert_statsd_increment('ActiveMerchant.Gateway.success') do
130
+ ActiveMerchant::UniqueGateway.new.purchase(true)
131
+ end
118
132
 
119
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.failure', 1)
120
- ActiveMerchant::UniqueGateway.new.purchase(false)
133
+ assert_statsd_increment('ActiveMerchant.Gateway.failure') do
134
+ ActiveMerchant::UniqueGateway.new.purchase(false)
135
+ end
121
136
 
122
137
  ActiveMerchant::UniqueGateway.statsd_remove_count_success :ssl_post, 'ActiveMerchant.Gateway'
123
138
  end
@@ -125,8 +140,9 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
125
140
  def test_statsd_count
126
141
  ActiveMerchant::Gateway.statsd_count :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
127
142
 
128
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.ssl_post', 1)
129
- ActiveMerchant::Gateway.new.purchase(true)
143
+ assert_statsd_increment('ActiveMerchant.Gateway.ssl_post') do
144
+ ActiveMerchant::Gateway.new.purchase(true)
145
+ end
130
146
 
131
147
  ActiveMerchant::Gateway.statsd_remove_count :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
132
148
  end
@@ -135,8 +151,9 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
135
151
  metric_namer = lambda { |object, args| object.class.to_s.downcase + ".insert." + args.first.to_s }
136
152
  ActiveMerchant::Gateway.statsd_count(:ssl_post, metric_namer)
137
153
 
138
- StatsD.expects(:increment).with('gatewaysubclass.insert.true', 1)
139
- GatewaySubClass.new.purchase(true)
154
+ assert_statsd_increment('gatewaysubclass.insert.true') do
155
+ GatewaySubClass.new.purchase(true)
156
+ end
140
157
 
141
158
  ActiveMerchant::Gateway.statsd_remove_count(:ssl_post, metric_namer)
142
159
  end
@@ -144,35 +161,39 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
144
161
  def test_statsd_count_with_method_receiving_block
145
162
  ActiveMerchant::Base.statsd_count :post_with_block, 'ActiveMerchant.Base.post_with_block'
146
163
 
147
- StatsD.expects(:collect).with(:c, 'ActiveMerchant.Base.post_with_block', 1, {})
148
- assert_equal 'block called', ActiveMerchant::Base.new.post_with_block { 'block called' }
164
+ assert_statsd_increment('ActiveMerchant.Base.post_with_block') do
165
+ assert_equal 'block called', ActiveMerchant::Base.new.post_with_block { 'block called' }
166
+ end
149
167
 
150
168
  ActiveMerchant::Base.statsd_remove_count :post_with_block, 'ActiveMerchant.Base.post_with_block'
151
169
  end
152
170
 
153
- def test_statsd_measure_with_nested_modules
154
- ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant::Gateway.ssl_post'
171
+ def test_statsd_measure
172
+ ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post', sample_rate: 0.3
155
173
 
156
- StatsD.expects(:measure).with('ActiveMerchant.Gateway.ssl_post', nil)
157
- ActiveMerchant::UniqueGateway.new.purchase(true)
174
+ assert_statsd_measure('ActiveMerchant.Gateway.ssl_post', sample_rate: 0.3) do
175
+ ActiveMerchant::UniqueGateway.new.purchase(true)
176
+ end
158
177
 
159
- ActiveMerchant::UniqueGateway.statsd_remove_measure :ssl_post, 'ActiveMerchant::Gateway.ssl_post'
178
+ ActiveMerchant::UniqueGateway.statsd_remove_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
160
179
  end
161
180
 
162
- def test_statsd_measure
163
- ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post', 0.3
181
+ def test_statsd_measure_uses_normalized_metric_name
182
+ ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant::Gateway.ssl_post'
164
183
 
165
- StatsD.expects(:measure).with('ActiveMerchant.Gateway.ssl_post', nil, 0.3)
166
- ActiveMerchant::UniqueGateway.new.purchase(true)
184
+ assert_statsd_measure('ActiveMerchant.Gateway.ssl_post') do
185
+ ActiveMerchant::UniqueGateway.new.purchase(true)
186
+ end
167
187
 
168
- ActiveMerchant::UniqueGateway.statsd_remove_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
188
+ ActiveMerchant::UniqueGateway.statsd_remove_measure :ssl_post, 'ActiveMerchant::Gateway.ssl_post'
169
189
  end
170
190
 
171
191
  def test_statsd_measure_with_method_receiving_block
172
192
  ActiveMerchant::Base.statsd_measure :post_with_block, 'ActiveMerchant.Base.post_with_block'
173
193
 
174
- StatsD.expects(:collect).with(:ms, 'ActiveMerchant.Base.post_with_block', is_a(Float), {})
175
- assert_equal 'block called', ActiveMerchant::Base.new.post_with_block { 'block called' }
194
+ assert_statsd_measure('ActiveMerchant.Base.post_with_block') do
195
+ assert_equal 'block called', ActiveMerchant::Base.new.post_with_block { 'block called' }
196
+ end
176
197
 
177
198
  ActiveMerchant::Base.statsd_remove_measure :post_with_block, 'ActiveMerchant.Base.post_with_block'
178
199
  end
@@ -181,18 +202,20 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
181
202
  ActiveMerchant::Gateway.singleton_class.extend StatsD::Instrument
182
203
  ActiveMerchant::Gateway.singleton_class.statsd_count :sync, 'ActiveMerchant.Gateway.sync'
183
204
 
184
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.sync', 1)
185
- ActiveMerchant::Gateway.sync
205
+ assert_statsd_increment('ActiveMerchant.Gateway.sync') do
206
+ ActiveMerchant::Gateway.sync
207
+ end
186
208
 
187
209
  ActiveMerchant::Gateway.singleton_class.statsd_remove_count :sync, 'ActiveMerchant.Gateway.sync'
188
210
  end
189
211
 
190
212
  def test_statsd_count_with_tags
191
213
  ActiveMerchant::Gateway.singleton_class.extend StatsD::Instrument
192
- ActiveMerchant::Gateway.singleton_class.statsd_count :sync, 'ActiveMerchant.Gateway.sync', :tags => ['t']
214
+ ActiveMerchant::Gateway.singleton_class.statsd_count :sync, 'ActiveMerchant.Gateway.sync', tags: { key: 'value' }
193
215
 
194
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.sync', 1, :tags => ['t'])
195
- ActiveMerchant::Gateway.sync
216
+ assert_statsd_increment('ActiveMerchant.Gateway.sync', tags: ['key:value']) do
217
+ ActiveMerchant::Gateway.sync
218
+ end
196
219
 
197
220
  ActiveMerchant::Gateway.singleton_class.statsd_remove_count :sync, 'ActiveMerchant.Gateway.sync'
198
221
  end
data/test/statsd_test.rb CHANGED
@@ -1,256 +1,104 @@
1
1
  require 'test_helper'
2
2
 
3
- class StatsDTest < Test::Unit::TestCase
3
+ class StatsDTest < Minitest::Test
4
+ include StatsD::Instrument::Assertions
4
5
 
5
- def setup
6
- StatsD.stubs(:rand).returns(0.0)
7
-
8
- UDPSocket.stubs(:new).returns(@socket = mock('socket'))
9
- @socket.stubs(:connect)
10
- @socket.stubs(:send)
11
- StatsD.invalidate_socket
12
-
13
- StatsD.stubs(:logger).returns(@logger = mock('logger'))
14
- @logger.stubs(:info)
15
- @logger.stubs(:error)
6
+ def test_statsd_passed_collections_to_backend
7
+ StatsD.backend.expects(:collect_metric).with(instance_of(StatsD::Instrument::Metric))
8
+ StatsD.increment('test')
16
9
  end
17
10
 
18
11
  def test_statsd_measure_with_explicit_value
19
- StatsD.expects(:collect).with(:ms, 'values.foobar', 42, {})
20
- StatsD.measure('values.foobar', 42)
12
+ result = nil
13
+ metric = capture_statsd_call { result = StatsD.measure('values.foobar', 42) }
14
+ assert_equal metric, result
15
+ assert_equal 'values.foobar', metric.name
16
+ assert_equal 42, metric.value
17
+ assert_equal :ms, metric.type
21
18
  end
22
19
 
23
20
  def test_statsd_measure_with_explicit_value_and_sample_rate
24
- StatsD.expects(:collect).with(:ms, 'values.foobar', 42, :sample_rate => 0.1)
25
- StatsD.measure('values.foobar', 42, :sample_rate => 0.1)
21
+ metric = capture_statsd_call { StatsD.measure('values.foobar', 42, :sample_rate => 0.1) }
22
+ assert_equal 0.1, metric.sample_rate
26
23
  end
27
24
 
28
- def test_statsd_measure_with_benchmarked_value
29
- Benchmark.stubs(:realtime).returns(1.12)
30
- StatsD.expects(:collect).with(:ms, 'values.foobar', 1120.0, {})
31
- StatsD.measure('values.foobar') do
32
- #noop
25
+ def test_statsd_measure_with_benchmarked_duration
26
+ StatsD::Instrument.stubs(:duration).returns(1.12)
27
+ metric = capture_statsd_call do
28
+ StatsD.measure('values.foobar') { 'foo' }
33
29
  end
30
+ assert_equal 1120.0, metric.value
34
31
  end
35
32
 
36
- def test_statsd_measure_with_benchmarked_value_and_options
37
- Benchmark.stubs(:realtime).returns(1.12)
38
- StatsD.expects(:collect).with(:ms, 'values.foobar', 1120.0, :sample_rate => 1.0)
39
- StatsD.measure('values.foobar', :sample_rate => 1.0) do
40
- #noop
41
- end
33
+ def test_statsd_measure_returns_return_value_of_block
34
+ return_value = StatsD.measure('values.foobar') { 'sarah' }
35
+ assert_equal 'sarah', return_value
36
+ end
37
+
38
+ def test_statsd_increment
39
+ result = nil
40
+ metric = capture_statsd_call { result = StatsD.increment('values.foobar', 3) }
41
+ assert_equal metric, result
42
+ assert_equal :c, metric.type
43
+ assert_equal 'values.foobar', metric.name
44
+ assert_equal 3, metric.value
42
45
  end
43
46
 
44
47
  def test_statsd_increment_with_hash_argument
45
- StatsD.expects(:collect).with(:c, 'values.foobar', 1, :tags => ['test'])
46
- StatsD.increment('values.foobar', :tags => ['test'])
48
+ metric = capture_statsd_call { StatsD.increment('values.foobar', :tags => ['test']) }
49
+ assert_equal StatsD.default_sample_rate, metric.sample_rate
50
+ assert_equal ['test'], metric.tags
51
+ assert_equal 1, metric.value
47
52
  end
48
53
 
49
54
  def test_statsd_increment_with_multiple_arguments
50
- StatsD.expects(:collect).with(:c, 'values.foobar', 12, :sample_rate => nil, :tags => ['test'])
51
- StatsD.increment('values.foobar', 12, nil, ['test'])
55
+ metric = capture_statsd_call { StatsD.increment('values.foobar', 12, nil, ['test']) }
56
+ assert_equal StatsD.default_sample_rate, metric.sample_rate
57
+ assert_equal ['test'], metric.tags
58
+ assert_equal 12, metric.value
52
59
  end
53
60
 
54
61
  def test_statsd_gauge
55
- StatsD.expects(:collect).with(:g, 'values.foobar', 12, {})
56
- StatsD.gauge('values.foobar', 12)
62
+ result = nil
63
+ metric = capture_statsd_call { result = StatsD.gauge('values.foobar', 12) }
64
+ assert_equal metric, result
65
+ assert_equal :g, metric.type
66
+ assert_equal 'values.foobar', metric.name
67
+ assert_equal 12, metric.value
57
68
  end
58
69
 
59
70
  def test_statsd_set
60
- StatsD.expects(:collect).with(:s, 'values.foobar', 12, {})
61
- StatsD.set('values.foobar', 12)
62
- end
63
-
64
- def test_statsd_histogram_on_datadog
65
- StatsD.stubs(:implementation).returns(:datadog)
66
- StatsD.expects(:collect).with(:h, 'values.hg', 12.33, :sample_rate => 0.2, :tags => ['tag_123', 'key-name:value123'])
67
- StatsD.histogram('values.hg', 12.33, :sample_rate => 0.2, :tags => ['tag_123', 'key-name:value123'])
68
- end
69
-
70
- def test_raise_when_using_histograms_and_not_on_datadog
71
- StatsD.stubs(:implementation).returns(:other)
72
- assert_raises(NotImplementedError) { StatsD.histogram('foohg', 3.33) }
73
- end
74
-
75
- def test_collect_respects_enabled
76
- StatsD.stubs(:enabled).returns(false)
77
- StatsD.expects(:write_packet).never
78
- StatsD.increment('counter')
79
- end
80
-
81
- def test_collect_respects_sampling_rate
82
- StatsD.expects(:write_packet).once
83
-
84
- StatsD.stubs(:rand).returns(0.6)
85
- StatsD.increment('counter', 1, 0.5)
86
-
87
- StatsD.stubs(:rand).returns(0.4)
88
- StatsD.increment('counter', 1, 0.5)
89
- end
90
-
91
- def test_support_counter_syntax
92
- StatsD.expects(:write_packet).with('counter:1|c')
93
- StatsD.increment('counter')
94
-
95
- StatsD.expects(:write_packet).with('counter:10|c|@0.5')
96
- StatsD.increment('counter', 10, 0.5)
97
- end
98
-
99
- def test_supports_gauge_syntax
100
- StatsD.expects(:write_packet).with('fooy:1.23|g')
101
- StatsD.gauge('fooy', 1.23)
102
-
103
- StatsD.expects(:write_packet).with('fooy:42|g|@0.01')
104
- StatsD.gauge('fooy', 42, 0.01)
105
- end
106
-
107
- def test_supports_set_syntax
108
- StatsD.expects(:write_packet).with('unique:10.0.0.10|s')
109
- StatsD.set('unique', '10.0.0.10')
110
-
111
- StatsD.expects(:write_packet).with('unique:10.0.0.10|s|@0.01')
112
- StatsD.set('unique', '10.0.0.10', 0.01)
113
- end
114
-
115
- def test_support_timing_syntax
116
- StatsD.expects(:write_packet).with('duration:1.23|ms')
117
- StatsD.measure('duration', 1.23)
118
-
119
- StatsD.expects(:write_packet).with('duration:0.42|ms|@0.01')
120
- StatsD.measure('duration', 0.42, 0.01)
121
- end
122
-
123
- def test_histogram_syntax_on_datadog
124
- StatsD.stubs(:implementation).returns(:datadog)
125
-
126
- StatsD.expects(:write_packet).with('fooh:42.4|h')
127
- StatsD.histogram('fooh', 42.4)
128
- end
129
-
130
- def test_support_tags_syntax_on_datadog
131
- StatsD.stubs(:implementation).returns(:datadog)
132
-
133
- StatsD.expects(:write_packet).with("fooc:3|c|#topic:foo,bar")
134
- StatsD.increment('fooc', 3, 1.0, ['topic:foo', 'bar'])
135
- end
136
-
137
- def test_raise_when_using_tags_and_not_on_datadog
138
- StatsD.stubs(:implementation).returns(:other)
139
- assert_raises(ArgumentError) { StatsD.increment('fooc', 3, 1.0, ['nonempty']) }
140
- end
141
-
142
- def test_rewrite_shitty_tags
143
- StatsD.stubs(:implementation).returns(:datadog)
144
-
145
- assert_equal ['igno_red'], StatsD.send(:clean_tags, ['igno,red'])
146
- assert_equal ['igno_red'], StatsD.send(:clean_tags, ['igno red'])
147
- assert_equal ['test:test_test'], StatsD.send(:clean_tags, ['test:test:test'])
148
-
149
- assert_equal ['tag:value'], StatsD.send(:clean_tags, { :tag => 'value' })
150
- assert_equal Set['tag:value', 'tag2:value2'], Set.new(StatsD.send(:clean_tags, { :tag => 'value', :tag2 => 'value2' }))
151
-
152
- StatsD.expects(:write_packet).with("fooc:3|c|#topic:foo_foo,bar_")
153
- StatsD.increment('fooc', 3, 1.0, ['topic:foo : foo', 'bar '])
154
-
155
- StatsD.expects(:write_packet).with("foot:1|c|#foo:bar")
156
- StatsD.increment('foot', :tags => { :foo => 'bar' })
157
- end
158
-
159
- def test_supports_key_value_syntax_on_statsite
160
- StatsD.stubs(:implementation).returns(:statsite)
161
-
162
- StatsD.expects(:write_packet).with("fooy:42|kv\n")
163
- StatsD.key_value('fooy', 42)
164
- end
165
-
166
- def test_supports_key_value_with_timestamp_on_statsite
167
- StatsD.stubs(:implementation).returns(:statsite)
168
-
169
- StatsD.expects(:write_packet).with("fooy:42|kv|@123456\n")
170
- StatsD.key_value('fooy', 42, 123456)
171
- end
172
-
173
- def test_raise_when_using_key_value_and_not_on_statsite
174
- StatsD.stubs(:implementation).returns(:other)
175
- assert_raises(NotImplementedError) { StatsD.key_value('fookv', 3.33) }
176
- end
177
-
178
- def test_support_key_prefix
179
- StatsD.expects(:write_packet).with('prefix.counter:1|c').once
180
- StatsD.expects(:write_packet).with('counter:1|c').once
181
-
182
- StatsD.stubs(:prefix).returns('prefix')
183
- StatsD.increment('counter')
184
- StatsD.stubs(:prefix).returns(nil)
185
- StatsD.increment('counter')
186
- end
187
-
188
- def test_development_mode_uses_logger
189
- StatsD.stubs(:mode).returns(:development)
190
-
191
- @logger.expects(:info).with(regexp_matches(/\A\[StatsD\] /))
192
- StatsD.increment('counter')
193
- end
194
-
195
- def test_production_mode_uses_udp_socket
196
- StatsD.stubs(:mode).returns(:production)
197
- StatsD.server = "localhost:9815"
198
-
199
- @socket.expects(:connect).with('localhost', 9815).once
200
- @socket.expects(:send).with(is_a(String), 0).twice
201
- StatsD.increment('counter')
202
- StatsD.increment('counter')
203
- end
204
-
205
- def test_changing_host_or_port_should_create_new_socket
206
- @socket.expects(:connect).with('localhost', 1234).once
207
- @socket.expects(:connect).with('localhost', 2345).once
208
- @socket.expects(:connect).with('127.0.0.1', 2345).once
209
-
210
- StatsD.server = "localhost:1234"
211
- StatsD.send(:socket)
212
-
213
- StatsD.port = 2345
214
- StatsD.send(:socket)
215
-
216
- StatsD.host = '127.0.0.1'
217
- StatsD.send(:socket)
218
- end
219
-
220
- def test_socket_error_should_not_raise_but_log
221
- StatsD.stubs(:mode).returns(:production)
222
- @socket.stubs(:connect).raises(SocketError)
223
-
224
- @logger.expects(:error).with(instance_of(SocketError))
225
- StatsD.measure('values.foobar', 42)
226
- end
227
-
228
- def test_system_call_error_should_not_raise_but_log
229
- StatsD.stubs(:mode).returns(:production)
230
- @socket.stubs(:send).raises(Errno::ETIMEDOUT)
231
-
232
- @logger.expects(:error).with(instance_of(Errno::ETIMEDOUT))
233
- StatsD.measure('values.foobar', 42)
234
- end
235
-
236
- def test_io_error_should_not_raise_but_log
237
- StatsD.stubs(:mode).returns(:production)
238
- @socket.stubs(:send).raises(IOError)
239
-
240
- @logger.expects(:error).with(instance_of(IOError))
241
- StatsD.measure('values.foobar', 42)
242
- end
243
-
244
- def test_live_local_udp_socket
245
- UDPSocket.unstub(:new)
246
-
247
- StatsD.stubs(:mode).returns(:production)
248
- StatsD.server = "localhost:31798"
249
-
250
- server = UDPSocket.new
251
- server.bind('localhost', 31798)
252
-
253
- StatsD.increment('counter')
254
- assert_equal "counter:1|c", server.recvfrom(100).first
71
+ result = nil
72
+ metric = capture_statsd_call { result = StatsD.set('values.foobar', 'unique_identifier') }
73
+ assert_equal metric, result
74
+ assert_equal :s, metric.type
75
+ assert_equal 'values.foobar', metric.name
76
+ assert_equal 'unique_identifier', metric.value
77
+ end
78
+
79
+ def test_statsd_histogram
80
+ result = nil
81
+ metric = capture_statsd_call { result = StatsD.histogram('values.foobar', 42) }
82
+ assert_equal metric, result
83
+ assert_equal :h, metric.type
84
+ assert_equal 'values.foobar', metric.name
85
+ assert_equal 42, metric.value
86
+ end
87
+
88
+ def test_statsd_key_value
89
+ result = nil
90
+ metric = capture_statsd_call { result = StatsD.key_value('values.foobar', 42) }
91
+ assert_equal metric, result
92
+ assert_equal :kv, metric.type
93
+ assert_equal 'values.foobar', metric.name
94
+ assert_equal 42, metric.value
95
+ end
96
+
97
+ protected
98
+
99
+ def capture_statsd_call(&block)
100
+ metrics = capture_statsd_calls(&block)
101
+ assert_equal 1, metrics.length
102
+ metrics.first
255
103
  end
256
104
  end
data/test/test_helper.rb CHANGED
@@ -1,8 +1,10 @@
1
- require 'rubygems'
2
- require 'statsd-instrument'
3
- require 'test/unit'
1
+ ENV['ENV'] = 'test'
2
+
3
+ require 'minitest/autorun'
4
+ require 'minitest/pride'
4
5
  require 'mocha/setup'
5
6
  require 'set'
6
7
  require 'logger'
8
+ require 'statsd-instrument'
7
9
 
8
10
  StatsD.logger = Logger.new('/dev/null')