beaneater 1.3.0 → 1.3.2

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/test/jobs_test.rb DELETED
@@ -1,549 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # test/jobs_test.rb
4
-
5
- require File.expand_path('../test_helper', __FILE__)
6
-
7
- describe Beaneater::Jobs do
8
- before do
9
- @beanstalk = Beaneater.new('localhost')
10
- @jobs = Beaneater::Jobs.new(@beanstalk)
11
- @tube = @beanstalk.tubes.find('baz')
12
- end
13
-
14
- describe "for #find" do
15
- before do
16
- @time = Time.now.to_i
17
- @tube.put("foo find #{@time}")
18
- @job = @tube.peek(:ready)
19
- end
20
-
21
- it "should return job from id" do
22
- assert_equal "foo find #{@time}", @jobs.find(@job.id).body
23
- end
24
-
25
- it "should return job using peek" do
26
- assert_equal "foo find #{@time}", @jobs.find(@job.id).body
27
- end
28
-
29
- it "should return job using hash syntax" do
30
- assert_equal "foo find #{@time}", @jobs.find(@job.id).body
31
- end
32
-
33
- it "should return nil for invalid negative id" do
34
- assert_nil @jobs.find(10000)
35
- end
36
-
37
- it "should return nil for invalid negative id" do
38
- assert_raises(Beaneater::BadFormatError) { @jobs.find(-1) }
39
- end
40
- end # find
41
-
42
- describe "for #register!" do
43
- before do
44
- $foo = 0
45
- @jobs.register('tube', :retry_on => [Timeout::Error]) do |job|
46
- $foo += 1
47
- end
48
- end
49
-
50
- it "should store processor" do
51
- assert_equal 'tube', @jobs.processors.keys.first
52
- assert_equal [Timeout::Error], @jobs.processors.values.first[:retry_on]
53
- end
54
-
55
- it "should store block for 'tube'" do
56
- @jobs.processors['tube'][:block].call nil
57
- assert_equal 1, $foo
58
- end
59
- end # register!
60
-
61
- describe "for process! with no processors" do
62
- it "should raise ArgumentError without sending a command" do
63
- client = Beaneater.new('localhost') # not @beanstalk, which the teardown uses
64
- client.connection.expects(:transmit).never
65
- error = assert_raises(ArgumentError) { Beaneater::Jobs.new(client).process! }
66
- assert_match(/register/, error.message)
67
- ensure
68
- client.close if client
69
- end
70
- end # process! with no processors
71
-
72
- describe "for process!" do
73
- before do
74
- $foo = []
75
-
76
- @jobs.register('tube_success', :retry_on => [Timeout::Error]) do |job|
77
- # p job.body
78
- $foo << job.body
79
- raise Beaneater::AbortProcessingError if job.body =~ /abort/
80
- end
81
-
82
- @jobs.register('tube_release', :retry_on => [Timeout::Error], :max_retries => 2) do |job|
83
- $foo << job.body
84
- raise Timeout::Error
85
- end
86
-
87
- @jobs.register('tube_buried') do |job|
88
- $foo << job.body
89
- raise RuntimeError
90
- end
91
-
92
- cleanup_tubes!(['tube_success', 'tube_release', 'tube_buried'])
93
-
94
- @beanstalk.tubes.find('tube_success').put("success abort", :pri => 2**31 + 1)
95
- @beanstalk.tubes.find('tube_success').put("success 2", :pri => 1)
96
- @beanstalk.tubes.find('tube_release').put("released")
97
- @beanstalk.tubes.find('tube_buried').put("buried")
98
-
99
- @jobs.process!(:release_delay => 0)
100
- end
101
-
102
- it "should process all jobs" do
103
- assert_equal ['success 2', 'released', 'released', 'released', 'buried', 'success abort'], $foo
104
- end
105
-
106
- it "should clear successful_jobs" do
107
- assert_equal 0, @beanstalk.tubes.find('tube_success').stats.current_jobs_ready
108
- assert_equal 1, @beanstalk.tubes.find('tube_success').stats.current_jobs_buried
109
- assert_equal 0, @beanstalk.tubes.find('tube_success').stats.current_jobs_reserved
110
- end
111
-
112
- it "should retry release jobs 2 times" do
113
- assert_equal 2, @beanstalk.tubes.find('tube_release').peek(:buried).stats.releases
114
- end
115
-
116
- it "should bury unexpected exception" do
117
- assert_equal 1, @beanstalk.tubes.find('tube_buried').stats.current_jobs_buried
118
- end
119
- end # for_process!
120
-
121
- describe "for process! with a dashed tube name" do
122
- before do
123
- $ran = []
124
- cleanup_tubes!(['email-jobs', 'tube_stop'])
125
- @jobs.register('email-jobs') { |job| $ran << job.body }
126
- @jobs.register('tube_stop') { |job| $ran << job.body; @jobs.stop! }
127
- @beanstalk.tubes.find('email-jobs').put('email', :pri => 1)
128
- @beanstalk.tubes.find('tube_stop').put('stop', :pri => 2)
129
- @jobs.process!(:release_delay => 0)
130
- end
131
-
132
- it "should run the handler registered for the tube" do
133
- assert_equal ['email', 'stop'], $ran
134
- assert_nil @beanstalk.tubes.find('email-jobs').peek(:buried)
135
- end
136
- end # process! with a dashed tube name
137
-
138
- describe "for process! when a failing handler no longer holds its job" do
139
- before do
140
- $foo = []
141
- cleanup_tubes!(['tube_lost'])
142
- end
143
-
144
- after do
145
- cleanup_tubes!(['tube_lost'])
146
- end
147
-
148
- # The handler loses 'first', puts 'second' and raises. 'second' outranks 'first', so it is
149
- # reserved next whatever happened to 'first'.
150
- def process_until_second_job(ttr = 120, &lose_job)
151
- tube = @beanstalk.tubes.find('tube_lost')
152
- @jobs.register('tube_lost') do |job|
153
- $foo << job.body
154
- if job.body == 'second'
155
- @jobs.stop!
156
- else
157
- lose_job.call(job)
158
- tube.put('second', :pri => 0)
159
- raise RuntimeError
160
- end
161
- end
162
- tube.put('first', :pri => 1, :ttr => ttr)
163
- Timeout.timeout(15) { @jobs.process!(:reserve_timeout => 1) }
164
- end
165
-
166
- it "keeps processing when the handler deleted the job" do
167
- process_until_second_job { |job| job.delete }
168
- assert_equal ['first', 'second'], $foo
169
- end
170
-
171
- it "keeps processing when the handler released the job" do
172
- process_until_second_job { |job| job.release(:delay => 5) }
173
- assert_equal ['first', 'second'], $foo
174
- assert_equal 1, @beanstalk.tubes.find('tube_lost').stats.current_jobs_delayed
175
- end
176
-
177
- it "keeps processing when the handler overran the job's TTR" do
178
- process_until_second_job(1) do |job|
179
- # the server releases a job some time after its TTR runs out
180
- deadline = Time.now + 10
181
- sleep 0.1 while job.stats.state == 'reserved' && Time.now < deadline
182
- refute_equal 'reserved', job.stats.state
183
- end
184
- assert_equal ['first', 'second'], $foo
185
- end
186
- end
187
-
188
- describe "for process! with stop!" do
189
- before do
190
- cleanup_tubes!(['tube_stop'])
191
- end
192
-
193
- after do
194
- cleanup_tubes!(['tube_stop'])
195
- end
196
-
197
- # Runs process! in a thread and yields. Returns :returned, the exception process! raised, or
198
- # :still_running if it hadn't finished within seconds.
199
- def process_in_thread(seconds)
200
- worker = Thread.new do
201
- begin
202
- @jobs.process!(:reserve_timeout => 1)
203
- :returned
204
- rescue StandardError => ex
205
- ex
206
- end
207
- end
208
- yield if block_given?
209
- return worker.value if worker.join(seconds)
210
- worker.kill
211
- :still_running
212
- end
213
-
214
- it "returns within one reserve_timeout when stop! is called while idle" do
215
- @jobs.register('tube_stop') { |job| }
216
- result = process_in_thread(3) do
217
- sleep 0.5
218
- @jobs.stop!
219
- end
220
- assert_equal :returned, result
221
- end
222
-
223
- it "returns when stop! is called by a handler whose job another worker took over" do
224
- other = Beaneater.new('localhost')
225
- other.tubes.watch!('tube_stop')
226
- runs = 0
227
- @jobs.register('tube_stop') do |job|
228
- runs += 1
229
- job.release # as the server does once a job's TTR runs out
230
- other.tubes.reserve(1)
231
- @jobs.stop!
232
- end
233
- @beanstalk.tubes.find('tube_stop').put('taken over')
234
-
235
- assert_equal :returned, process_in_thread(5)
236
- assert_equal 1, runs
237
- ensure
238
- other.close if other
239
- end
240
- end
241
-
242
- describe "for process! when the job_parser rejects a job" do
243
- before do
244
- cleanup_tubes!(['tube_bad_body'])
245
- Beaneater.configure.job_parser = lambda { |body| JSON.parse(body) }
246
- end
247
-
248
- after do
249
- Beaneater.configure.job_parser = lambda { |body| body }
250
- cleanup_tubes!(['tube_bad_body'])
251
- end
252
-
253
- it "buries the job and goes on to the next one" do
254
- handled = []
255
- @jobs.register('tube_bad_body') do |job|
256
- handled << job.body
257
- @jobs.stop!
258
- end
259
- tube = @beanstalk.tubes.find('tube_bad_body')
260
- tube.put('not json', :pri => 1)
261
- tube.put('{"ok":1}', :pri => 2)
262
-
263
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
264
- assert_equal [{ 'ok' => 1 }], handled
265
- Beaneater.configure.job_parser = lambda { |body| body }
266
- buried = tube.peek(:buried)
267
- refute_nil buried, "the job the parser rejected was not buried"
268
- assert_equal 'not json', buried.body
269
- end
270
-
271
- # process! goes on after NotFoundError (and JobNotReserved, TimedOutError); the job it had
272
- # must not be taken for the bad job that follows.
273
- def put_after_retry_jobs
274
- tube = @beanstalk.tubes.find('tube_bad_body')
275
- first = tube.put('{"first":1}', :pri => 1)[:id]
276
- bad = tube.put('not json', :pri => 2)[:id]
277
- tube.put('{"stop":1}', :pri => 3)
278
- [tube, first, bad]
279
- end
280
-
281
- def stats_of(id)
282
- Beaneater.configure.job_parser = lambda { |body| body }
283
- @beanstalk.connection.transmit("stats-job #{id}")[:body]
284
- end
285
-
286
- it "buries the bad job after a handler deleted its own job" do
287
- @jobs.register('tube_bad_body') do |job|
288
- job.body == { 'stop' => 1 } ? @jobs.stop! : job.delete # process!'s own delete then gets NOT_FOUND
289
- end
290
- tube, _first, bad = put_after_retry_jobs
291
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
292
- assert_equal 'buried', stats_of(bad)['state']
293
- end
294
-
295
- it "buries both the failed job and the bad job after a handler raised NotFoundError" do
296
- @jobs.register('tube_bad_body') do |job|
297
- if job.body == { 'stop' => 1 }
298
- @jobs.stop!
299
- else
300
- @beanstalk.connection.transmit('delete 999999') # NOT_FOUND for some other job
301
- end
302
- end
303
- tube, first, bad = put_after_retry_jobs
304
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
305
- assert_equal 'buried', stats_of(bad)['state']
306
- assert_equal 'buried', stats_of(first)['state'] # its handler failed, with the job still reserved
307
- ensure
308
- begin
309
- @beanstalk.connection.transmit("delete #{first}") if first
310
- rescue Beaneater::NotFoundError
311
- nil
312
- end
313
- end
314
-
315
- it "buries the job when the parser raises an error process! handles itself" do
316
- Beaneater.configure.job_parser = lambda do |body|
317
- raise Beaneater::NotFoundError.new('NOT_FOUND', 'parse') if body == 'not found'
318
- raise Beaneater::TimedOutError.new('TIMED_OUT', 'parse') if body == 'timed out'
319
- body
320
- end
321
- handled = []
322
- @jobs.register('tube_bad_body') do |job|
323
- handled << job.body
324
- @jobs.stop!
325
- end
326
- tube = @beanstalk.tubes.find('tube_bad_body')
327
- not_found = tube.put('not found', :pri => 1, :ttr => 2)[:id]
328
- timed_out = tube.put('timed out', :pri => 2, :ttr => 2)[:id]
329
- tube.put('stop', :pri => 3)
330
-
331
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
332
- assert_equal ['stop'], handled
333
- [not_found, timed_out].each do |id|
334
- stats = stats_of(id)
335
- assert_equal 'buried', stats['state'], "job #{id}"
336
- assert_equal 0, stats['timeouts'], "job #{id}"
337
- end
338
- end
339
-
340
- it "buries the job and stops when the parser raises AbortProcessingError" do
341
- Beaneater.configure.job_parser = lambda { |body| raise Beaneater::AbortProcessingError if body == 'abort'; body }
342
- @jobs.register('tube_bad_body') { |job| }
343
- id = @beanstalk.tubes.find('tube_bad_body').put('abort')[:id]
344
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
345
- assert_equal 'buried', stats_of(id)['state']
346
- end
347
-
348
- it "buries the job when the parser raises a frozen exception" do
349
- frozen = ArgumentError.new('frozen').freeze
350
- Beaneater.configure.job_parser = lambda { |body| body == 'bad' ? raise(frozen) : body }
351
- handled = []
352
- @jobs.register('tube_bad_body') do |job|
353
- handled << job.body
354
- @jobs.stop!
355
- end
356
- tube = @beanstalk.tubes.find('tube_bad_body')
357
- tube.put('bad', :pri => 1)
358
- tube.put('good', :pri => 2)
359
-
360
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
361
- assert_equal ['good'], handled
362
- Beaneater.configure.job_parser = lambda { |body| body }
363
- assert_equal 'bad', (tube.peek(:buried) || flunk("the bad job was not buried")).body
364
- end
365
-
366
- it "buries the handler's job, not the one its own reserve rejected, when a handler raises it" do
367
- cleanup_tubes!(['tube_bad_body_other'])
368
- other_id = @beanstalk.tubes.find('tube_bad_body_other').put('not json')[:id]
369
- @jobs.register('tube_bad_body') do |job|
370
- @jobs.stop!
371
- @beanstalk.tubes.watch('tube_bad_body_other')
372
- @beanstalk.tubes.reserve(0) # raises: the parser rejects the other tube's job
373
- end
374
- @beanstalk.tubes.find('tube_bad_body').put('{"ok":1}')
375
-
376
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
377
- Beaneater.configure.job_parser = lambda { |body| body }
378
- assert_equal '{"ok":1}', (@beanstalk.tubes.find('tube_bad_body').peek(:buried) || flunk("the handler's job was not buried")).body
379
- assert_equal 'reserved', @beanstalk.connection.transmit("stats-job #{other_id}")[:body]['state']
380
- ensure
381
- Beaneater.configure.job_parser = lambda { |body| body }
382
- begin
383
- @beanstalk.connection.transmit("delete #{other_id}") if other_id
384
- rescue Beaneater::NotFoundError
385
- nil
386
- end
387
- cleanup_tubes!(['tube_bad_body_other'])
388
- end
389
- end
390
-
391
- describe "for process! when a handler raises NotFoundError with its job reserved" do
392
- before do
393
- cleanup_tubes!(['tube_not_found'])
394
- end
395
-
396
- after do
397
- cleanup_tubes!(['tube_not_found'])
398
- end
399
-
400
- it "buries the job once instead of reserving in a loop near its TTR" do
401
- runs = 0
402
- @jobs.register('tube_not_found') do |job|
403
- runs += 1
404
- @beanstalk.connection.transmit('delete 999999') # NOT_FOUND for some other job
405
- end
406
- id = @beanstalk.tubes.find('tube_not_found').put('x', :ttr => 2)[:id]
407
- worker = Thread.new { @jobs.process!(:reserve_timeout => 1) }
408
- sleep 3 # past the job's TTR
409
- @jobs.stop!
410
- assert worker.join(5), "process! did not stop"
411
- checker = Beaneater.new('localhost')
412
- stats = checker.connection.transmit("stats-job #{id}")[:body]
413
- checker.close
414
- assert_equal 1, runs
415
- assert_equal 'buried', stats['state']
416
- assert_equal 0, stats['timeouts']
417
- end
418
-
419
- it "goes on when the job stops being reserved just before it is buried" do
420
- @jobs.register('tube_not_found') do |job|
421
- @jobs.stop!
422
- raise 'handler failed'
423
- end
424
- id = @beanstalk.tubes.find('tube_not_found').put('x')[:id]
425
- Beaneater::Job.any_instance.stubs(:bury).raises(Beaneater::JobNotReserved) # another client's reservation ended
426
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) } # must not raise JobNotReserved
427
- assert_nil @jobs.current_job
428
- ensure
429
- Beaneater::Job.any_instance.unstub(:bury)
430
- begin
431
- @beanstalk.connection.transmit("delete #{id}") if id
432
- rescue Beaneater::NotFoundError
433
- nil
434
- end
435
- end
436
-
437
- it "releases the job for another try instead when the error is in retry_on" do
438
- runs = 0
439
- @jobs.register('tube_not_found', :retry_on => [Beaneater::NotFoundError], :max_retries => 1) do |job|
440
- runs += 1
441
- @jobs.stop! if runs == 2
442
- @beanstalk.connection.transmit('delete 999999') # NOT_FOUND for some other job
443
- end
444
- id = @beanstalk.tubes.find('tube_not_found').put('x')[:id]
445
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1, :release_delay => 0) }
446
- stats = @beanstalk.connection.transmit("stats-job #{id}")[:body]
447
- assert_equal 2, runs
448
- assert_equal 1, stats['releases'] # released once, then buried once the retries ran out
449
- assert_equal 'buried', stats['state']
450
- end
451
- end
452
-
453
- describe "for process! when a handler raises NotConnected for another server" do
454
- before do
455
- cleanup_tubes!(['tube_other_down'])
456
- end
457
-
458
- it "buries the job and keeps processing" do
459
- seen = []
460
- @jobs.register('tube_other_down') do |job|
461
- seen << job.body
462
- @jobs.stop! if job.body == 'second'
463
- raise Beaneater::NotConnected, "the other server is down" if job.body == 'first'
464
- end
465
- tube = @beanstalk.tubes.find('tube_other_down')
466
- tube.put('first', :pri => 1)
467
- tube.put('second', :pri => 2)
468
-
469
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
470
- assert_equal ['first', 'second'], seen
471
- assert_equal 'first', tube.peek(:buried).body
472
- end
473
- end
474
-
475
- describe "for process! when the server goes away" do
476
- before do
477
- @port = free_port
478
- @pid = start_beanstalkd(@port)
479
- @client = Beaneater.new("127.0.0.1:#{@port}")
480
- end
481
-
482
- after do
483
- @client.close if @client
484
- stop_beanstalkd(@pid) if @pid
485
- end
486
-
487
- it "forgets the job when burying it fails, so the next run buries its own bad job" do
488
- Beaneater.configure.job_parser = lambda do |body|
489
- stop_beanstalkd(@pid) if body == 'kill' # the server dies before the bad job is buried
490
- JSON.parse(body)
491
- end
492
- handled = []
493
- @client.jobs.register('bury_fails') do |job|
494
- handled << job.body
495
- @client.jobs.stop!
496
- end
497
- @client.tubes.find('bury_fails').put('kill')
498
- assert_raises(Beaneater::NotConnected) { Timeout.timeout(10) { @client.jobs.process!(:reserve_timeout => 1) } }
499
- assert_nil @client.jobs.current_job
500
-
501
- @pid = start_beanstalkd(@port) # a new server: job ids start again at 1
502
- producer = Beaneater.new("127.0.0.1:#{@port}")
503
- producer.tubes.find('bury_fails_filler').put('filler') # takes the id the lost job had
504
- producer.tubes.find('bury_fails').put('not json', :pri => 1)
505
- producer.tubes.find('bury_fails').put('{"ok":1}', :pri => 2)
506
- Timeout.timeout(10) { @client.jobs.process!(:reserve_timeout => 1) }
507
- assert_equal [{ 'ok' => 1 }], handled
508
- Beaneater.configure.job_parser = lambda { |body| body }
509
- buried = producer.tubes.find('bury_fails').peek(:buried)
510
- refute_nil buried, "the bad job was not buried"
511
- assert_equal 'not json', buried.body
512
- producer.close
513
- ensure
514
- Beaneater.configure.job_parser = lambda { |body| body }
515
- end
516
-
517
- it "raises NotConnected instead of looping, and the client resumes once the server is back" do
518
- seen = []
519
- @client.jobs.register('reconnect') do |job|
520
- seen << job.body
521
- @client.jobs.stop! if job.body == 'after restart'
522
- end
523
- @client.tubes.find('reconnect').put('before outage')
524
-
525
- worker = Thread.new do
526
- begin
527
- @client.jobs.process!(:reserve_timeout => 1)
528
- rescue StandardError => ex
529
- ex
530
- end
531
- end
532
- Timeout.timeout(5) { sleep 0.05 while seen.empty? }
533
- stop_beanstalkd(@pid)
534
-
535
- finished = worker.join(10)
536
- worker.kill unless finished
537
- assert finished, "process! was still running 10s after the server went away"
538
- assert_kind_of Beaneater::NotConnected, worker.value
539
-
540
- @pid = start_beanstalkd(@port)
541
- producer = Beaneater.new("127.0.0.1:#{@port}")
542
- producer.tubes.find('reconnect').put('after restart')
543
- producer.close
544
-
545
- Timeout.timeout(10) { @client.jobs.process!(:reserve_timeout => 1) }
546
- assert_equal ['before outage', 'after restart'], seen
547
- end
548
- end
549
- end # Beaneater::Jobs
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # test/prompt_regexp_test.rb
4
-
5
- require File.expand_path('../test_helper', __FILE__)
6
- require 'socket'
7
-
8
- describe "Reading from socket client" do
9
- before do
10
- @fake_port = 11301
11
- @tube_name = 'tube.to.test'
12
-
13
- @fake_server = Thread.start do
14
- server = TCPServer.new(@fake_port)
15
- loop do
16
- IO.select([server])
17
- client = server.accept_nonblock
18
- while line = client.gets
19
- case line
20
- when /list-tubes-watched/i
21
- client.print "OK #{7+@tube_name.size}\r\n---\n- #{@tube_name}\n\r\n"
22
- when /watch #{@tube_name}/i
23
- client.print "WATCHING 1\r\n"
24
- when /reserve/i
25
- client.print "RESERVED 17 25\r\n"
26
- client.print "[first part]"
27
- # Emulate network delay
28
- sleep 0.5
29
- client.print "[second part]\r\n"
30
- else
31
- client.print "ERROR\r\n"
32
- end
33
- end
34
- end
35
- end
36
-
37
- slept = 0
38
- while @beanstalk.nil?
39
- begin
40
- @beanstalk = Beaneater.new("localhost:#{@fake_port}")
41
- rescue Beaneater::NotConnected
42
- raise 'Could not connect to fake beanstalkd server' if slept > 1
43
- sleep 0.1
44
- slept += 0.1
45
- end
46
- end
47
-
48
- end
49
-
50
- it 'should reserve job with full body' do
51
- job = @beanstalk.tubes[@tube_name].reserve
52
- assert_equal '[first part][second part]', job.body
53
- end
54
-
55
- after do
56
- @beanstalk.close
57
- @fake_server.kill
58
- end
59
- end
@@ -1,83 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # test/stat_struct_test.rb
4
-
5
- require File.expand_path('../test_helper', __FILE__)
6
-
7
- describe Beaneater::StatStruct do
8
- before do
9
- @hash = { :foo => "bar", :bar => "baz", :baz => "foo", :"under-score" => "demo" }
10
- @struct = Beaneater::StatStruct.from_hash(@hash)
11
- end
12
-
13
- describe "for #from_hash" do
14
- it "should have 4 keys" do
15
- assert_equal 'bar', @struct.foo
16
- assert_equal 'baz', @struct.bar
17
- assert_equal 'foo', @struct.baz
18
- assert_equal 'demo', @struct.under_score
19
- end
20
- end # from_hash
21
-
22
- describe "for [] access" do
23
- it "should have hash lookup" do
24
- assert_equal 'bar', @struct['foo']
25
- assert_equal 'baz', @struct['bar']
26
- end
27
-
28
- it "should convert keys to string" do
29
- assert_equal 'foo', @struct[:baz]
30
- assert_equal 'demo', @struct[:"under_score"]
31
- end
32
-
33
- it "should look up hyphenated beanstalkd keys" do
34
- assert_equal 'demo', @struct['under-score']
35
- assert_equal 'demo', @struct[:"under-score"]
36
- end
37
-
38
- it "should read a stat whose name is also a method name" do
39
- struct = Beaneater::StatStruct.from_hash('hash' => 'h', 'class' => 'c')
40
- assert_equal 'h', struct['hash']
41
- assert_equal 'c', struct[:class]
42
- end
43
-
44
- it "should return nil for a key that is not a stat" do
45
- [:object_id, 'send', 'freeze', 'foo=', 'no such key'].each do |key|
46
- assert_nil @struct[key], "Expected nil for #{key.inspect}"
47
- end
48
- refute @struct.frozen?
49
- end
50
- end # []
51
-
52
- describe "for #keys" do
53
- it "should return 4 keys" do
54
- assert_equal 4, @struct.keys.size
55
- end
56
-
57
- it "should return expected keys" do
58
- assert_equal ['foo', 'bar', 'baz', 'under_score'].sort, @struct.keys.sort
59
- end
60
- end # keys
61
-
62
- describe "for #respond_to?" do
63
- it "should respond to a key before it is first read" do
64
- struct = Beaneater::StatStruct.from_hash('rusage-utime' => 0.5)
65
- assert struct.respond_to?(:rusage_utime), "Expected struct to respond to rusage_utime"
66
- assert_equal 0.5, struct.method(:rusage_utime).call
67
- end
68
-
69
- it "should not respond to a name that is not a key" do
70
- refute @struct.respond_to?(:no_such_key)
71
- end
72
- end # respond_to?
73
-
74
- describe "for #to_h" do
75
- it "should return 4 keys / values" do
76
- assert_equal 4, @struct.to_h.size
77
- end
78
-
79
- it "should return expect hash" do
80
- assert_equal [['foo', 'bar'], ['bar', 'baz'], ['baz', 'foo'], ['under_score', 'demo']].sort, @struct.to_h.sort
81
- end
82
- end # to_h
83
- end # Beaneater::StatStruct