beaneater 0.3.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,8 +4,8 @@ require File.expand_path('../test_helper', __FILE__)
4
4
 
5
5
  describe Beaneater::Tube do
6
6
  before do
7
- @pool = Beaneater::Pool.new(['localhost'])
8
- @tube = Beaneater::Tube.new(@pool, 'baz')
7
+ @beanstalk = Beaneater.new('localhost')
8
+ @tube = Beaneater::Tube.new(@beanstalk, 'baz')
9
9
  end
10
10
 
11
11
  describe "for #put" do
@@ -26,22 +26,15 @@ describe Beaneater::Tube do
26
26
  assert_equal "delayed put #{@time}", @tube.peek(:delayed).body
27
27
  end
28
28
 
29
- it "should try to put 2 times before put successfully" do
30
- Beaneater::Connection.any_instance.expects(:transmit).once.with(includes('use baz'), {})
31
- Beaneater::Connection.any_instance.expects(:transmit).times(2).with(includes("bar put #{@time}"), {}).
32
- raises(Beaneater::DrainingError.new(nil, nil)).then.returns('foo')
33
- assert_equal 'foo', @tube.put("bar put #{@time}")
34
- end
35
-
36
- it "should try to put 3 times before to raise" do
37
- Beaneater::Connection.any_instance.expects(:transmit).once.with(includes('use baz'), {})
38
- Beaneater::Connection.any_instance.expects(:transmit).with(includes("bar put #{@time}"), {}).
39
- times(3).raises(Beaneater::DrainingError.new(nil, nil))
40
- assert_raises(Beaneater::DrainingError) { @tube.put "bar put #{@time}" }
29
+ it "should support custom serializer" do
30
+ Beaneater.configure.job_serializer = lambda { |b| JSON.dump(b) }
31
+ @tube.put({ foo: "bar"})
32
+ assert_equal '{"foo":"bar"}', @tube.peek(:ready).body
41
33
  end
42
34
 
43
35
  after do
44
36
  Beaneater::Connection.any_instance.unstub(:transmit)
37
+ Beaneater.configure.job_serializer = lambda { |b| b }
45
38
  end
46
39
  end # put
47
40
 
@@ -140,7 +133,7 @@ describe Beaneater::Tube do
140
133
  describe "for #pause" do
141
134
  before do
142
135
  @time = Time.now.to_i
143
- @tube = Beaneater::Tube.new(@pool, 'bam')
136
+ @tube = Beaneater::Tube.new(@beanstalk, 'bam')
144
137
  @tube.put "foo pause #{@time}"
145
138
  end
146
139
 
@@ -164,7 +157,7 @@ describe Beaneater::Tube do
164
157
  end
165
158
 
166
159
  it "should raise error for empty tube" do
167
- assert_raises(Beaneater::NotFoundError) { @pool.tubes.find('fake_tube').stats }
160
+ assert_raises(Beaneater::NotFoundError) { @beanstalk.tubes.find('fake_tube').stats }
168
161
  end
169
162
  end # stats
170
163
 
@@ -202,8 +195,4 @@ describe Beaneater::Tube do
202
195
  assert_equal [0, 0, 0], tube_counts.call
203
196
  end
204
197
  end # clear
205
-
206
- after do
207
- cleanup_tubes!(['baz'])
208
- end
209
198
  end # Beaneater::Tube
@@ -5,8 +5,8 @@ require File.expand_path('../test_helper', __FILE__)
5
5
  describe Beaneater::Tubes do
6
6
  describe "for #find" do
7
7
  before do
8
- @pool = stub
9
- @tubes = Beaneater::Tubes.new(@pool)
8
+ @beanstalk = stub
9
+ @tubes = Beaneater::Tubes.new(@beanstalk)
10
10
  end
11
11
 
12
12
  it("should return Tube obj") { assert_kind_of Beaneater::Tube, @tubes.find(:foo) }
@@ -16,143 +16,148 @@ describe Beaneater::Tubes do
16
16
 
17
17
  describe "for #use" do
18
18
  before do
19
- @pool = Beaneater::Pool.new(['localhost'])
19
+ @beanstalk = Beaneater.new('localhost')
20
20
  end
21
21
 
22
22
  it "should switch to used tube for valid name" do
23
- tube = Beaneater::Tube.new(@pool, 'some_name')
24
- @pool.tubes.use('some_name')
25
- assert_equal 'some_name', @pool.tubes.used.name
23
+ tube = Beaneater::Tube.new(@beanstalk, 'some_name')
24
+ @beanstalk.tubes.use('some_name')
25
+ assert_equal 'some_name', @beanstalk.tubes.used.name
26
26
  end
27
27
 
28
28
  it "should raise for invalid tube name" do
29
- assert_raises(Beaneater::InvalidTubeName) { @pool.tubes.use('; ') }
29
+ assert_raises(Beaneater::InvalidTubeName) { @beanstalk.tubes.use('; ') }
30
30
  end
31
31
  end # use
32
32
 
33
33
  describe "for #watch & #watched" do
34
34
  before do
35
- @pool = Beaneater::Pool.new(['localhost'])
35
+ @beanstalk = Beaneater.new('localhost')
36
36
  end
37
37
 
38
38
  it 'should watch specified tubes' do
39
- @pool.tubes.watch('foo')
40
- @pool.tubes.watch('bar')
41
- assert_equal ['default', 'foo', 'bar'].sort, @pool.tubes.watched.map(&:name).sort
39
+ @beanstalk.tubes.watch('foo')
40
+ @beanstalk.tubes.watch('bar')
41
+ assert_equal ['default', 'foo', 'bar'].sort, @beanstalk.tubes.watched.map(&:name).sort
42
42
  end
43
43
 
44
44
  it 'should raise invalid name for bad tube' do
45
- assert_raises(Beaneater::InvalidTubeName) { @pool.tubes.watch('; ') }
45
+ assert_raises(Beaneater::InvalidTubeName) { @beanstalk.tubes.watch('; ') }
46
46
  end
47
47
  end # watch! & watched
48
48
 
49
49
  describe "for #all" do
50
50
  before do
51
- @pool = Beaneater::Pool.new(['localhost'])
52
- @pool.tubes.find('foo').put 'bar'
53
- @pool.tubes.find('bar').put 'foo'
51
+ @beanstalk = Beaneater.new('localhost')
52
+ @beanstalk.tubes.find('foo').put 'bar'
53
+ @beanstalk.tubes.find('bar').put 'foo'
54
54
  end
55
55
 
56
56
  it 'should retrieve all tubes' do
57
57
  ['default', 'foo', 'bar'].each do |t|
58
- assert @pool.tubes.all.map(&:name).include?(t)
58
+ assert @beanstalk.tubes.all.map(&:name).include?(t)
59
59
  end
60
60
  end
61
61
  end # all
62
62
 
63
+ describe "for Enumerable" do
64
+ before do
65
+ @beanstalk = Beaneater.new('localhost')
66
+ @beanstalk.tubes.find('foo').put 'bar'
67
+ @beanstalk.tubes.find('bar').put 'foo'
68
+ end
69
+
70
+ it 'should map tubes' do
71
+ ['default', 'foo', 'bar'].each do |t|
72
+ assert @beanstalk.tubes.map(&:name).include?(t)
73
+ end
74
+ end
75
+ end
76
+
63
77
  describe "for #used" do
64
78
  before do
65
- @pool = Beaneater::Pool.new(['localhost'])
66
- @pool.tubes.find('foo').put 'bar'
67
- @pool.tubes.find('bar').put 'foo'
79
+ @beanstalk = Beaneater.new('localhost')
80
+ @beanstalk.tubes.find('foo').put 'bar'
81
+ @beanstalk.tubes.find('bar').put 'foo'
68
82
  end
69
83
 
70
84
  it 'should retrieve used tube' do
71
- assert_equal 'bar', @pool.tubes.used.name
85
+ assert_equal 'bar', @beanstalk.tubes.used.name
72
86
  end
73
87
 
74
88
  it 'should support dashed tubes' do
75
- @pool.tubes.find('der-bam').put 'foo'
76
- assert_equal 'der-bam', @pool.tubes.used.name
89
+ @beanstalk.tubes.find('der-bam').put 'foo'
90
+ assert_equal 'der-bam', @beanstalk.tubes.used.name
77
91
  end
78
92
  end # used
79
93
 
80
94
  describe "for #watch!" do
81
95
  before do
82
- @pool = Beaneater::Pool.new(['localhost'])
96
+ @beanstalk = Beaneater.new('localhost')
83
97
  end
84
98
 
85
99
  it 'should watch specified tubes' do
86
- @pool.tubes.watch!(:foo)
87
- @pool.tubes.watch!('bar')
88
- assert_equal ['bar'].sort, @pool.tubes.watched.map(&:name).sort
100
+ @beanstalk.tubes.watch!(:foo)
101
+ @beanstalk.tubes.watch!('bar')
102
+ assert_equal ['bar'].sort, @beanstalk.tubes.watched.map(&:name).sort
89
103
  end
90
104
  end # watch!
91
105
 
92
106
  describe "for #ignore" do
93
107
  before do
94
- @pool = Beaneater::Pool.new(['localhost'])
108
+ @beanstalk = Beaneater.new('localhost')
95
109
  end
96
110
 
97
111
  it 'should ignore specified tubes' do
98
- @pool.tubes.watch('foo')
99
- @pool.tubes.watch('bar')
100
- @pool.tubes.ignore('foo')
101
- assert_equal ['default', 'bar'].sort, @pool.tubes.watched.map(&:name).sort
112
+ @beanstalk.tubes.watch('foo')
113
+ @beanstalk.tubes.watch('bar')
114
+ @beanstalk.tubes.ignore('foo')
115
+ assert_equal ['default', 'bar'].sort, @beanstalk.tubes.watched.map(&:name).sort
102
116
  end
103
117
  end # ignore
104
118
 
105
119
  describe "for #reserve" do
106
120
  before do
107
- @pool = Beaneater::Pool.new(['localhost'])
108
- @tube = @pool.tubes.find 'tube'
121
+ @beanstalk = Beaneater.new('localhost')
122
+ @tube = @beanstalk.tubes.find 'tube'
109
123
  @time = Time.now.to_i
110
124
  @tube.put "foo reserve #{@time}"
111
125
  end
112
126
 
113
127
  it("should reserve job") do
114
- @pool.tubes.watch 'tube'
115
- job = @pool.tubes.reserve
128
+ @beanstalk.tubes.watch 'tube'
129
+ job = @beanstalk.tubes.reserve
116
130
  assert_equal "foo reserve #{@time}", job.body
117
131
  job.delete
118
132
  end
119
133
 
120
134
  it("should reserve job with block") do
121
- @pool.tubes.watch 'tube'
135
+ @beanstalk.tubes.watch 'tube'
122
136
  job = nil
123
- @pool.tubes.reserve { |j| job = j; job.delete }
137
+ @beanstalk.tubes.reserve { |j| job = j; job.delete }
124
138
  assert_equal "foo reserve #{@time}", job.body
125
139
  end
126
140
 
127
141
  it("should reserve job with block and timeout") do
128
- @pool.tubes.watch 'tube'
142
+ @beanstalk.tubes.watch 'tube'
129
143
  job = nil
130
- res = @pool.tubes.reserve(0) { |j| job = j; job.delete }
144
+ res = @beanstalk.tubes.reserve(0) { |j| job = j; job.delete }
131
145
  assert_equal "foo reserve #{@time}", job.body
132
146
  end
133
147
 
134
148
  it "should raise TimedOutError with timeout" do
135
- @pool.tubes.watch 'tube'
136
- @pool.tubes.reserve(0) { |j| job = j; job.delete }
137
- assert_raises(Beaneater::TimedOutError) { @pool.tubes.reserve(0) }
138
- end
139
-
140
- it "should raise TimedOutError no delete, with timeout" do
141
- @pool.tubes.watch 'tube'
142
- @pool.tubes.reserve(0) { |j| job = j; job.delete }
143
- assert_raises(Beaneater::TimedOutError) { @pool.tubes.reserve(0) }
149
+ @beanstalk.tubes.watch 'tube'
150
+ @beanstalk.tubes.reserve(0) { |j| job = j; job.delete }
151
+ assert_raises(Beaneater::TimedOutError) { @beanstalk.tubes.reserve(0) }
144
152
  end
145
153
 
146
154
  it "should raise DeadlineSoonError with ttr 1" do
147
155
  @tube.reserve.delete
148
156
  @tube.put "foo reserve #{@time}", :ttr => 1
149
- @pool.tubes.watch 'tube'
150
- @pool.tubes.reserve
151
- assert_raises(Beaneater::DeadlineSoonError) { @pool.tubes.reserve(0) }
157
+ @beanstalk.tubes.watch 'tube'
158
+ @beanstalk.tubes.reserve
159
+ assert_raises(Beaneater::DeadlineSoonError) { @beanstalk.tubes.reserve(0) }
152
160
  end
153
161
 
154
- after do
155
- cleanup_tubes!(['foo', 'tube'])
156
- end
157
162
  end # reserve
158
163
  end # Beaneater::Tubes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beaneater
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nico Taing
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-17 00:00:00.000000000 Z
11
+ date: 2015-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -105,8 +105,6 @@ files:
105
105
  - lib/beaneater/job.rb
106
106
  - lib/beaneater/job/collection.rb
107
107
  - lib/beaneater/job/record.rb
108
- - lib/beaneater/pool.rb
109
- - lib/beaneater/pool_command.rb
110
108
  - lib/beaneater/stats.rb
111
109
  - lib/beaneater/stats/fast_struct.rb
112
110
  - lib/beaneater/stats/stat_struct.rb
@@ -119,8 +117,6 @@ files:
119
117
  - test/errors_test.rb
120
118
  - test/job_test.rb
121
119
  - test/jobs_test.rb
122
- - test/pool_command_test.rb
123
- - test/pool_test.rb
124
120
  - test/prompt_regexp_test.rb
125
121
  - test/stat_struct_test.rb
126
122
  - test/stats_test.rb
@@ -157,8 +153,6 @@ test_files:
157
153
  - test/errors_test.rb
158
154
  - test/job_test.rb
159
155
  - test/jobs_test.rb
160
- - test/pool_command_test.rb
161
- - test/pool_test.rb
162
156
  - test/prompt_regexp_test.rb
163
157
  - test/stat_struct_test.rb
164
158
  - test/stats_test.rb
@@ -1,166 +0,0 @@
1
- # Simple ruby client for interacting with beanstalkd.
2
- module Beaneater
3
- # Represents collection of beanstalkd connections.
4
- class Pool
5
- # Default number of retries to send a command to a connection
6
- MAX_RETRIES = 3
7
-
8
- # @!attribute connections
9
- # @return [Array<Beaneater::Connection>] returns Collection of connections
10
- attr_reader :connections
11
-
12
- # Initialize new connection
13
- #
14
- # @param [Array<String>] addresses Array of beanstalkd server addresses
15
- # @example
16
- # Beaneater::Pool.new(['localhost:11300', '127.0.0.1:11300'])
17
- #
18
- # ENV['BEANSTALKD_URL'] = 'localhost:11300,127.0.0.1:11300'
19
- # @bp = Beaneater::Pool.new
20
- # @bp.connections.first.host # => 'localhost'
21
- # @bp.connections.last.host # => '127.0.0.1'
22
- #
23
- def initialize(addresses=nil)
24
- addresses = addresses || host_from_env || Beaneater.configuration.beanstalkd_url
25
- @connections = Array(addresses).map { |a| Connection.new(a) }
26
- end
27
-
28
- # Returns Beaneater::Stats object for accessing beanstalk stats.
29
- #
30
- # @return [Beaneater::Stats] stats object
31
- # @api public
32
- def stats
33
- @stats ||= Stats.new(self)
34
- end
35
-
36
- # Returns Beaneater::Jobs object for accessing job related functions.
37
- #
38
- # @return [Beaneater::Jobs] jobs object
39
- # @api public
40
- def jobs
41
- @jobs ||= Jobs.new(self)
42
- end
43
-
44
- # Returns Beaneater::Tubes object for accessing tube related functions.
45
- #
46
- # @return [Beaneater::Tubes] tubes object
47
- # @api public
48
- def tubes
49
- @tubes ||= Tubes.new(self)
50
- end
51
-
52
- # Sends command to every beanstalkd server set in the pool.
53
- #
54
- # @param [String] command Beanstalkd command
55
- # @param [Hash{String => String, Boolean}] options socket connections options
56
- # @param [Proc] block Block passed to socket connection during transmit
57
- # @return [Array<Hash{String => String, Number}>] Beanstalkd command response from each instance
58
- # @example
59
- # @pool.transmit_to_all("stats")
60
- #
61
- def transmit_to_all(command, options={}, &block)
62
- res_exception = nil
63
- res = connections.map { |conn|
64
- begin
65
- safe_transmit { conn.transmit(command, options, &block) }
66
- rescue UnexpectedResponse => ex # not the correct status
67
- res_exception = ex
68
- nil
69
- end
70
- }.compact
71
- raise res_exception if res.none? && res_exception
72
- res
73
- end
74
-
75
- # Send command to each beanstalkd servers until getting response expected
76
- #
77
- # @param [String] command Beanstalkd command
78
- # @param [Hash{String => String, Boolean}] options socket connections options
79
- # @param [Proc] block Block passed in socket connection object
80
- # @return [Array<Hash{String => String, Number}>] Beanstalkd command response from the instance
81
- # @example
82
- # @pool.transmit_until_res('peek-ready', :status => "FOUND", &block)
83
- #
84
- def transmit_until_res(command, options={}, &block)
85
- status_expected = options.delete(:status)
86
- res_exception = nil
87
- connections.each do |conn|
88
- begin
89
- res = safe_transmit { conn.transmit(command, options, &block) }
90
- return res if res[:status] == status_expected
91
- rescue UnexpectedResponse => ex # not the correct status
92
- res_exception = ex
93
- next
94
- end
95
- end
96
- raise res_exception if res_exception
97
- end
98
-
99
- # Sends command to a random beanstalkd server in the pool.
100
- #
101
- # @param [String] command Beanstalkd command
102
- # @param [Hash{String => String,Boolean}] options socket connections options
103
- # @param [Proc] block Block passed in socket connection object
104
- # @return [Array<Hash{String => String, Number}>] Beanstalkd command response from the instance
105
- # @example
106
- # @pool.transmit_to_rand("stats", :match => /\n/)
107
- #
108
- def transmit_to_rand(command, options={}, &block)
109
- safe_transmit do
110
- conn = connections.respond_to?(:sample) ? connections.sample : connections.choice
111
- conn.transmit(command, options, &block)
112
- end
113
- end
114
-
115
- # Closes all connections within the pool.
116
- #
117
- # @example
118
- # @pool.close
119
- #
120
- def close
121
- while @connections.any?
122
- conn = @connections.pop
123
- conn.close
124
- end
125
- end
126
-
127
- protected
128
-
129
- # Transmit command to beanstalk connections safely handling failed connections
130
- #
131
- # @param [Proc] block The command to execute.
132
- # @return [Object] Result of the block passed
133
- # @raise [Beaneater::DrainingError,Beaneater::NotConnected] Could not connect to Beanstalk client
134
- # @example
135
- # safe_transmit { conn.transmit('foo') }
136
- # # => "result of foo command from beanstalk"
137
- #
138
- def safe_transmit(&block)
139
- retries = 1
140
- begin
141
- yield
142
- rescue DrainingError, EOFError, Errno::ECONNRESET, Errno::EPIPE => ex
143
- # TODO remove faulty connections from pool?
144
- # https://github.com/kr/beanstalk-client-ruby/blob/master/lib/beanstalk-client/connection.rb#L405-410
145
- if retries < MAX_RETRIES
146
- retries += 1
147
- retry
148
- else # finished retrying, fail out
149
- ex.is_a?(DrainingError) ? raise(ex) : raise(NotConnected, "Could not connect!")
150
- end
151
- end
152
- end # transmit_call
153
-
154
- # The hosts provided by BEANSTALKD_URL environment variable, if available.
155
- #
156
- # @return [Array] Set of beanstalkd host addresses
157
- # @example
158
- # ENV['BEANSTALKD_URL'] = "localhost:1212,localhost:2424"
159
- # # => ['localhost:1212', 'localhost:2424']
160
- #
161
- def host_from_env
162
- ENV['BEANSTALKD_URL'].respond_to?(:length) && ENV['BEANSTALKD_URL'].length > 0 && ENV['BEANSTALKD_URL'].split(',').map(&:strip)
163
- end
164
-
165
- end # Pool
166
- end # Beaneater