beaneater 1.3.1 → 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,597 +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! when it deletes or releases a job" do
122
- before do
123
- $ran = []
124
- cleanup_tubes!(['tube_done', 'tube_retry'])
125
- @sent = []
126
- sent = @sent
127
- @beanstalk.connection.define_singleton_method(:transmit) do |command, **options|
128
- sent << command.to_s
129
- super(command, **options)
130
- end
131
- @jobs.register('tube_retry', :retry_on => [Timeout::Error], :max_retries => 1) do |job|
132
- $ran << job.id
133
- raise Timeout::Error
134
- end
135
- @jobs.register('tube_done') { |job| $ran << job.id; @jobs.stop! if $ran.size == 4 }
136
- @retry_id = @beanstalk.tubes.find('tube_retry').put("retry", :pri => 1)[:id]
137
- @done_ids = 2.times.map { |i| @beanstalk.tubes.find('tube_done').put("done #{i}", :pri => 2)[:id] }
138
- Timeout.timeout(10) { @jobs.process!(:release_delay => 0, :reserve_timeout => 1) }
139
- end
140
-
141
- after do
142
- connection = @beanstalk.connection
143
- connection.singleton_class.send(:remove_method, :transmit) if connection.singleton_methods(false).include?(:transmit)
144
- end
145
-
146
- # The commands naming job id after the given command, up to the next reserve
147
- def sent_about(id, command)
148
- start = @sent.index(command)
149
- refute_nil start, "#{command.inspect} was not sent: #{@sent.inspect}"
150
- @sent.drop(start + 1).take_while { |c| !c.start_with?('reserve') }.select { |c| c.split(' ')[1] == id.to_s }
151
- end
152
-
153
- it "sends nothing more about a job it deleted" do
154
- @done_ids.each do |id|
155
- assert_empty sent_about(id, "delete #{id}"), "sent after deleting job #{id}: #{@sent.inspect}"
156
- end
157
- stats = @beanstalk.tubes.find('tube_done').stats
158
- assert_equal [0, 0, 0], [stats.current_jobs_ready, stats.current_jobs_reserved, stats.current_jobs_buried]
159
- end
160
-
161
- it "sends nothing more about a job it released, and buries it when it runs out of retries" do
162
- assert_empty sent_about(@retry_id, "release #{@retry_id} 1 0"), "sent after releasing job #{@retry_id}: #{@sent.inspect}"
163
- assert_equal [@retry_id, @retry_id], $ran.first(2)
164
- job = @beanstalk.jobs.find(@retry_id)
165
- assert_equal ['buried', 1], [job.stats.state, job.stats.releases]
166
- end
167
- end # process! when it deletes or releases a job
168
-
169
- describe "for process! with a dashed tube name" do
170
- before do
171
- $ran = []
172
- cleanup_tubes!(['email-jobs', 'tube_stop'])
173
- @jobs.register('email-jobs') { |job| $ran << job.body }
174
- @jobs.register('tube_stop') { |job| $ran << job.body; @jobs.stop! }
175
- @beanstalk.tubes.find('email-jobs').put('email', :pri => 1)
176
- @beanstalk.tubes.find('tube_stop').put('stop', :pri => 2)
177
- @jobs.process!(:release_delay => 0)
178
- end
179
-
180
- it "should run the handler registered for the tube" do
181
- assert_equal ['email', 'stop'], $ran
182
- assert_nil @beanstalk.tubes.find('email-jobs').peek(:buried)
183
- end
184
- end # process! with a dashed tube name
185
-
186
- describe "for process! when a failing handler no longer holds its job" do
187
- before do
188
- $foo = []
189
- cleanup_tubes!(['tube_lost'])
190
- end
191
-
192
- after do
193
- cleanup_tubes!(['tube_lost'])
194
- end
195
-
196
- # The handler loses 'first', puts 'second' and raises. 'second' outranks 'first', so it is
197
- # reserved next whatever happened to 'first'.
198
- def process_until_second_job(ttr = 120, &lose_job)
199
- tube = @beanstalk.tubes.find('tube_lost')
200
- @jobs.register('tube_lost') do |job|
201
- $foo << job.body
202
- if job.body == 'second'
203
- @jobs.stop!
204
- else
205
- lose_job.call(job)
206
- tube.put('second', :pri => 0)
207
- raise RuntimeError
208
- end
209
- end
210
- tube.put('first', :pri => 1, :ttr => ttr)
211
- Timeout.timeout(15) { @jobs.process!(:reserve_timeout => 1) }
212
- end
213
-
214
- it "keeps processing when the handler deleted the job" do
215
- process_until_second_job { |job| job.delete }
216
- assert_equal ['first', 'second'], $foo
217
- end
218
-
219
- it "keeps processing when the handler released the job" do
220
- process_until_second_job { |job| job.release(:delay => 5) }
221
- assert_equal ['first', 'second'], $foo
222
- assert_equal 1, @beanstalk.tubes.find('tube_lost').stats.current_jobs_delayed
223
- end
224
-
225
- it "keeps processing when the handler overran the job's TTR" do
226
- process_until_second_job(1) do |job|
227
- # the server releases a job some time after its TTR runs out
228
- deadline = Time.now + 10
229
- sleep 0.1 while job.stats.state == 'reserved' && Time.now < deadline
230
- refute_equal 'reserved', job.stats.state
231
- end
232
- assert_equal ['first', 'second'], $foo
233
- end
234
- end
235
-
236
- describe "for process! with stop!" do
237
- before do
238
- cleanup_tubes!(['tube_stop'])
239
- end
240
-
241
- after do
242
- cleanup_tubes!(['tube_stop'])
243
- end
244
-
245
- # Runs process! in a thread and yields. Returns :returned, the exception process! raised, or
246
- # :still_running if it hadn't finished within seconds.
247
- def process_in_thread(seconds)
248
- worker = Thread.new do
249
- begin
250
- @jobs.process!(:reserve_timeout => 1)
251
- :returned
252
- rescue StandardError => ex
253
- ex
254
- end
255
- end
256
- yield if block_given?
257
- return worker.value if worker.join(seconds)
258
- worker.kill
259
- :still_running
260
- end
261
-
262
- it "returns within one reserve_timeout when stop! is called while idle" do
263
- @jobs.register('tube_stop') { |job| }
264
- result = process_in_thread(3) do
265
- sleep 0.5
266
- @jobs.stop!
267
- end
268
- assert_equal :returned, result
269
- end
270
-
271
- it "returns when stop! is called by a handler whose job another worker took over" do
272
- other = Beaneater.new('localhost')
273
- other.tubes.watch!('tube_stop')
274
- runs = 0
275
- @jobs.register('tube_stop') do |job|
276
- runs += 1
277
- job.release # as the server does once a job's TTR runs out
278
- other.tubes.reserve(1)
279
- @jobs.stop!
280
- end
281
- @beanstalk.tubes.find('tube_stop').put('taken over')
282
-
283
- assert_equal :returned, process_in_thread(5)
284
- assert_equal 1, runs
285
- ensure
286
- other.close if other
287
- end
288
- end
289
-
290
- describe "for process! when the job_parser rejects a job" do
291
- before do
292
- cleanup_tubes!(['tube_bad_body'])
293
- Beaneater.configure.job_parser = lambda { |body| JSON.parse(body) }
294
- end
295
-
296
- after do
297
- Beaneater.configure.job_parser = lambda { |body| body }
298
- cleanup_tubes!(['tube_bad_body'])
299
- end
300
-
301
- it "buries the job and goes on to the next one" do
302
- handled = []
303
- @jobs.register('tube_bad_body') do |job|
304
- handled << job.body
305
- @jobs.stop!
306
- end
307
- tube = @beanstalk.tubes.find('tube_bad_body')
308
- tube.put('not json', :pri => 1)
309
- tube.put('{"ok":1}', :pri => 2)
310
-
311
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
312
- assert_equal [{ 'ok' => 1 }], handled
313
- Beaneater.configure.job_parser = lambda { |body| body }
314
- buried = tube.peek(:buried)
315
- refute_nil buried, "the job the parser rejected was not buried"
316
- assert_equal 'not json', buried.body
317
- end
318
-
319
- # process! goes on after NotFoundError (and JobNotReserved, TimedOutError); the job it had
320
- # must not be taken for the bad job that follows.
321
- def put_after_retry_jobs
322
- tube = @beanstalk.tubes.find('tube_bad_body')
323
- first = tube.put('{"first":1}', :pri => 1)[:id]
324
- bad = tube.put('not json', :pri => 2)[:id]
325
- tube.put('{"stop":1}', :pri => 3)
326
- [tube, first, bad]
327
- end
328
-
329
- def stats_of(id)
330
- Beaneater.configure.job_parser = lambda { |body| body }
331
- @beanstalk.connection.transmit("stats-job #{id}")[:body]
332
- end
333
-
334
- it "buries the bad job after a handler deleted its own job" do
335
- @jobs.register('tube_bad_body') do |job|
336
- job.body == { 'stop' => 1 } ? @jobs.stop! : job.delete # process!'s own delete then gets NOT_FOUND
337
- end
338
- tube, _first, bad = put_after_retry_jobs
339
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
340
- assert_equal 'buried', stats_of(bad)['state']
341
- end
342
-
343
- it "buries both the failed job and the bad job after a handler raised NotFoundError" do
344
- @jobs.register('tube_bad_body') do |job|
345
- if job.body == { 'stop' => 1 }
346
- @jobs.stop!
347
- else
348
- @beanstalk.connection.transmit('delete 999999') # NOT_FOUND for some other job
349
- end
350
- end
351
- tube, first, bad = put_after_retry_jobs
352
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
353
- assert_equal 'buried', stats_of(bad)['state']
354
- assert_equal 'buried', stats_of(first)['state'] # its handler failed, with the job still reserved
355
- ensure
356
- begin
357
- @beanstalk.connection.transmit("delete #{first}") if first
358
- rescue Beaneater::NotFoundError
359
- nil
360
- end
361
- end
362
-
363
- it "buries the job when the parser raises an error process! handles itself" do
364
- Beaneater.configure.job_parser = lambda do |body|
365
- raise Beaneater::NotFoundError.new('NOT_FOUND', 'parse') if body == 'not found'
366
- raise Beaneater::TimedOutError.new('TIMED_OUT', 'parse') if body == 'timed out'
367
- body
368
- end
369
- handled = []
370
- @jobs.register('tube_bad_body') do |job|
371
- handled << job.body
372
- @jobs.stop!
373
- end
374
- tube = @beanstalk.tubes.find('tube_bad_body')
375
- not_found = tube.put('not found', :pri => 1, :ttr => 2)[:id]
376
- timed_out = tube.put('timed out', :pri => 2, :ttr => 2)[:id]
377
- tube.put('stop', :pri => 3)
378
-
379
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
380
- assert_equal ['stop'], handled
381
- [not_found, timed_out].each do |id|
382
- stats = stats_of(id)
383
- assert_equal 'buried', stats['state'], "job #{id}"
384
- assert_equal 0, stats['timeouts'], "job #{id}"
385
- end
386
- end
387
-
388
- it "buries the job and stops when the parser raises AbortProcessingError" do
389
- Beaneater.configure.job_parser = lambda { |body| raise Beaneater::AbortProcessingError if body == 'abort'; body }
390
- @jobs.register('tube_bad_body') { |job| }
391
- id = @beanstalk.tubes.find('tube_bad_body').put('abort')[:id]
392
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
393
- assert_equal 'buried', stats_of(id)['state']
394
- end
395
-
396
- it "buries the job when the parser raises a frozen exception" do
397
- frozen = ArgumentError.new('frozen').freeze
398
- Beaneater.configure.job_parser = lambda { |body| body == 'bad' ? raise(frozen) : body }
399
- handled = []
400
- @jobs.register('tube_bad_body') do |job|
401
- handled << job.body
402
- @jobs.stop!
403
- end
404
- tube = @beanstalk.tubes.find('tube_bad_body')
405
- tube.put('bad', :pri => 1)
406
- tube.put('good', :pri => 2)
407
-
408
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
409
- assert_equal ['good'], handled
410
- Beaneater.configure.job_parser = lambda { |body| body }
411
- assert_equal 'bad', (tube.peek(:buried) || flunk("the bad job was not buried")).body
412
- end
413
-
414
- it "buries the handler's job, not the one its own reserve rejected, when a handler raises it" do
415
- cleanup_tubes!(['tube_bad_body_other'])
416
- other_id = @beanstalk.tubes.find('tube_bad_body_other').put('not json')[:id]
417
- @jobs.register('tube_bad_body') do |job|
418
- @jobs.stop!
419
- @beanstalk.tubes.watch('tube_bad_body_other')
420
- @beanstalk.tubes.reserve(0) # raises: the parser rejects the other tube's job
421
- end
422
- @beanstalk.tubes.find('tube_bad_body').put('{"ok":1}')
423
-
424
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
425
- Beaneater.configure.job_parser = lambda { |body| body }
426
- assert_equal '{"ok":1}', (@beanstalk.tubes.find('tube_bad_body').peek(:buried) || flunk("the handler's job was not buried")).body
427
- assert_equal 'reserved', @beanstalk.connection.transmit("stats-job #{other_id}")[:body]['state']
428
- ensure
429
- Beaneater.configure.job_parser = lambda { |body| body }
430
- begin
431
- @beanstalk.connection.transmit("delete #{other_id}") if other_id
432
- rescue Beaneater::NotFoundError
433
- nil
434
- end
435
- cleanup_tubes!(['tube_bad_body_other'])
436
- end
437
- end
438
-
439
- describe "for process! when a handler raises NotFoundError with its job reserved" do
440
- before do
441
- cleanup_tubes!(['tube_not_found'])
442
- end
443
-
444
- after do
445
- cleanup_tubes!(['tube_not_found'])
446
- end
447
-
448
- it "buries the job once instead of reserving in a loop near its TTR" do
449
- runs = 0
450
- @jobs.register('tube_not_found') do |job|
451
- runs += 1
452
- @beanstalk.connection.transmit('delete 999999') # NOT_FOUND for some other job
453
- end
454
- id = @beanstalk.tubes.find('tube_not_found').put('x', :ttr => 2)[:id]
455
- worker = Thread.new { @jobs.process!(:reserve_timeout => 1) }
456
- sleep 3 # past the job's TTR
457
- @jobs.stop!
458
- assert worker.join(5), "process! did not stop"
459
- checker = Beaneater.new('localhost')
460
- stats = checker.connection.transmit("stats-job #{id}")[:body]
461
- checker.close
462
- assert_equal 1, runs
463
- assert_equal 'buried', stats['state']
464
- assert_equal 0, stats['timeouts']
465
- end
466
-
467
- it "goes on when the job stops being reserved just before it is buried" do
468
- @jobs.register('tube_not_found') do |job|
469
- @jobs.stop!
470
- raise 'handler failed'
471
- end
472
- id = @beanstalk.tubes.find('tube_not_found').put('x')[:id]
473
- Beaneater::Job.any_instance.stubs(:bury).raises(Beaneater::JobNotReserved) # another client's reservation ended
474
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) } # must not raise JobNotReserved
475
- assert_nil @jobs.current_job
476
- ensure
477
- Beaneater::Job.any_instance.unstub(:bury)
478
- begin
479
- @beanstalk.connection.transmit("delete #{id}") if id
480
- rescue Beaneater::NotFoundError
481
- nil
482
- end
483
- end
484
-
485
- it "releases the job for another try instead when the error is in retry_on" do
486
- runs = 0
487
- @jobs.register('tube_not_found', :retry_on => [Beaneater::NotFoundError], :max_retries => 1) do |job|
488
- runs += 1
489
- @jobs.stop! if runs == 2
490
- @beanstalk.connection.transmit('delete 999999') # NOT_FOUND for some other job
491
- end
492
- id = @beanstalk.tubes.find('tube_not_found').put('x')[:id]
493
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1, :release_delay => 0) }
494
- stats = @beanstalk.connection.transmit("stats-job #{id}")[:body]
495
- assert_equal 2, runs
496
- assert_equal 1, stats['releases'] # released once, then buried once the retries ran out
497
- assert_equal 'buried', stats['state']
498
- end
499
- end
500
-
501
- describe "for process! when a handler raises NotConnected for another server" do
502
- before do
503
- cleanup_tubes!(['tube_other_down'])
504
- end
505
-
506
- it "buries the job and keeps processing" do
507
- seen = []
508
- @jobs.register('tube_other_down') do |job|
509
- seen << job.body
510
- @jobs.stop! if job.body == 'second'
511
- raise Beaneater::NotConnected, "the other server is down" if job.body == 'first'
512
- end
513
- tube = @beanstalk.tubes.find('tube_other_down')
514
- tube.put('first', :pri => 1)
515
- tube.put('second', :pri => 2)
516
-
517
- Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
518
- assert_equal ['first', 'second'], seen
519
- assert_equal 'first', tube.peek(:buried).body
520
- end
521
- end
522
-
523
- describe "for process! when the server goes away" do
524
- before do
525
- @port = free_port
526
- @pid = start_beanstalkd(@port)
527
- @client = Beaneater.new("127.0.0.1:#{@port}")
528
- end
529
-
530
- after do
531
- @client.close if @client
532
- stop_beanstalkd(@pid) if @pid
533
- end
534
-
535
- it "forgets the job when burying it fails, so the next run buries its own bad job" do
536
- Beaneater.configure.job_parser = lambda do |body|
537
- stop_beanstalkd(@pid) if body == 'kill' # the server dies before the bad job is buried
538
- JSON.parse(body)
539
- end
540
- handled = []
541
- @client.jobs.register('bury_fails') do |job|
542
- handled << job.body
543
- @client.jobs.stop!
544
- end
545
- @client.tubes.find('bury_fails').put('kill')
546
- assert_raises(Beaneater::NotConnected) { Timeout.timeout(10) { @client.jobs.process!(:reserve_timeout => 1) } }
547
- assert_nil @client.jobs.current_job
548
-
549
- @pid = start_beanstalkd(@port) # a new server: job ids start again at 1
550
- producer = Beaneater.new("127.0.0.1:#{@port}")
551
- producer.tubes.find('bury_fails_filler').put('filler') # takes the id the lost job had
552
- producer.tubes.find('bury_fails').put('not json', :pri => 1)
553
- producer.tubes.find('bury_fails').put('{"ok":1}', :pri => 2)
554
- Timeout.timeout(10) { @client.jobs.process!(:reserve_timeout => 1) }
555
- assert_equal [{ 'ok' => 1 }], handled
556
- Beaneater.configure.job_parser = lambda { |body| body }
557
- buried = producer.tubes.find('bury_fails').peek(:buried)
558
- refute_nil buried, "the bad job was not buried"
559
- assert_equal 'not json', buried.body
560
- producer.close
561
- ensure
562
- Beaneater.configure.job_parser = lambda { |body| body }
563
- end
564
-
565
- it "raises NotConnected instead of looping, and the client resumes once the server is back" do
566
- seen = []
567
- @client.jobs.register('reconnect') do |job|
568
- seen << job.body
569
- @client.jobs.stop! if job.body == 'after restart'
570
- end
571
- @client.tubes.find('reconnect').put('before outage')
572
-
573
- worker = Thread.new do
574
- begin
575
- @client.jobs.process!(:reserve_timeout => 1)
576
- rescue StandardError => ex
577
- ex
578
- end
579
- end
580
- Timeout.timeout(5) { sleep 0.05 while seen.empty? }
581
- stop_beanstalkd(@pid)
582
-
583
- finished = worker.join(10)
584
- worker.kill unless finished
585
- assert finished, "process! was still running 10s after the server went away"
586
- assert_kind_of Beaneater::NotConnected, worker.value
587
-
588
- @pid = start_beanstalkd(@port)
589
- producer = Beaneater.new("127.0.0.1:#{@port}")
590
- producer.tubes.find('reconnect').put('after restart')
591
- producer.close
592
-
593
- Timeout.timeout(10) { @client.jobs.process!(:reserve_timeout => 1) }
594
- assert_equal ['before outage', 'after restart'], seen
595
- end
596
- end
597
- 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