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
191 lines (159 loc) · 5.74 KB
/
statsd_spec.rb
File metadata and controls
191 lines (159 loc) · 5.74 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
require_relative './helper'
describe GitHub::Statsd do
before do
@statsd = GitHub::Statsd.new(FakeUDPSocket)
@statsd.add_shard
class << @statsd
public :sampled # we need to test this
end
end
after { @statsd.shards.first.clear }
describe "#initialize" do
it "should default client class to UDPClient" do
statsd = GitHub::Statsd.new
statsd.client_class.must_equal GitHub::Statsd::UDPClient
end
it "should allow changing client class" do
statsd = GitHub::Statsd.new(FakeUDPSocket)
statsd.client_class.must_equal FakeUDPSocket
end
end
describe "#increment" do
it "should format the message according to the statsd spec" do
@statsd.increment('foobar')
@statsd.shards.first.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.shards.first.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.shards.first.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.shards.first.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.shards.first.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.shards.first.recv.must_equal ['foobar:500|ms|@0.5']
end
end
end
describe "#distribution" do
it "should format the message according to the statsd spec" do
@statsd.distribution('foobar', 500)
@statsd.shards.first.recv.must_equal ["foobar:500|d"]
end
end
describe "#time" do
it "should format the message according to the statsd spec" do
@statsd.time('foobar') { sleep(0.001); 'test' }
data = @statsd.shards.first.recv
key, value, unit = data.first.split(/[:|]/)
key.must_equal "foobar"
value.must_match /^\d\.\d+$/
unit.must_equal "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' }
data = @statsd.shards.first.recv
key, value, unit, frequency = data.first.split(/[:|]/)
key.must_equal "foobar"
value.must_match /^\d\.\d+$/
unit.must_equal "ms"
frequency.must_equal "@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.shards.first.recv.must_equal ['service.foobar:1|c']
end
it "should add namespace to decrement" do
@statsd.decrement('foobar')
@statsd.shards.first.recv.must_equal ['service.foobar:-1|c']
end
it "should add namespace to timing" do
@statsd.timing('foobar', 500)
@statsd.shards.first.recv.must_equal ['service.foobar:500|ms']
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 GitHub::Statsd::SomeClass; end
@statsd.increment(GitHub::Statsd::SomeClass, 1)
@statsd.shards.first.recv.must_equal ['GitHub.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.shards.first.recv.must_equal ['ray_hostname.blah_blah.blah_blah:1|c']
end
end
end
describe GitHub::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 = GitHub::Statsd.new(host, port)
statsd.increment('foobar')
message = socket.recvfrom(16).first
message.must_equal 'foobar:1|c'
end
end
end if ENV['LIVE']