beaneater 0.3.1 → 1.1.1

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/pool_test.rb DELETED
@@ -1,185 +0,0 @@
1
- # test/connection_test.rb
2
-
3
- require File.expand_path('../test_helper', __FILE__)
4
-
5
- describe Beaneater::Pool do
6
-
7
- before do
8
- @hosts = ['localhost', 'localhost']
9
- @bp = Beaneater::Pool.new(@hosts)
10
- end
11
-
12
- describe 'for #new' do
13
-
14
- describe "for multiple connection" do
15
- before do
16
- @host_string_port = 'localhost:11301'
17
- @host_num_port = '127.0.0.1:11302'
18
- @host_string = 'host.local'
19
- @host_num = '1.1.1.1:11303'
20
- @hosts = [@host_string_port, @host_num_port, @host_string, @host_num]
21
-
22
- TCPSocket.expects(:new).with('localhost',11301).once
23
- TCPSocket.expects(:new).with('127.0.0.1',11302).once
24
- TCPSocket.expects(:new).with('host.local',11300).once
25
- TCPSocket.expects(:new).with('1.1.1.1',11303).once
26
-
27
- @bp = Beaneater::Pool.new(@hosts)
28
- end
29
-
30
- it "should init 4 connections" do
31
- assert_equal 4, @bp.connections.size
32
- end
33
- end
34
-
35
- describe "for invalid connection in setup" do
36
- it "should raise NotConnected" do
37
- assert_raises(Beaneater::NotConnected) { Beaneater::Pool.new('localhost:5679') }
38
- end
39
- end
40
-
41
- it "raises UnexpectedException when any Exception occurs inside the block" do
42
- invalid_command = nil
43
- assert_raises(Beaneater::UnknownCommandError) { @bp.transmit_to_rand(invalid_command) }
44
- end
45
-
46
- describe "for clearing watch list" do
47
- it "should clear connections of tube watches" do
48
- @bp.tubes.watch!('foo', 'bar')
49
- assert_equal ['foo', 'bar'].sort, @bp.tubes.watched.map(&:name).sort
50
- @bp2 = Beaneater::Pool.new(@hosts)
51
- assert_equal ['default'], @bp2.tubes.watched.map(&:name)
52
- end
53
- end
54
-
55
- describe "with ENV variable set" do
56
- before do
57
- ENV['BEANSTALKD_URL'] = '0.0.0.0:11300,127.0.0.1:11300'
58
- end
59
-
60
- it "should create 1 connection" do
61
- bp = Beaneater::Pool.new
62
- bc = bp.connections.first
63
- bc2 = bp.connections.last
64
-
65
- assert_equal 2, bp.connections.size
66
- assert_equal '0.0.0.0', bc.host
67
- assert_equal 11300, bc.port
68
-
69
- assert_equal '127.0.0.1', bc2.host
70
- assert_equal 11300, bc2.port
71
- end
72
-
73
- after do
74
- ENV['BEANSTALKD_URL'] = nil
75
- end
76
- end
77
-
78
- describe "by configuring via Beaneater.configure" do
79
- before do
80
- Beaneater.configure.beanstalkd_url = ['0.0.0.0:11300', '127.0.0.1:11300']
81
- end
82
-
83
- it "should create 1 connection" do
84
- bp = Beaneater::Pool.new
85
- bc = bp.connections.first
86
- bc2 = bp.connections.last
87
-
88
- assert_equal 2, bp.connections.size
89
- assert_equal '0.0.0.0', bc.host
90
- assert_equal 11300, bc.port
91
-
92
- assert_equal '127.0.0.1', bc2.host
93
- assert_equal 11300, bc2.port
94
- end
95
-
96
- after do
97
- Beaneater.configure.beanstalkd_url = ['0.0.0.0:11300']
98
- end
99
- end
100
- end # new
101
-
102
- describe 'for #transmit_to_all' do
103
- it "should return yaml loaded response" do
104
- res = @bp.transmit_to_all 'stats'
105
- assert_equal 2, res.size
106
- refute_nil res.first[:body]['current-connections']
107
- end
108
- end # transmit_to_all
109
-
110
- describe 'for #transmit_to_rand' do
111
- it "should return yaml loaded response" do
112
- res = @bp.transmit_to_rand 'stats'
113
- refute_nil res[:body]['current-connections']
114
- assert_equal 'OK', res[:status]
115
- end
116
-
117
- it "should return id" do
118
- Beaneater::Connection.any_instance.expects(:transmit).with("foo",{}).returns({:id => "254", :status => "INSERTED"})
119
- res = @bp.transmit_to_rand 'foo'
120
- assert_equal '254', res[:id]
121
- assert_equal 'INSERTED', res[:status]
122
- end
123
- end # transmit_to_rand
124
-
125
- describe 'for #transmit_until_res' do
126
- before do
127
- Beaneater::Connection.any_instance.expects(:transmit).with('foo', {}).twice.
128
- returns({:status => "FAILED", :body => 'x'}).then.
129
- returns({:status => "OK", :body => 'y'}).then.returns({:status => "OK", :body => 'z'})
130
- end
131
-
132
- it "should returns first matching status" do
133
- assert_equal 'y', @bp.transmit_until_res('foo', :status => 'OK')[:body]
134
- end
135
- end # transmit_until_res
136
-
137
- describe 'for #stats' do
138
- it("should return stats object"){ assert_kind_of Beaneater::Stats, @bp.stats }
139
- end # stats
140
-
141
- describe 'for #tubes' do
142
- it("should return Tubes object"){ assert_kind_of Beaneater::Tubes, @bp.tubes }
143
- end # tubes
144
-
145
- describe "for #safe_transmit" do
146
- it "should retry 3 times for temporary failed connection" do
147
- TCPSocket.any_instance.expects(:write).times(3)
148
- TCPSocket.any_instance.expects(:gets).raises(Errno::ECONNRESET).then.
149
- raises(Errno::ECONNRESET).then.returns('INSERTED 254').times(3)
150
- res = @bp.transmit_to_rand "put 0 0 10 2\r\nxy"
151
- assert_equal '254', res[:id]
152
- assert_equal 'INSERTED', res[:status]
153
- end
154
-
155
- it 'should raise proper exception for invalid status NOT_FOUND' do
156
- TCPSocket.any_instance.expects(:write).once
157
- TCPSocket.any_instance.expects(:gets).returns('NOT_FOUND')
158
- assert_raises(Beaneater::NotFoundError) { @bp.transmit_to_rand 'foo' }
159
- end
160
-
161
- it 'should raise proper exception for invalid status BAD_FORMAT' do
162
- TCPSocket.any_instance.expects(:write).once
163
- TCPSocket.any_instance.expects(:gets).returns('BAD_FORMAT')
164
- assert_raises(Beaneater::BadFormatError) { @bp.transmit_to_rand 'foo' }
165
- end
166
-
167
- it 'should raise proper exception for invalid status DEADLINE_SOON' do
168
- TCPSocket.any_instance.expects(:write).once
169
- TCPSocket.any_instance.expects(:gets).once.returns('DEADLINE_SOON')
170
- assert_raises(Beaneater::DeadlineSoonError) { @bp.transmit_to_rand 'expecting deadline' }
171
- end
172
- end
173
-
174
- describe "for #close" do
175
- it "should support closing the pool" do
176
- connection = @bp.connections.first
177
- assert_equal 2, @bp.connections.size
178
- assert_kind_of Beaneater::Connection, connection
179
- assert_kind_of TCPSocket, connection.connection
180
- @bp.close
181
- assert_equal 0, @bp.connections.size
182
- assert_nil connection.connection
183
- end
184
- end # close
185
- end # Beaneater::Pool