forked from github/statsd-ruby
/
statsd_spec.rb
135 lines (115 loc) · 4.06 KB
/
statsd_spec.rb
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
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
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']