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.
@@ -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
data/test/stats_test.rb DELETED
@@ -1,61 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # test/stats_test.rb
4
-
5
- require File.expand_path('../test_helper', __FILE__)
6
-
7
- describe Beaneater::Stats do
8
- before do
9
- connection = stub(:transmit => { :body => { 'uptime' => 1, 'cmd-use' => 2 }, :status => "OK"})
10
- @beanstalk = stub(:connection => connection)
11
- @stats = Beaneater::Stats.new(@beanstalk)
12
- end
13
-
14
- describe 'for #[]' do
15
- it "should return stats by key" do
16
- assert_equal 1, @stats[:uptime]
17
- end
18
-
19
- it "should return stats by underscore key" do
20
- assert_equal 2, @stats[:'cmd_use']
21
- end
22
- end # []
23
-
24
- describe 'for #keys' do
25
- it "should return list of keys" do
26
- assert_equal 2, @stats.keys.size
27
- assert @stats.keys.include?('uptime'), "Expected keys to include 'uptime'"
28
- assert @stats.keys.include?('cmd_use'), "Expected keys to include 'cmd-use'"
29
- end
30
- end # keys
31
-
32
- describe 'for #method_missing' do
33
- it "should return stats by key" do
34
- assert_equal 1, @stats.uptime
35
- end
36
-
37
- it "should return stats by underscore key" do
38
- assert_equal 2, @stats.cmd_use
39
- end
40
-
41
- it "should raise NoMethodError" do
42
- assert_raises(NoMethodError) { @stats.cmd }
43
- end
44
- end # method_missing
45
-
46
- describe 'for #respond_to?' do
47
- before do
48
- connection = stub(:transmit => { :body => { 'max-job-size' => 65535 }, :status => "OK" })
49
- @stats = Beaneater::Stats.new(stub(:connection => connection))
50
- end
51
-
52
- it "should respond to a stats key before it is first read" do
53
- assert @stats.respond_to?(:max_job_size), "Expected stats to respond to max_job_size"
54
- assert_equal 65535, @stats.method(:max_job_size).call
55
- end
56
-
57
- it "should not respond to a name that is not a stats key" do
58
- refute @stats.respond_to?(:no_such_stat)
59
- end
60
- end # respond_to?
61
- end # Beaneater::Stats
data/test/test_helper.rb DELETED
@@ -1,93 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- ENV["TEST"] = 'true'
4
- require 'rubygems'
5
- require 'coveralls'
6
- Coveralls.wear!
7
- require 'minitest/autorun'
8
- $:.unshift File.expand_path("../../lib")
9
- require 'beaneater'
10
- require 'timeout'
11
- begin
12
- require 'mocha/minitest'
13
- rescue LoadError
14
- require 'mocha'
15
- end
16
- require 'json'
17
-
18
- class MiniTest::Unit::TestCase
19
-
20
- # Cleans up all jobs from specific tubes
21
- #
22
- # @example
23
- # cleanup_tubes!(['foo'], @beanstalk)
24
- #
25
- def cleanup_tubes!(tubes, client=nil)
26
- client ||= @beanstalk
27
- tubes.each do |name|
28
- client.tubes.find(name).clear
29
- end
30
- end
31
-
32
- # Cleans up all jobs from all tubes known to the connection
33
- def flush_all(client=nil)
34
- client ||= @beanstalk
35
-
36
- # Do not continue if it is a mock or the connection has been closed
37
- return if !client.is_a?(Beaneater) || !client.connection.connection
38
-
39
- client.tubes.all.each do |tube|
40
- tube.clear
41
- end
42
- end
43
-
44
- # Run clean up after each test to ensure clean state in all tests
45
- def teardown
46
- flush_all
47
- end
48
-
49
- # The beanstalkd binary for tests that stop and restart their own server: BEANSTALKD_BIN, or
50
- # `beanstalkd` on PATH. Skips the test when neither is available.
51
- def beanstalkd_bin
52
- bin = ENV['BEANSTALKD_BIN']
53
- return bin if bin && !bin.empty?
54
- on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR).any? do |dir|
55
- File.executable?(File.join(dir, 'beanstalkd'))
56
- end
57
- skip("beanstalkd not found: put it on PATH or set BEANSTALKD_BIN") unless on_path
58
- 'beanstalkd'
59
- end
60
-
61
- # A free local port for a private beanstalkd
62
- def free_port
63
- server = TCPServer.new('127.0.0.1', 0)
64
- server.addr[1]
65
- ensure
66
- server.close if server
67
- end
68
-
69
- # Starts a private beanstalkd on port and waits until it accepts connections.
70
- #
71
- # @return [Integer] the server's pid
72
- def start_beanstalkd(port)
73
- pid = Process.spawn(beanstalkd_bin, '-l', '127.0.0.1', '-p', port.to_s, [:out, :err] => File::NULL)
74
- 50.times do
75
- begin
76
- TCPSocket.new('127.0.0.1', port).close
77
- return pid
78
- rescue Errno::ECONNREFUSED
79
- sleep 0.05
80
- end
81
- end
82
- stop_beanstalkd(pid)
83
- flunk "beanstalkd did not start on port #{port}"
84
- end
85
-
86
- # Kills a server started by start_beanstalkd
87
- def stop_beanstalkd(pid)
88
- Process.kill('KILL', pid)
89
- Process.wait(pid)
90
- rescue Errno::ESRCH, Errno::ECHILD
91
- nil
92
- end
93
- end
data/test/tube_test.rb DELETED
@@ -1,323 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # test/tube_test.rb
4
-
5
- require File.expand_path('../test_helper', __FILE__)
6
-
7
- describe Beaneater::Tube do
8
- before do
9
- @beanstalk = Beaneater.new('localhost')
10
- @tube = Beaneater::Tube.new(@beanstalk, 'baz')
11
- end
12
-
13
- describe "for #put" do
14
- before do
15
- @time = Time.now.to_i
16
- end
17
-
18
- it "should insert a job" do
19
- @tube.put "bar put #{@time}"
20
- assert_equal "bar put #{@time}", @tube.peek(:ready).body
21
- assert_equal 120, @tube.peek(:ready).ttr
22
- assert_equal 65536, @tube.peek(:ready).pri
23
- assert_equal 0, @tube.peek(:ready).delay
24
- end
25
-
26
- it "should insert a delayed job" do
27
- @tube.put "delayed put #{@time}", :delay => 1
28
- assert_equal "delayed put #{@time}", @tube.peek(:delayed).body
29
- end
30
-
31
- it "should support custom serializer" do
32
- Beaneater.configure.job_serializer = lambda { |b| JSON.dump(b) }
33
- @tube.put({ foo: "bar"})
34
- assert_equal '{"foo":"bar"}', @tube.peek(:ready).body
35
- end
36
-
37
- after do
38
- Beaneater::Connection.any_instance.unstub(:transmit)
39
- Beaneater.configure.job_serializer = lambda { |b| b }
40
- end
41
- end # put
42
-
43
- describe "for #peek" do
44
- before do
45
- @time = Time.now.to_i
46
- end
47
-
48
- it "should peek delayed" do
49
- @tube.put "foo delay #{@time}", :delay => 1
50
- assert_equal "foo delay #{@time}", @tube.peek(:delayed).body
51
- end
52
-
53
- it "should peek ready" do
54
- @tube.put "foo ready #{@time}", :delay => 0
55
- assert_equal "foo ready #{@time}", @tube.peek(:ready).body
56
- end
57
-
58
- it "should peek buried" do
59
- @tube.put "foo buried #{@time}"
60
- @tube.reserve.bury
61
-
62
- assert_equal "foo buried #{@time}", @tube.peek(:buried).body
63
- end
64
-
65
- it "should return nil for empty peek" do
66
- assert_nil @tube.peek(:buried)
67
- end
68
-
69
- it "supports valid JSON" do
70
- json = '{ "foo" : "bar" }'
71
- @tube.put(json)
72
- assert_equal 'bar', JSON.parse(@tube.peek(:ready).body)['foo']
73
- end
74
-
75
- it "supports non valid JSON" do
76
- json = '{"message":"hi"}'
77
- @tube.put(json)
78
- assert_equal 'hi', JSON.parse(@tube.peek(:ready).body)['message']
79
- end
80
-
81
- it "supports passing crlf through" do
82
- @tube.put("\r\n")
83
- assert_equal "\r\n", @tube.peek(:ready).body
84
- end
85
-
86
- it "supports passing any byte value through" do
87
- bytes = (0..255).to_a.pack("c*")
88
- @tube.put(bytes)
89
- assert_equal bytes, @tube.peek(:ready).body
90
- end
91
-
92
- it "should support custom parser" do
93
- Beaneater.configure.job_parser = lambda { |b| JSON.parse(b) }
94
- json = '{"message": "hi: there! this: is a long message"}'
95
- @tube.put(json)
96
- assert_equal 'hi: there! this: is a long message', @tube.peek(:ready).body['message']
97
- end
98
-
99
- it "should keep the connection usable when the custom parser raises" do
100
- Beaneater.configure.job_parser = lambda { |b| JSON.parse(b) }
101
- @tube.put('not json')
102
- socket = @beanstalk.connection.connection
103
- assert_raises(JSON::ParserError) { @tube.peek(:ready) }
104
- assert_same socket, @beanstalk.connection.connection
105
- assert_equal 'baz', @beanstalk.tubes.used.name
106
- end
107
-
108
- after do
109
- Beaneater.configure.job_parser = lambda { |b| b }
110
- end
111
- end # peek
112
-
113
- describe "for a job_parser that raises on reserve" do
114
- before do
115
- Beaneater.configure.job_parser = lambda { |b| JSON.parse(b) }
116
- @tube = @beanstalk.tubes.find('poison_tube')
117
- @poison_id = @tube.put('not json', :pri => 1)[:id]
118
- @tube.put('{"ok":1}', :pri => 2)
119
- @beanstalk.tubes.watch!('poison_tube')
120
- end
121
-
122
- after do
123
- Beaneater.configure.job_parser = lambda { |b| b }
124
- # The bad job is still reserved by this connection, which clearing the tube can't touch;
125
- # left alone, it would be released when the socket is collected, into a later test.
126
- begin
127
- @beanstalk.connection.transmit("delete #{@poison_id}")
128
- rescue Beaneater::NotFoundError
129
- nil
130
- end
131
- cleanup_tubes!(['poison_tube'])
132
- end
133
-
134
- it "should raise the parser's own error from reserve, tagged with the job id" do
135
- error = assert_raises(JSON::ParserError) { @beanstalk.tubes.reserve(0) }
136
- assert_kind_of Beaneater::JobParseFailure, error
137
- assert_equal @poison_id, error.job_id
138
- assert_nil error.cause # the parser's error had none
139
- Beaneater.configure.job_parser = lambda { |b| b }
140
- assert_equal 'reserved', @beanstalk.connection.transmit("stats-job #{error.job_id}")[:body]['state']
141
- end
142
-
143
- it "should keep the parser's error intact: class, message, backtrace and cause" do
144
- original = nil
145
- Beaneater.configure.job_parser = lambda do |b|
146
- begin
147
- JSON.parse(b)
148
- rescue JSON::ParserError => ex
149
- original = ex
150
- raise ArgumentError, 'not a job' # its cause is the ParserError
151
- end
152
- end
153
- error = assert_raises(ArgumentError) { @beanstalk.tubes.reserve(0) }
154
- assert_equal @poison_id, error.job_id
155
- assert_equal 'not a job', error.message
156
- assert_same original, error.cause
157
- assert_operator error.backtrace.first, :include?, 'tube_test.rb'
158
- refute_nil error.backtrace_locations
159
- end
160
-
161
- # An exception class that can't be copied on any Ruby (clone calls initialize_copy)
162
- class UncopyableError < StandardError
163
- def initialize_copy(_original)
164
- raise TypeError, "can't copy"
165
- end
166
- end
167
-
168
- # An exception class whose #cause is not an exception
169
- class OddCauseError < StandardError
170
- def cause
171
- 'not an exception'
172
- end
173
- end
174
-
175
- it "should raise the parser's own exception, untagged, when it can't be copied" do
176
- Beaneater.configure.job_parser = lambda { |b| raise UncopyableError, 'no copies' }
177
- error = assert_raises(UncopyableError) { @beanstalk.tubes.reserve(0) }
178
- assert_equal 'no copies', error.message
179
- refute_kind_of Beaneater::JobParseFailure, error
180
- end
181
-
182
- it "should raise a frozen exception that can't be copied as it is" do
183
- frozen = UncopyableError.new('frozen, no copies').freeze
184
- Beaneater.configure.job_parser = lambda { |b| raise frozen }
185
- error = assert_raises(UncopyableError) { @beanstalk.tubes.reserve(0) }
186
- assert_same frozen, error
187
- assert_nil error.cause
188
- end
189
-
190
- it "should tag the copy when the exception's cause is not an exception" do
191
- Beaneater.configure.job_parser = lambda { |b| raise OddCauseError, 'odd' }
192
- error = assert_raises(OddCauseError) { @beanstalk.tubes.reserve(0) }
193
- assert_equal @poison_id, error.job_id
194
- end
195
-
196
- it "should not tag an exception the parser reuses, so a later error carries no old id" do
197
- shared = JSON::ParserError.new('always this one')
198
- Beaneater.configure.job_parser = lambda { |b| raise shared }
199
- error = assert_raises(JSON::ParserError) { @beanstalk.tubes.reserve(0) }
200
- assert_equal @poison_id, error.job_id
201
- peeked = assert_raises(JSON::ParserError) { @tube.peek(:ready) } # not a reserve: not tagged
202
- refute_kind_of Beaneater::JobParseFailure, peeked
203
- refute_kind_of Beaneater::JobParseFailure, shared
204
- end
205
-
206
- it "should keep the connection, so the bad job stays reserved and the next job is delivered" do
207
- socket = @beanstalk.connection.connection
208
- assert_raises(JSON::ParserError) { @beanstalk.tubes.reserve(0) }
209
- assert_same socket, @beanstalk.connection.connection
210
- job = @beanstalk.tubes.reserve(0)
211
- assert_equal({ 'ok' => 1 }, job.body)
212
- job.delete
213
- end
214
-
215
- it "should keep the connection when the raising parser used the same client first" do
216
- tube = 'poison_tube'
217
- Beaneater.configure.job_parser = lambda { |b| @beanstalk.connection.transmit("use #{tube}"); JSON.parse(b) }
218
- socket = @beanstalk.connection.connection
219
- assert_raises(JSON::ParserError) { @beanstalk.tubes.reserve(0) }
220
- assert_same socket, @beanstalk.connection.connection
221
- job = @beanstalk.tubes.reserve(0)
222
- assert_equal({ 'ok' => 1 }, job.body)
223
- job.delete
224
- end
225
- end # job_parser that raises on reserve
226
-
227
- describe "for #reserve" do
228
- before do
229
- @time = Time.now.to_i
230
- @json = %Q&{ "foo" : "#{@time} bar" }&
231
- @tube.put @json
232
- end
233
-
234
- it "should reserve job" do
235
- @job = @tube.reserve
236
- assert_equal "#{@time} bar", JSON.parse(@job.body)['foo']
237
- @job.delete
238
- end
239
-
240
- it "should reserve job with block" do
241
- job = nil
242
- @tube.reserve { |j| job = j; job.delete }
243
- assert_equal "#{@time} bar", JSON.parse(job.body)['foo']
244
- end
245
-
246
- it "should support custom parser" do
247
- Beaneater.configure.job_parser = lambda { |b| JSON.parse(b) }
248
- @job = @tube.reserve
249
- assert_equal "#{@time} bar", @job.body['foo']
250
- @job.delete
251
- end
252
-
253
- after do
254
- Beaneater.configure.job_parser = lambda { |b| b }
255
- end
256
- end # reserve
257
-
258
- describe "for #pause" do
259
- before do
260
- @time = Time.now.to_i
261
- @tube = Beaneater::Tube.new(@beanstalk, 'bam')
262
- @tube.put "foo pause #{@time}"
263
- end
264
-
265
- it "should allow tube pause" do
266
- assert_equal 0, @tube.stats.pause
267
- @tube.pause(1)
268
- assert_equal 1, @tube.stats.pause
269
- end
270
- end # pause
271
-
272
- describe "for #stats" do
273
- before do
274
- @time = Time.now.to_i
275
- @tube.put "foo stats #{@time}"
276
- @stats = @tube.stats
277
- end
278
-
279
- it "should return total number of jobs in tube" do
280
- assert_equal 1, @stats['current_jobs_ready']
281
- assert_equal 0, @stats.current_jobs_delayed
282
- end
283
-
284
- it "should raise error for empty tube" do
285
- assert_raises(Beaneater::NotFoundError) { @beanstalk.tubes.find('fake_tube').stats }
286
- end
287
- end # stats
288
-
289
- describe "for #kick" do
290
- before do
291
- @time, @time2 = 2.times.map { Time.now.to_i }
292
- @tube.put "kick #{@time}"
293
- @tube.put "kick #{@time2}"
294
-
295
- 2.times.map { @tube.reserve.bury }
296
- end
297
-
298
- it "should kick 2 buried jobs" do
299
- assert_equal 2, @tube.stats.current_jobs_buried
300
- @tube.kick(2)
301
- assert_equal 0, @tube.stats.current_jobs_buried
302
- assert_equal 2, @tube.stats.current_jobs_ready
303
- end
304
- end # kick
305
-
306
- describe "for #clear" do
307
- @time = Time.now.to_i
308
- before do
309
- 2.times { |i| @tube.put "to clear success #{i} #{@time}" }
310
- 2.times { |i| @tube.put "to clear delayed #{i} #{@time}", :delay => 5 }
311
- 2.times { |i| @tube.put "to clear bury #{i} #{@time}", :pri => 1 }
312
- @tube.reserve.bury while @tube.peek(:ready).stats['pri'] == 1
313
- end
314
-
315
- it "should clear all jobs in tube" do
316
- tube_counts = lambda { %w(ready buried delayed).map { |s| @tube.stats["current_jobs_#{s}"] } }
317
- assert_equal [2, 2, 2], tube_counts.call
318
- @tube.clear
319
- @tube.stats
320
- assert_equal [0, 0, 0], tube_counts.call
321
- end
322
- end # clear
323
- end # Beaneater::Tube