This repository was archived by the owner on Jun 21, 2023. It is now read-only.
forked from reinh/statsd
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathstatsd_spec.rb
More file actions
245 lines (200 loc) · 6.96 KB
/
statsd_spec.rb
File metadata and controls
245 lines (200 loc) · 6.96 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
244
245
require 'helper'
describe Statsd do
before do
@statsd = Statsd.new('localhost', 1234)
class << @statsd
public :sampled # we need to test this
attr_reader :host, :port # we also need to test this
def socket; @socket ||= FakeUDPSocket.new end
end
end
after { @statsd.socket.clear }
describe "#initialize" do
it "should set the host and port" do
@statsd.host.must_equal 'localhost'
@statsd.port.must_equal 1234
end
it "should default the port to 8125" do
Statsd.new('localhost').instance_variable_get('@port').must_equal 8125
end
end
describe "#increment" do
it "should format the message according to the statsd spec" do
@statsd.increment('foobar')
@statsd.socket.recv.must_equal ['foobar:1|c']
end
describe "with a sample rate" do
before { class << @statsd; def rand; 0; end; end } # ensure delivery
it "should format the message according to the statsd spec" do
@statsd.increment('foobar', 0.5)
@statsd.socket.recv.must_equal ['foobar:1|c|@0.5']
end
end
end
describe "#decrement" do
it "should format the message according to the statsd spec" do
@statsd.decrement('foobar')
@statsd.socket.recv.must_equal ['foobar:-1|c']
end
describe "with a sample rate" do
before { class << @statsd; def rand; 0; end; end } # ensure delivery
it "should format the message according to the statsd spec" do
@statsd.decrement('foobar', 0.5)
@statsd.socket.recv.must_equal ['foobar:-1|c|@0.5']
end
end
end
describe "#timing" do
it "should format the message according to the statsd spec" do
@statsd.timing('foobar', 500)
@statsd.socket.recv.must_equal ['foobar:500|ms']
end
describe "with a sample rate" do
before { class << @statsd; def rand; 0; end; end } # ensure delivery
it "should format the message according to the statsd spec" do
@statsd.timing('foobar', 500, 0.5)
@statsd.socket.recv.must_equal ['foobar:500|ms|@0.5']
end
end
end
describe "#time" do
it "should format the message according to the statsd spec" do
@statsd.time('foobar') { sleep(0.001); 'test' }
@statsd.socket.recv.must_equal ['foobar:1|ms']
end
it "should return the result of the block" do
result = @statsd.time('foobar') { sleep(0.001); 'test' }
result.must_equal 'test'
end
describe "with a sample rate" do
before { class << @statsd; def rand; 0; end; end } # ensure delivery
it "should format the message according to the statsd spec" do
result = @statsd.time('foobar', 0.5) { sleep(0.001); 'test' }
@statsd.socket.recv.must_equal ['foobar:1|ms|@0.5']
end
end
end
describe "#sampled" do
describe "when the sample rate is 1" do
it "should yield" do
@statsd.sampled(1) { :yielded }.must_equal :yielded
end
end
describe "when the sample rate is greater than a random value [0,1]" do
before { class << @statsd; def rand; 0; end; end } # ensure delivery
it "should yield" do
@statsd.sampled(0.5) { :yielded }.must_equal :yielded
end
end
describe "when the sample rate is less than a random value [0,1]" do
before { class << @statsd; def rand; 1; end; end } # ensure no delivery
it "should not yield" do
@statsd.sampled(0.5) { :yielded }.must_equal nil
end
end
describe "when the sample rate is equal to a random value [0,1]" do
before { class << @statsd; def rand; 0.5; end; end } # ensure delivery
it "should yield" do
@statsd.sampled(0.5) { :yielded }.must_equal :yielded
end
end
end
describe "with namespace" do
before { @statsd.namespace = 'service' }
it "should add namespace to increment" do
@statsd.increment('foobar')
@statsd.socket.recv.must_equal ['service.foobar:1|c']
end
it "should add namespace to decrement" do
@statsd.decrement('foobar')
@statsd.socket.recv.must_equal ['service.foobar:-1|c']
end
it "should add namespace to timing" do
@statsd.timing('foobar', 500)
@statsd.socket.recv.must_equal ['service.foobar:500|ms']
end
end
describe "with logging" do
require 'stringio'
before { Statsd.logger = Logger.new(@log = StringIO.new)}
it "should write to the log in debug" do
Statsd.logger.level = Logger::DEBUG
@statsd.increment('foobar')
@log.string.must_match "Statsd: foobar:1|c"
end
it "should not write to the log unless debug" do
Statsd.logger.level = Logger::INFO
@statsd.increment('foobar')
@log.string.must_be_empty
end
end
describe "stat names" do
it "should accept anything as stat" do
@statsd.increment(Object, 1)
end
it "should replace ruby constant delimeter with graphite package name" do
class Statsd::SomeClass; end
@statsd.increment(Statsd::SomeClass, 1)
@statsd.socket.recv.must_equal ['Statsd.SomeClass:1|c']
end
it "should replace statsd reserved chars in the stat name" do
@statsd.increment('ray@hostname.blah|blah.blah:blah', 1)
@statsd.socket.recv.must_equal ['ray_hostname.blah_blah.blah_blah:1|c']
end
end
describe "should support direct calls" do
before do
Statsd.clear_setup
Statsd.setup(:host => "localhost", :port => 1234)
@instance = Statsd.instance_variable_get('@instance')
class << @instance
public :sampled # we need to test this
attr_reader :host, :port # we also need to test this
def socket; @socket ||= FakeUDPSocket.new end
end
end
it 'should complain if setup has not been called' do
Statsd.clear_setup
assert_raises(Statsd::ConfigError) do
Statsd.timing("dummy", rand)
end
end
it 'should setup instance correctly' do
@instance.host.must_equal 'localhost'
@instance.port.must_equal 1234
end
it "should support increment" do
Statsd.increment('foobar')
@instance.socket.recv.must_equal ['foobar:1|c']
end
it "should support decrement" do
Statsd.decrement('foobar')
@instance.socket.recv.must_equal ['foobar:-1|c']
end
it "should support timing" do
t = 100
Statsd.timing('foobar', t)
@instance.socket.recv.must_equal ["foobar:#{t}|ms"]
end
it "should support time" do
Statsd.time('foobar') { sleep(0.001); 'test' }
@instance.socket.recv.must_equal ['foobar:1|ms']
end
it "should support sampled" do
Statsd.sampled(1) { :yielded }.must_equal :yielded
end
end
end
describe Statsd do
describe "with a real UDP socket" do
it "should actually send stuff over the socket" do
socket = UDPSocket.new
host, port = 'localhost', 12345
socket.bind(host, port)
statsd = Statsd.new(host, port)
statsd.increment('foobar')
message = socket.recvfrom(16).first
message.must_equal 'foobar:1|c'
end
end
end if ENV['LIVE']