riemann-client 1.0.0 → 1.1.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.
data/spec/client.rb DELETED
@@ -1,384 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # How to run the bacon tests:
4
- # 1. Start Riemann using the config from riemann.config
5
- # 2. $ bundle exec bacon spec/client.rb
6
-
7
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'riemann'))
8
- require 'riemann/client'
9
- require 'bacon'
10
- require 'set'
11
- require 'timecop'
12
-
13
- Bacon.summary_on_exit
14
-
15
- include Riemann
16
-
17
- INACTIVITY_TIME = 5
18
-
19
- def wait_for(&block)
20
- tries = 0
21
- while tries < 30
22
- tries += 1
23
- begin
24
- res = block.call
25
- return res if res
26
- rescue NoMethodError
27
- # If a query returns no result (#query retruns nil or #[] returns []),
28
- # calling #first on it will raise a NoMethodError. We can ignore it for
29
- # these tests.
30
- end
31
- sleep(0.1)
32
- end
33
-
34
- raise "wait_for condition never realized"
35
- end
36
-
37
- def roundtrip_metric(m)
38
- @client_with_transport << {
39
- :service => 'metric-test',
40
- :metric => m
41
- }
42
-
43
- wait_for {@client["service = \"metric-test\" and metric = #{m}"].first }.
44
- metric.should.equal m
45
- end
46
-
47
- def truthy
48
- lambda { |obj| !(obj.nil? || obj == false) }
49
- end
50
-
51
- def falsey
52
- lambda { |obj| obj.nil? || obj == false }
53
- end
54
-
55
- shared "a riemann client" do
56
-
57
- should 'yield itself to given block' do
58
- client = nil
59
- Client.new(:host => "localhost", :port => 5555) do |c|
60
- client = c
61
- end
62
- client.should.be.kind_of?(Client)
63
- client.should.not.be.connected
64
- end
65
-
66
- should 'close sockets if given a block that raises' do
67
- client = nil
68
- begin
69
- Client.new(:host => "localhost", :port => 5555) do |c|
70
- client = c
71
- raise "The Boom"
72
- end
73
- rescue
74
- # swallow the exception
75
- end
76
- client.should.be.kind_of?(Client)
77
- client.should.not.be.connected
78
- end
79
-
80
- should 'be connected after sending' do
81
- @client_with_transport.connected?.should.be falsey
82
- @client.connected?.should.be falsey
83
- @client_with_transport << {:state => 'ok', :service => 'connected check' }
84
- @client_with_transport.connected?.should.be truthy
85
- # NOTE: only single transport connected at this point, @client.connected? is still false until all transports used
86
- end
87
-
88
- should 'send longs' do
89
- roundtrip_metric(0)
90
- roundtrip_metric(-3)
91
- roundtrip_metric(5)
92
- roundtrip_metric(-(2**63))
93
- roundtrip_metric(2**63 - 1)
94
- end
95
-
96
- should 'send doubles' do
97
- roundtrip_metric 0.0
98
- roundtrip_metric 12.0
99
- roundtrip_metric 1.2300000190734863
100
- end
101
-
102
- should 'send custom attributes' do
103
- event = Event.new(
104
- :service => 'custom',
105
- :state => 'ok',
106
- :cats => 'meow',
107
- :env => 'prod'
108
- )
109
- event[:sneak] = 'attack'
110
- @client_with_transport << event
111
- event2 = wait_for { @client['service = "custom"'].first }
112
- event2.service.should.equal 'custom'
113
- event2.state.should.equal 'ok'
114
- event2[:cats].should.equal 'meow'
115
- event2[:env].should.equal 'prod'
116
- event2[:sneak].should.equal 'attack'
117
- end
118
-
119
- should 'send a state with a time' do
120
- Timecop.freeze do
121
- t = (Time.now - 10).to_i
122
- @client_with_transport << {
123
- :state => 'ok',
124
- :service => 'test',
125
- :time => t
126
- }
127
- wait_for { @client.query('service = "test"').events.first.time == t }
128
- e = @client.query('service = "test"').events.first
129
- e.time.should.equal t
130
- e.time_micros.should.equal t * 1_000_000
131
- end
132
- end
133
-
134
- should 'send a state with a time_micros' do
135
- Timecop.freeze do
136
- t = ((Time.now - 10).to_f * 1_000_000).to_i
137
- @client_with_transport << {
138
- :state => 'ok',
139
- :service => 'test',
140
- :time_micros => t
141
- }
142
- wait_for { @client.query('service = "test"').events.first.time_micros == t }
143
- e = @client.query('service = "test"').events.first
144
- e.time.should.equal (Time.now - 10).to_i
145
- e.time_micros.should.equal t
146
- end
147
- end
148
-
149
- should 'send a state without time nor time_micros' do
150
- time_before = (Time.now.to_f * 1_000_000).to_i
151
- @client_with_transport << {
152
- :state => 'ok',
153
- :service => 'timeless test'
154
- }
155
- wait_for { @client.query('service = "timeless test"').events.first.time_micros >= time_before }
156
- e = @client.query('service = "timeless test"').events.first
157
- time_after = (Time.now.to_f * 1_000_000).to_i
158
-
159
- [time_before, e.time_micros, time_after].sort.should.equal([time_before, e.time_micros, time_after])
160
- end
161
-
162
- should "query states" do
163
- @client_with_transport << { :state => 'critical', :service => '1' }
164
- @client_with_transport << { :state => 'warning', :service => '2' }
165
- @client_with_transport << { :state => 'critical', :service => '3' }
166
- wait_for { @client.query('service = "3"').events.first }
167
- @client.query.events.
168
- map(&:service).to_set.should.superset ['1', '2', '3'].to_set
169
- @client.query('state = "critical" and (service = "1" or service = "2" or service = "3")').events.
170
- map(&:service).to_set.should.equal ['1', '3'].to_set
171
- end
172
-
173
- it '[]' do
174
- # @client['state = "critical"'].should == []
175
- @client_with_transport << {:state => 'critical'}
176
- wait_for { @client['state = "critical"'].first }.state.should.equal 'critical'
177
- end
178
-
179
- should 'query quickly' do
180
- t1 = Time.now
181
- total = 1000
182
- total.times do |i|
183
- @client.query('state = "critical"')
184
- end
185
- t2 = Time.now
186
-
187
- rate = total / (t2 - t1)
188
- puts "\n #{"%.2f" % rate} queries/sec (#{"%.2f" % (1000/rate)}ms per query)"
189
- rate.should > 100
190
- end
191
-
192
- should 'be threadsafe' do
193
- concurrency = 10
194
- per_thread = 200
195
- total = concurrency * per_thread
196
-
197
- t1 = Time.now
198
- (0...concurrency).map do |i|
199
- Thread.new do
200
- per_thread.times do
201
- @client_with_transport.<<({
202
- :state => 'ok',
203
- :service => 'test',
204
- :description => 'desc',
205
- :metric_f => 1.0
206
- })
207
- end
208
- end
209
- end.each do |t|
210
- t.join
211
- end
212
- t2 = Time.now
213
-
214
- rate = total / (t2 - t1)
215
- puts "\n #{"%.2f" % rate} inserts/sec (#{"%.2f" % (1000/rate)}ms per insert)"
216
- rate.should > @expected_rate
217
- end
218
-
219
- end
220
-
221
- describe "Riemann::Client (TLS transport)" do
222
- before do
223
- @client = Client.new(:host => "localhost", :port => 5554, :ssl => true, :key_file => '/etc/riemann/riemann_server.pkcs8', :cert_file => '/etc/riemann/riemann_server.crt', :ca_file => '/etc/riemann/riemann_server.crt', :ssl_verify => true)
224
- @client_with_transport = @client.tcp
225
- @expected_rate = 100
226
- end
227
- behaves_like "a riemann client"
228
-
229
- should 'send a state' do
230
- res = @client_with_transport << {
231
- :state => 'ok',
232
- :service => 'test',
233
- :description => 'desc',
234
- :metric_f => 1.0
235
- }
236
-
237
- res.ok.should.be truthy
238
- wait_for { @client['service = "test"'].first }.state.should.equal 'ok'
239
- end
240
-
241
- should 'survive inactivity' do
242
- @client_with_transport.<<({
243
- :state => 'warning',
244
- :service => 'survive TCP inactivity',
245
- })
246
- wait_for { @client['service = "survive TCP inactivity"'].first.state == 'warning' }
247
-
248
- sleep INACTIVITY_TIME
249
-
250
- @client_with_transport.<<({
251
- :state => 'ok',
252
- :service => 'survive TCP inactivity',
253
- }).ok.should.be truthy
254
- wait_for { @client['service = "survive TCP inactivity"'].first.state == 'ok' }
255
- end
256
-
257
- should 'survive local close' do
258
- @client_with_transport.<<({
259
- :state => 'warning',
260
- :service => 'survive TCP local close',
261
- }).ok.should.be truthy
262
- wait_for { @client['service = "survive TCP local close"'].first .state == 'warning' }
263
-
264
- @client.close
265
-
266
- @client_with_transport.<<({
267
- :state => 'ok',
268
- :service => 'survive TCP local close',
269
- }).ok.should.be truthy
270
- wait_for { @client['service = "survive TCP local close"'].first.state == 'ok' }
271
- end
272
- end
273
-
274
- describe "Riemann::Client (TCP transport)" do
275
- before do
276
- @client = Client.new(:host => "localhost", :port => 5555)
277
- @client_with_transport = @client.tcp
278
- @expected_rate = 100
279
- end
280
- behaves_like "a riemann client"
281
-
282
- should 'send a state' do
283
- res = @client_with_transport << {
284
- :state => 'ok',
285
- :service => 'test',
286
- :description => 'desc',
287
- :metric_f => 1.0
288
- }
289
-
290
- res.ok.should.be truthy
291
- wait_for { @client['service = "test"'].first }.state.should.equal 'ok'
292
- end
293
-
294
- should 'survive inactivity' do
295
- @client_with_transport.<<({
296
- :state => 'warning',
297
- :service => 'survive TCP inactivity',
298
- })
299
- wait_for { @client['service = "survive TCP inactivity"'].first.state == 'warning' }
300
-
301
- sleep INACTIVITY_TIME
302
-
303
- @client_with_transport.<<({
304
- :state => 'ok',
305
- :service => 'survive TCP inactivity',
306
- }).ok.should.be truthy
307
- wait_for { @client['service = "survive TCP inactivity"'].first.state == 'ok' }
308
- end
309
-
310
- should 'survive local close' do
311
- @client_with_transport.<<({
312
- :state => 'warning',
313
- :service => 'survive TCP local close',
314
- }).ok.should.be truthy
315
- wait_for { @client['service = "survive TCP local close"'].first.state == 'warning' }
316
-
317
- @client.close
318
-
319
- @client_with_transport.<<({
320
- :state => 'ok',
321
- :service => 'survive TCP local close',
322
- }).ok.should.be truthy
323
- wait_for { @client['service = "survive TCP local close"'].first.state == 'ok' }
324
- end
325
- end
326
-
327
- describe "Riemann::Client (UDP transport)" do
328
- before do
329
- @client = Client.new(:host => "localhost", :port => 5555)
330
- @client_with_transport = @client.udp
331
- @expected_rate = 1000
332
- end
333
- behaves_like "a riemann client"
334
-
335
- should 'send a state' do
336
- res = @client_with_transport << {
337
- :state => 'ok',
338
- :service => 'test',
339
- :description => 'desc',
340
- :metric_f => 1.0
341
- }
342
-
343
- res.should.be.nil
344
- wait_for { @client['service = "test"'].first }.state.should.equal 'ok'
345
- end
346
-
347
- should 'survive inactivity' do
348
- @client_with_transport.<<({
349
- :state => 'warning',
350
- :service => 'survive UDP inactivity',
351
- }).should.be.nil
352
- wait_for { @client['service = "survive UDP inactivity"'].first.state == 'warning' }
353
-
354
- sleep INACTIVITY_TIME
355
-
356
- @client_with_transport.<<({
357
- :state => 'ok',
358
- :service => 'survive UDP inactivity',
359
- }).should.be.nil
360
- wait_for { @client['service = "survive UDP inactivity"'].first.state == 'ok' }
361
- end
362
-
363
- should 'survive local close' do
364
- @client_with_transport.<<({
365
- :state => 'warning',
366
- :service => 'survive UDP local close',
367
- }).should.be.nil
368
- wait_for { @client['service = "survive UDP local close"'].first.state == 'warning' }
369
-
370
- @client.close
371
-
372
- @client_with_transport.<<({
373
- :state => 'ok',
374
- :service => 'survive UDP local close',
375
- }).should.be.nil
376
- wait_for { @client['service = "survive UDP local close"'].first.state == 'ok' }
377
- end
378
-
379
- should "raise Riemann::Client::Unsupported exception on query" do
380
- should.raise(Riemann::Client::Unsupported) { @client_with_transport['service = "test"'] }
381
- should.raise(Riemann::Client::Unsupported) { @client_with_transport.query('service = "test"') }
382
- end
383
-
384
- end