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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +3 -1
- data/lib/beaneater/connection.rb +5 -4
- data/lib/beaneater/job/collection.rb +2 -0
- data/lib/beaneater/stats/stat_struct.rb +11 -2
- data/lib/beaneater/version.rb +1 -1
- metadata +3 -34
- data/.github/workflows/ruby.yml +0 -46
- data/.gitignore +0 -17
- data/Gemfile +0 -16
- data/Rakefile +0 -90
- data/TODO +0 -1
- data/beaneater.gemspec +0 -33
- data/examples/demo.rb +0 -97
- data/test/arguments_test.rb +0 -174
- data/test/beaneater_test.rb +0 -112
- data/test/connection_test.rb +0 -749
- data/test/errors_test.rb +0 -35
- data/test/job_test.rb +0 -286
- data/test/jobs_test.rb +0 -549
- data/test/prompt_regexp_test.rb +0 -59
- data/test/stat_struct_test.rb +0 -83
- data/test/stats_test.rb +0 -61
- data/test/test_helper.rb +0 -93
- data/test/tube_test.rb +0 -323
- data/test/tubes_test.rb +0 -194
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
|
data/test/tubes_test.rb
DELETED
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
# test/tubes_test.rb
|
|
4
|
-
|
|
5
|
-
require File.expand_path('../test_helper', __FILE__)
|
|
6
|
-
|
|
7
|
-
describe Beaneater::Tubes do
|
|
8
|
-
describe "for #find" do
|
|
9
|
-
before do
|
|
10
|
-
@beanstalk = stub
|
|
11
|
-
@tubes = Beaneater::Tubes.new(@beanstalk)
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
it("should return Tube obj") { assert_kind_of Beaneater::Tube, @tubes.find(:foo) }
|
|
15
|
-
it("should return Tube name") { assert_equal "foo", @tubes.find(:foo).name }
|
|
16
|
-
it("should support hash syntax") { assert_equal "bar", @tubes["bar"].name }
|
|
17
|
-
end # find
|
|
18
|
-
|
|
19
|
-
describe "for #use" do
|
|
20
|
-
before do
|
|
21
|
-
@beanstalk = Beaneater.new('localhost')
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
it "should switch to used tube for valid name" do
|
|
25
|
-
Beaneater::Tube.new(@beanstalk, 'some_name')
|
|
26
|
-
@beanstalk.tubes.use('some_name')
|
|
27
|
-
assert_equal 'some_name', @beanstalk.tubes.used.name
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
it "should raise for invalid tube name" do
|
|
31
|
-
assert_raises(Beaneater::InvalidTubeName) { @beanstalk.tubes.use('; ') }
|
|
32
|
-
end
|
|
33
|
-
end # use
|
|
34
|
-
|
|
35
|
-
describe "for #watch & #watched" do
|
|
36
|
-
before do
|
|
37
|
-
@beanstalk = Beaneater.new('localhost')
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
it 'should watch specified tubes' do
|
|
41
|
-
@beanstalk.tubes.watch('foo')
|
|
42
|
-
@beanstalk.tubes.watch('bar')
|
|
43
|
-
assert_equal ['default', 'foo', 'bar'].sort, @beanstalk.tubes.watched.map(&:name).sort
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
it 'should raise invalid name for bad tube' do
|
|
47
|
-
assert_raises(Beaneater::InvalidTubeName) { @beanstalk.tubes.watch('; ') }
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
it 'should remember each watched tube once, by name, for a reconnect to restore' do
|
|
51
|
-
@beanstalk.tubes.watch('a')
|
|
52
|
-
@beanstalk.tubes.watch('a')
|
|
53
|
-
@beanstalk.tubes.watch(:a)
|
|
54
|
-
assert_equal ['default', 'a'], @beanstalk.connection.tubes_watched
|
|
55
|
-
end
|
|
56
|
-
end # watch! & watched
|
|
57
|
-
|
|
58
|
-
describe "for #all" do
|
|
59
|
-
before do
|
|
60
|
-
@beanstalk = Beaneater.new('localhost')
|
|
61
|
-
@beanstalk.tubes.find('foo').put 'bar'
|
|
62
|
-
@beanstalk.tubes.find('bar').put 'foo'
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
it 'should retrieve all tubes' do
|
|
66
|
-
['default', 'foo', 'bar'].each do |t|
|
|
67
|
-
assert @beanstalk.tubes.all.map(&:name).include?(t)
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
it 'should return tube names that look like numbers or dates as strings' do
|
|
72
|
-
names = ['1.10', '2026-10-30', 'true', 'a-b']
|
|
73
|
-
names.each { |t| @beanstalk.tubes.find(t).put 'x' }
|
|
74
|
-
all = @beanstalk.tubes.all.map(&:name)
|
|
75
|
-
names.each { |t| assert_includes all, t }
|
|
76
|
-
end
|
|
77
|
-
end # all
|
|
78
|
-
|
|
79
|
-
describe "for Enumerable" do
|
|
80
|
-
before do
|
|
81
|
-
@beanstalk = Beaneater.new('localhost')
|
|
82
|
-
@beanstalk.tubes.find('foo').put 'bar'
|
|
83
|
-
@beanstalk.tubes.find('bar').put 'foo'
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
it 'should map tubes' do
|
|
87
|
-
['default', 'foo', 'bar'].each do |t|
|
|
88
|
-
assert @beanstalk.tubes.map(&:name).include?(t)
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
describe "for #used" do
|
|
94
|
-
before do
|
|
95
|
-
@beanstalk = Beaneater.new('localhost')
|
|
96
|
-
@beanstalk.tubes.find('foo').put 'bar'
|
|
97
|
-
@beanstalk.tubes.find('bar').put 'foo'
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
it 'should retrieve used tube' do
|
|
101
|
-
assert_equal 'bar', @beanstalk.tubes.used.name
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
it 'should support dashed tubes' do
|
|
105
|
-
@beanstalk.tubes.find('der-bam').put 'foo'
|
|
106
|
-
assert_equal 'der-bam', @beanstalk.tubes.used.name
|
|
107
|
-
end
|
|
108
|
-
end # used
|
|
109
|
-
|
|
110
|
-
describe "for #watch!" do
|
|
111
|
-
before do
|
|
112
|
-
@beanstalk = Beaneater.new('localhost')
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
it 'should watch specified tubes' do
|
|
116
|
-
@beanstalk.tubes.watch!(:foo)
|
|
117
|
-
@beanstalk.tubes.watch!('bar')
|
|
118
|
-
assert_equal ['bar'].sort, @beanstalk.tubes.watched.map(&:name).sort
|
|
119
|
-
end
|
|
120
|
-
end # watch!
|
|
121
|
-
|
|
122
|
-
describe "for #ignore" do
|
|
123
|
-
before do
|
|
124
|
-
@beanstalk = Beaneater.new('localhost')
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
it 'should ignore specified tubes' do
|
|
128
|
-
@beanstalk.tubes.watch('foo')
|
|
129
|
-
@beanstalk.tubes.watch('bar')
|
|
130
|
-
@beanstalk.tubes.ignore('foo')
|
|
131
|
-
assert_equal ['default', 'bar'].sort, @beanstalk.tubes.watched.map(&:name).sort
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
it 'should forget an ignored tube however its name is given' do
|
|
135
|
-
@beanstalk.tubes.watch('foo')
|
|
136
|
-
@beanstalk.tubes.watch('bar')
|
|
137
|
-
@beanstalk.tubes.ignore(:foo)
|
|
138
|
-
assert_equal ['default', 'bar'], @beanstalk.connection.tubes_watched
|
|
139
|
-
end
|
|
140
|
-
end # ignore
|
|
141
|
-
|
|
142
|
-
describe "for #reserve" do
|
|
143
|
-
before do
|
|
144
|
-
@beanstalk = Beaneater.new('localhost')
|
|
145
|
-
@tube = @beanstalk.tubes.find 'tube'
|
|
146
|
-
@time = Time.now.to_i
|
|
147
|
-
@tube.put "foo reserve #{@time}"
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
it("should reserve job") do
|
|
151
|
-
@beanstalk.tubes.watch 'tube'
|
|
152
|
-
job = @beanstalk.tubes.reserve
|
|
153
|
-
assert_equal "foo reserve #{@time}", job.body
|
|
154
|
-
job.delete
|
|
155
|
-
end
|
|
156
|
-
|
|
157
|
-
it("should reserve job with block") do
|
|
158
|
-
@beanstalk.tubes.watch 'tube'
|
|
159
|
-
job = nil
|
|
160
|
-
@beanstalk.tubes.reserve { |j| job = j; job.delete }
|
|
161
|
-
assert_equal "foo reserve #{@time}", job.body
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
it("should reserve job with block and timeout") do
|
|
165
|
-
@beanstalk.tubes.watch 'tube'
|
|
166
|
-
job = nil
|
|
167
|
-
@beanstalk.tubes.reserve(0) { |j| job = j; job.delete }
|
|
168
|
-
assert_equal "foo reserve #{@time}", job.body
|
|
169
|
-
end
|
|
170
|
-
|
|
171
|
-
it "should raise TimedOutError with timeout" do
|
|
172
|
-
@beanstalk.tubes.watch 'tube'
|
|
173
|
-
@beanstalk.tubes.reserve(0) { |j| job = j; job.delete }
|
|
174
|
-
assert_raises(Beaneater::TimedOutError) { @beanstalk.tubes.reserve(0) }
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
it "should raise DeadlineSoonError with ttr 1" do
|
|
178
|
-
@tube.reserve.delete
|
|
179
|
-
@tube.put "foo reserve #{@time}", :ttr => 1
|
|
180
|
-
@beanstalk.tubes.watch 'tube'
|
|
181
|
-
job = @beanstalk.tubes.reserve
|
|
182
|
-
assert_raises(Beaneater::DeadlineSoonError) { @beanstalk.tubes.reserve(0) }
|
|
183
|
-
ensure
|
|
184
|
-
# left reserved, the job would be released (TTR, or the socket being collected) into a
|
|
185
|
-
# later #reserve test
|
|
186
|
-
begin
|
|
187
|
-
job.delete if job
|
|
188
|
-
rescue Beaneater::NotFoundError
|
|
189
|
-
nil
|
|
190
|
-
end
|
|
191
|
-
end
|
|
192
|
-
|
|
193
|
-
end # reserve
|
|
194
|
-
end # Beaneater::Tubes
|