asir 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +5 -0
- data/asir.gemspec +0 -2
- data/example/asir_control.sh +0 -27
- data/example/config/asir_config.rb +0 -7
- data/lib/asir/version.rb +1 -1
- metadata +3 -40
- data/example/asir_control_client_resque.rb +0 -16
- data/example/ex25.rb +0 -41
- data/lib/asir/transport/resque.rb +0 -246
- data/spec/resque_spec.rb +0 -210
data/ChangeLog
CHANGED
data/asir.gemspec
CHANGED
@@ -26,8 +26,6 @@ Gem::Specification.new do |s|
|
|
26
26
|
# see lib/asir/uuid.rb
|
27
27
|
gem.add_dependency "uuid", "~> 2.3.6"
|
28
28
|
end
|
29
|
-
s.add_dependency "redis", "~> 3.0.2"
|
30
|
-
s.add_dependency "resque", "~> 1.23.0"
|
31
29
|
s.add_dependency "httpclient", "~> 2.3.0"
|
32
30
|
s.add_dependency "libxml-ruby", "~> 2.3.3"
|
33
31
|
s.add_dependency "rack", "~> 1.4.1"
|
data/example/asir_control.sh
CHANGED
@@ -33,31 +33,4 @@ esac
|
|
33
33
|
|
34
34
|
#############################
|
35
35
|
|
36
|
-
case "$args"
|
37
|
-
in
|
38
|
-
*resque*|*ALL*)
|
39
|
-
|
40
|
-
$asir start resque conduit
|
41
|
-
sleep 1
|
42
|
-
if $asir alive resque conduit; then
|
43
|
-
echo "resque conduit alive"
|
44
|
-
fi
|
45
|
-
$asir start resque worker
|
46
|
-
sleep 1
|
47
|
-
$asir pid resque worker
|
48
|
-
if $asir alive resque worker; then
|
49
|
-
echo "resque worker alive"
|
50
|
-
fi
|
51
|
-
|
52
|
-
ruby "$dir/asir_control_client_resque.rb"
|
53
|
-
sleep 1
|
54
|
-
$asir stop resque worker
|
55
|
-
sleep 1
|
56
|
-
$asir stop resque conduit
|
57
|
-
|
58
|
-
;;
|
59
|
-
esac
|
60
|
-
|
61
|
-
#############################
|
62
|
-
|
63
36
|
exit 0
|
@@ -36,9 +36,6 @@ when :transport
|
|
36
36
|
|
37
37
|
# Setup requested Transport.
|
38
38
|
case asir.adjective
|
39
|
-
when :beanstalk
|
40
|
-
require 'asir/transport/beanstalk'
|
41
|
-
transport = ASIR::Transport::Beanstalk.new
|
42
39
|
when :http, :webrick
|
43
40
|
require 'asir/transport/webrick'
|
44
41
|
transport = ASIR::Transport::Webrick.new
|
@@ -47,10 +44,6 @@ when :transport
|
|
47
44
|
require 'asir/transport/rack'
|
48
45
|
transport = ASIR::Transport::Rack.new
|
49
46
|
transport.uri = "http://localhost:#{30000 + asir.identifier.to_s.to_i}/asir"
|
50
|
-
when :resque
|
51
|
-
gem 'resque'
|
52
|
-
require 'asir/transport/resque'
|
53
|
-
transport = ASIR::Transport::Resque.new
|
54
47
|
else
|
55
48
|
raise "Cannot configure Transport for #{asir.adjective}"
|
56
49
|
end
|
data/lib/asir/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: asir
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -27,38 +27,6 @@ dependencies:
|
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 2.3.6
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: redis
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
|
-
requirements:
|
35
|
-
- - ~>
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
version: 3.0.2
|
38
|
-
type: :runtime
|
39
|
-
prerelease: false
|
40
|
-
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
|
-
requirements:
|
43
|
-
- - ~>
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: 3.0.2
|
46
|
-
- !ruby/object:Gem::Dependency
|
47
|
-
name: resque
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
|
-
requirements:
|
51
|
-
- - ~>
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: 1.23.0
|
54
|
-
type: :runtime
|
55
|
-
prerelease: false
|
56
|
-
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
|
-
requirements:
|
59
|
-
- - ~>
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: 1.23.0
|
62
30
|
- !ruby/object:Gem::Dependency
|
63
31
|
name: httpclient
|
64
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -211,7 +179,6 @@ files:
|
|
211
179
|
- doc/sequence.pic
|
212
180
|
- example/asir_control.sh
|
213
181
|
- example/asir_control_client_http.rb
|
214
|
-
- example/asir_control_client_resque.rb
|
215
182
|
- example/config/asir_config.rb
|
216
183
|
- example/delayed_service.rb
|
217
184
|
- example/ex01.rb
|
@@ -233,7 +200,6 @@ files:
|
|
233
200
|
- example/ex22.rb
|
234
201
|
- example/ex23.rb
|
235
202
|
- example/ex24.rb
|
236
|
-
- example/ex25.rb
|
237
203
|
- example/ex26.rb
|
238
204
|
- example/example_helper.rb
|
239
205
|
- example/sample_service.rb
|
@@ -310,7 +276,6 @@ files:
|
|
310
276
|
- lib/asir/transport/null.rb
|
311
277
|
- lib/asir/transport/payload_io.rb
|
312
278
|
- lib/asir/transport/rack.rb
|
313
|
-
- lib/asir/transport/resque.rb
|
314
279
|
- lib/asir/transport/retry.rb
|
315
280
|
- lib/asir/transport/stream.rb
|
316
281
|
- lib/asir/transport/subprocess.rb
|
@@ -327,7 +292,6 @@ files:
|
|
327
292
|
- spec/json_spec.rb
|
328
293
|
- spec/message_spec.rb
|
329
294
|
- spec/performance_spec.rb
|
330
|
-
- spec/resque_spec.rb
|
331
295
|
- spec/spec_helper.rb
|
332
296
|
- spec/thread_pool_spec.rb
|
333
297
|
- spec/thread_variable_spec.rb
|
@@ -349,7 +313,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
349
313
|
version: '0'
|
350
314
|
segments:
|
351
315
|
- 0
|
352
|
-
hash:
|
316
|
+
hash: 947396790058340770
|
353
317
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
354
318
|
none: false
|
355
319
|
requirements:
|
@@ -358,7 +322,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
358
322
|
version: '0'
|
359
323
|
segments:
|
360
324
|
- 0
|
361
|
-
hash:
|
325
|
+
hash: 947396790058340770
|
362
326
|
requirements: []
|
363
327
|
rubyforge_project:
|
364
328
|
rubygems_version: 1.8.24
|
@@ -373,7 +337,6 @@ test_files:
|
|
373
337
|
- spec/json_spec.rb
|
374
338
|
- spec/message_spec.rb
|
375
339
|
- spec/performance_spec.rb
|
376
|
-
- spec/resque_spec.rb
|
377
340
|
- spec/spec_helper.rb
|
378
341
|
- spec/thread_pool_spec.rb
|
379
342
|
- spec/thread_variable_spec.rb
|
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'example_helper'
|
2
|
-
gem 'resque'
|
3
|
-
require 'asir/transport/resque'
|
4
|
-
require 'asir/coder/marshal'
|
5
|
-
begin
|
6
|
-
Email.asir.transport = t =
|
7
|
-
ASIR::Transport::Resque.new
|
8
|
-
t.one_way = true
|
9
|
-
t.encoder = ASIR::Coder::Marshal.new
|
10
|
-
pr Email.asir.send_email(:pdf_invoice,
|
11
|
-
:to => "user@email.com",
|
12
|
-
:customer => @customer)
|
13
|
-
ensure
|
14
|
-
t.close rescue nil
|
15
|
-
end
|
16
|
-
|
data/example/ex25.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
# !SLIDE :capture_code_output true
|
2
|
-
# One-way Resque service.
|
3
|
-
|
4
|
-
require 'rubygems'
|
5
|
-
gem 'resque'
|
6
|
-
require 'example_helper'
|
7
|
-
require 'asir/transport/resque'
|
8
|
-
begin
|
9
|
-
t = ASIR::Transport::Resque.new(:port => 31925,
|
10
|
-
:encoder => ASIR::Coder::Marshal.new)
|
11
|
-
# Control throttling of Resque::Worker inside ASIR::Transport::Resque
|
12
|
-
t.throttle = {
|
13
|
-
# :verbose => true,
|
14
|
-
:min_sleep => 0.0,
|
15
|
-
:max_sleep => 2.0,
|
16
|
-
:inc_sleep => 0.1,
|
17
|
-
:mul_sleep => 1.5,
|
18
|
-
:rand_sleep => 0.1,
|
19
|
-
}
|
20
|
-
t.start_conduit!; sleep 1
|
21
|
-
server_process do
|
22
|
-
t.prepare_server!
|
23
|
-
t.run_server!
|
24
|
-
end
|
25
|
-
UnsafeService.asir.transport = t
|
26
|
-
pr UnsafeService.asir.do_it(":ok")
|
27
|
-
rescue ::Exception => err
|
28
|
-
$stderr.puts "### #{$$}: ERROR: #{err.inspect}\n #{err.backtrace * "\n "}"
|
29
|
-
raise
|
30
|
-
ensure
|
31
|
-
sleep 5
|
32
|
-
t.close rescue nil; sleep 1; server_kill
|
33
|
-
t.stop_conduit!
|
34
|
-
end
|
35
|
-
|
36
|
-
# !SLIDE END
|
37
|
-
# EXPECT: : client process
|
38
|
-
# EXPECT: : server process
|
39
|
-
# EXPECT: UnsafeService.do_it => :ok
|
40
|
-
# EXPECT: : pr: nil
|
41
|
-
# EXPECT!: ERROR
|
@@ -1,246 +0,0 @@
|
|
1
|
-
require 'asir/transport/connection_oriented'
|
2
|
-
require 'resque'
|
3
|
-
require 'asir/poll_throttle'
|
4
|
-
|
5
|
-
module ASIR
|
6
|
-
class Transport
|
7
|
-
# !SLIDE
|
8
|
-
# Resque Transport
|
9
|
-
class Resque < ConnectionOriented
|
10
|
-
include PollThrottle
|
11
|
-
|
12
|
-
attr_accessor :queues, :queue, :namespace, :throttle
|
13
|
-
|
14
|
-
def initialize *args
|
15
|
-
@port_default = 6379
|
16
|
-
@scheme_default = 'redis'.freeze
|
17
|
-
super
|
18
|
-
self.one_way = true
|
19
|
-
end
|
20
|
-
|
21
|
-
# !SLIDE
|
22
|
-
# Resque client.
|
23
|
-
def _client_connect!
|
24
|
-
# $stderr.puts " #{$$} #{self} _client_connect!"
|
25
|
-
resque_connect!
|
26
|
-
rescue ::Exception => exc
|
27
|
-
raise exc.class, "#{self.class} #{uri}: #{exc.message}", exc.backtrace
|
28
|
-
end
|
29
|
-
|
30
|
-
# !SLIDE
|
31
|
-
# Resque server (worker).
|
32
|
-
def _server!
|
33
|
-
# $stderr.puts " #{$$} #{self} _server!"
|
34
|
-
resque_connect!
|
35
|
-
resque_worker
|
36
|
-
rescue ::Exception => exc
|
37
|
-
raise exc.class, "#{self.class} #{uri}: #{exc.message}", exc.backtrace
|
38
|
-
end
|
39
|
-
|
40
|
-
def _receive_result message, opaque_result
|
41
|
-
return nil if one_way || message.one_way
|
42
|
-
super
|
43
|
-
end
|
44
|
-
|
45
|
-
def _send_result message, result, result_payload, stream, message_state
|
46
|
-
return nil if one_way || message.one_way
|
47
|
-
super
|
48
|
-
end
|
49
|
-
|
50
|
-
def _send_message message, message_payload
|
51
|
-
stream.with_stream! do | io | # Force connect
|
52
|
-
queue = message[:resque_queue] || message[:queue] || self.queue
|
53
|
-
$stderr.puts " #{$$} #{self} _send_message #{message_payload.inspect} to queue=#{queue.inspect} as #{self.class} :process_job" if @verbose >= 2
|
54
|
-
# Invokes Transport::Resque.perform(metadata, payload)
|
55
|
-
metadata = message[:resque_metadata] || message.description
|
56
|
-
::Resque.enqueue_to(queue, self.class, metadata, message_payload)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def queues
|
61
|
-
@queues ||=
|
62
|
-
(
|
63
|
-
x = nil
|
64
|
-
x = path if @uri
|
65
|
-
x ||= ""
|
66
|
-
root, x = x.split('/')
|
67
|
-
x ||= ""
|
68
|
-
x = x.split(/(\s+|\s*,\s*)/)
|
69
|
-
x.each(&:freeze)
|
70
|
-
x.freeze
|
71
|
-
)
|
72
|
-
end
|
73
|
-
|
74
|
-
# Defaults to [ 'asir' ].
|
75
|
-
def queues_
|
76
|
-
@queues_ ||=
|
77
|
-
queues.empty? ? [ DEFAULT_QUEUE ] : queues.freeze
|
78
|
-
end
|
79
|
-
|
80
|
-
# Defaults to 'asir'.
|
81
|
-
def queue
|
82
|
-
@queue ||= queues_.first || DEFAULT_QUEUE
|
83
|
-
end
|
84
|
-
|
85
|
-
# Defaults to 'asir'.
|
86
|
-
def namespace_
|
87
|
-
@namespace_ ||= namespace || DEFAULT_QUEUE
|
88
|
-
end
|
89
|
-
|
90
|
-
DEFAULT_QUEUE = 'asir'.freeze
|
91
|
-
|
92
|
-
def _server_accept_connection! server
|
93
|
-
[ server, server ]
|
94
|
-
end
|
95
|
-
|
96
|
-
# Resque is message-oriented, process only one message per "connection".
|
97
|
-
def stream_eof? stream
|
98
|
-
false
|
99
|
-
end
|
100
|
-
|
101
|
-
# Nothing to be closed for Resque.
|
102
|
-
def _server_close_connection! in_stream, out_stream
|
103
|
-
# NOTHING
|
104
|
-
end
|
105
|
-
|
106
|
-
def serve_stream_message! in_stream, out_stream # ignored
|
107
|
-
save = ::Thread.current[:asir_transport_resque_instance]
|
108
|
-
::Thread.current[:asir_transport_resque_instance] = self
|
109
|
-
poll_throttle throttle do
|
110
|
-
$stderr.puts " #{$$} #{self} serve_stream_message!: resque_worker = #{resque_worker} on queues #{resque_worker.queues.inspect}" if @verbose >= 3
|
111
|
-
if job = resque_worker.reserve
|
112
|
-
$stderr.puts " #{$$} #{self} serve_stream_message! job=#{job.class}:#{job.inspect}" if @verbose >= 2
|
113
|
-
resque_worker.process(job)
|
114
|
-
end
|
115
|
-
job
|
116
|
-
end
|
117
|
-
self
|
118
|
-
ensure
|
119
|
-
::Thread.current[:asir_transport_resque_instance] = save
|
120
|
-
end
|
121
|
-
|
122
|
-
# Class method entry point from Resque::Job.perform.
|
123
|
-
def self.perform metadata, payload = nil
|
124
|
-
payload ||= metadata # old calling signature (just payload).
|
125
|
-
# $stderr.puts " #{self} process_job payload=#{payload.inspect}"
|
126
|
-
t = ::Thread.current[:asir_transport_resque_instance]
|
127
|
-
# Pass payload as in_stream; _receive_message will return it.
|
128
|
-
t.serve_message! payload, nil
|
129
|
-
end
|
130
|
-
|
131
|
-
def _receive_message payload, additional_data # is actual payload
|
132
|
-
# $stderr.puts " #{$$} #{self} _receive_message payload=#{payload.inspect}"
|
133
|
-
[ payload, nil ]
|
134
|
-
end
|
135
|
-
|
136
|
-
####################################
|
137
|
-
|
138
|
-
def resque_uri
|
139
|
-
@resque_uri ||=
|
140
|
-
(
|
141
|
-
unless scheme == 'redis'
|
142
|
-
raise ArgumentError, "Invalid resque URI: #{uri.inspect}"
|
143
|
-
end
|
144
|
-
_uri
|
145
|
-
)
|
146
|
-
end
|
147
|
-
|
148
|
-
def resque_connect!
|
149
|
-
@redis_config = {
|
150
|
-
:host => host,
|
151
|
-
:port => port,
|
152
|
-
:thread_safe => true,
|
153
|
-
}
|
154
|
-
@redis =
|
155
|
-
::Redis.new(@redis_config)
|
156
|
-
if namespace_
|
157
|
-
::Resque.redis =
|
158
|
-
@redis =
|
159
|
-
::Redis::Namespace.new(namespace_, :redis => @redis)
|
160
|
-
::Resque.redis.namespace = namespace_
|
161
|
-
else
|
162
|
-
::Resque.redis = @redis
|
163
|
-
end
|
164
|
-
# $stderr.puts " *** #{$$} #{self} resque_connect! #{@redis.inspect}"
|
165
|
-
@redis
|
166
|
-
end
|
167
|
-
|
168
|
-
def resque_disconnect!
|
169
|
-
::Resque.redis = nil
|
170
|
-
end
|
171
|
-
|
172
|
-
def resque_worker
|
173
|
-
@resque_worker ||= ::Resque::Worker.new(*queues_)
|
174
|
-
end
|
175
|
-
|
176
|
-
def server_on_start!
|
177
|
-
# prune_dead_workers expects processes to have "resque " in the name.
|
178
|
-
@save_progname ||= $0.dup
|
179
|
-
$0 = "resque #{$0}"
|
180
|
-
if worker = resque_worker
|
181
|
-
worker.prune_dead_workers
|
182
|
-
worker.register_worker
|
183
|
-
end
|
184
|
-
self
|
185
|
-
end
|
186
|
-
|
187
|
-
def server_on_stop!
|
188
|
-
$0 = @save_progname if @save_progname
|
189
|
-
if worker = @resque_worker
|
190
|
-
worker.unregister_worker
|
191
|
-
end
|
192
|
-
self
|
193
|
-
rescue Redis::CannotConnectError
|
194
|
-
# This error is not actionable since server
|
195
|
-
# is stopping.
|
196
|
-
nil
|
197
|
-
end
|
198
|
-
|
199
|
-
#########################################
|
200
|
-
|
201
|
-
@@redis_server_version = nil
|
202
|
-
def redis_server_version
|
203
|
-
@@redis_server_version ||=
|
204
|
-
begin
|
205
|
-
case v = `redis-server --version`
|
206
|
-
when /v=([.0-9]+)/ # 3.x
|
207
|
-
v = $1
|
208
|
-
when / version ([.0-9]+)/ # 2.x
|
209
|
-
v = $1
|
210
|
-
else
|
211
|
-
v = 'UNKNOWN'
|
212
|
-
end
|
213
|
-
v
|
214
|
-
end
|
215
|
-
end
|
216
|
-
|
217
|
-
def _start_conduit!
|
218
|
-
@redis_dir ||= "/tmp"
|
219
|
-
@redis_conf ||= "#{@redis_dir}/asir-redis-#{port}.conf"
|
220
|
-
@redis_log ||= "#{@redis_dir}/asir-redis-#{port}.log"
|
221
|
-
@redis_cmd = [ 'redis-server' ]
|
222
|
-
case redis_server_version
|
223
|
-
when /^2\.4/
|
224
|
-
::File.open(@redis_conf, "w+") do | out |
|
225
|
-
out.puts "daemonize no"
|
226
|
-
out.puts "port #{port}"
|
227
|
-
out.puts "loglevel warning"
|
228
|
-
out.puts "logfile #{@redis_log}"
|
229
|
-
end
|
230
|
-
@redis_cmd << @redis_conf
|
231
|
-
else
|
232
|
-
@redis_cmd <<
|
233
|
-
'--port' << port <<
|
234
|
-
'--loglevel' << 'warning' <<
|
235
|
-
'--logfile' << @redis_log
|
236
|
-
end
|
237
|
-
@redis_cmd.map! { | x | x.to_s }
|
238
|
-
# $stderr.puts " redis_cmd = #{@redis_cmd * ' '}" if @verbose >= 1
|
239
|
-
exec *@redis_cmd
|
240
|
-
end
|
241
|
-
end
|
242
|
-
# !SLIDE END
|
243
|
-
end # class
|
244
|
-
end # module
|
245
|
-
|
246
|
-
|
data/spec/resque_spec.rb
DELETED
@@ -1,210 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
gem 'redis'
|
3
|
-
gem 'resque'
|
4
|
-
require 'asir'
|
5
|
-
require 'asir/transport/resque'
|
6
|
-
require 'asir/coder/marshal'
|
7
|
-
require 'timeout'
|
8
|
-
|
9
|
-
describe "ASIR::Transport::Resque" do
|
10
|
-
it "should be able to start/stop redis" do
|
11
|
-
with_cleanup! do
|
12
|
-
create_transport!
|
13
|
-
pid = Process.fork do
|
14
|
-
# exec "false" # simulate failure to start.
|
15
|
-
transport._start_conduit!
|
16
|
-
end
|
17
|
-
sleep 1
|
18
|
-
Process.kill('TERM', pid)
|
19
|
-
sleep 1
|
20
|
-
wpid, status = nil, nil
|
21
|
-
Timeout.timeout(5) do
|
22
|
-
wpid, status = Process.waitpid2(pid, Process::WUNTRACED)
|
23
|
-
end
|
24
|
-
# puts status.inspect
|
25
|
-
wpid.should == pid
|
26
|
-
status.stopsig.should == nil
|
27
|
-
status.termsig.should == nil
|
28
|
-
status.exited?.should == true
|
29
|
-
status.exitstatus.should == 0
|
30
|
-
status.success?.should == true
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should process and stop! gracefully" do
|
35
|
-
with_cleanup! do
|
36
|
-
create_transport!
|
37
|
-
start_conduit!; sleep 1
|
38
|
-
start_client!
|
39
|
-
|
40
|
-
message_count = 0
|
41
|
-
transport.after_receive_message = lambda do | t, message |
|
42
|
-
message_count += 1
|
43
|
-
$stderr.write ">#{message_count}" if verbose
|
44
|
-
if message_count >= 5
|
45
|
-
t.stop!
|
46
|
-
end
|
47
|
-
end
|
48
|
-
Timeout.timeout(20) do
|
49
|
-
start_server!
|
50
|
-
end
|
51
|
-
|
52
|
-
message_count.should == 5
|
53
|
-
exceptions.should == [ ]
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should bubble up Redis::CannotConnectError if redis is not running" do
|
58
|
-
with_cleanup! do
|
59
|
-
create_transport!
|
60
|
-
message_count = 0
|
61
|
-
lambda do
|
62
|
-
transport.after_receive_message = lambda do | t, message |
|
63
|
-
message_count += 1
|
64
|
-
$stderr.write ">#{message_count}" if verbose
|
65
|
-
if message_count >= 10
|
66
|
-
t.stop!
|
67
|
-
end
|
68
|
-
end
|
69
|
-
Timeout.timeout(20) do
|
70
|
-
start_server!
|
71
|
-
end
|
72
|
-
end.should raise_error(Redis::CannotConnectError)
|
73
|
-
message_count.should == 0
|
74
|
-
exceptions.should == [ ]
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
it "should bubble up dropped connection error" do
|
79
|
-
with_cleanup! do
|
80
|
-
create_transport!
|
81
|
-
start_conduit!; sleep 1
|
82
|
-
start_client!
|
83
|
-
|
84
|
-
# Server should have errors.
|
85
|
-
message_count = 0
|
86
|
-
lambda do
|
87
|
-
transport.after_receive_message = lambda do | t, message |
|
88
|
-
message_count += 1
|
89
|
-
$stderr.write ">#{message_count}" if verbose
|
90
|
-
if message_count >= 5
|
91
|
-
stop_client!
|
92
|
-
stop_conduit! :signal => 9
|
93
|
-
end
|
94
|
-
if message_count >= 10
|
95
|
-
t.stop!
|
96
|
-
end
|
97
|
-
end
|
98
|
-
Timeout.timeout(20) do
|
99
|
-
start_server!
|
100
|
-
end
|
101
|
-
raise "start_server! exited"
|
102
|
-
end.should raise_error(Redis::CannotConnectError)
|
103
|
-
message_count.should == 5
|
104
|
-
exceptions.should == [ ]
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
attr_accessor :transport, :target, :exceptions, :verbose
|
109
|
-
|
110
|
-
before :each do
|
111
|
-
@target = ASIR::Test::ResqueTarget.new
|
112
|
-
@exceptions = [ ]
|
113
|
-
@verbose = (ENV['ASIR_TEST_VERBOSE'] || 0).to_i > 0
|
114
|
-
end
|
115
|
-
|
116
|
-
def with_cleanup!
|
117
|
-
yield
|
118
|
-
ensure
|
119
|
-
stop_client!
|
120
|
-
stop_server!
|
121
|
-
stop_conduit!
|
122
|
-
end
|
123
|
-
|
124
|
-
def create_transport!
|
125
|
-
@uri = "redis://localhost:23456"
|
126
|
-
@transport = ASIR::Transport::Resque.new(:uri => @uri)
|
127
|
-
transport.encoder = ASIR::Coder::Marshal.new
|
128
|
-
if verbose
|
129
|
-
transport._logger = $stderr
|
130
|
-
transport._log_enabled = true
|
131
|
-
end
|
132
|
-
ASIR::Client::Proxy.config_callbacks[target.class] = lambda do | proxy |
|
133
|
-
proxy.transport = transport
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
def start_conduit!
|
138
|
-
transport.verbose = 0
|
139
|
-
transport.start_conduit!
|
140
|
-
transport.verbose = 0
|
141
|
-
end
|
142
|
-
|
143
|
-
def stop_conduit! opts = nil
|
144
|
-
transport.verbose = 0
|
145
|
-
transport.stop_conduit! opts
|
146
|
-
transport.verbose = 0
|
147
|
-
end
|
148
|
-
|
149
|
-
def start_client! &blk
|
150
|
-
@client_pid = Process.fork do
|
151
|
-
# transport.verbose = 3
|
152
|
-
i = 0
|
153
|
-
loop do
|
154
|
-
i += 1
|
155
|
-
$stderr.write "<#{i}" if verbose
|
156
|
-
target.asir.eval! "2 + 2"
|
157
|
-
sleep 0.25
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
def stop_client!
|
163
|
-
if @client_pid
|
164
|
-
Process.kill 9, @client_pid
|
165
|
-
end
|
166
|
-
ensure
|
167
|
-
@client_pid = nil
|
168
|
-
end
|
169
|
-
|
170
|
-
def start_server!
|
171
|
-
transport.verbose = 0
|
172
|
-
transport.prepare_server!
|
173
|
-
transport.on_exception = lambda do | t, exc, kind, message, result |
|
174
|
-
@exceptions << exc
|
175
|
-
$stderr.puts " on_exception: #{kind.inspect}: #{exc.inspect}\n #{exc.backtrace * "\n "}"
|
176
|
-
end
|
177
|
-
transport.throttle = {
|
178
|
-
:min_sleep => 0.01,
|
179
|
-
:max_sleep => 2,
|
180
|
-
:inc_sleep => 0.1,
|
181
|
-
:mul_sleep => 1.25,
|
182
|
-
# :verbose => true,
|
183
|
-
}
|
184
|
-
transport.run_server!
|
185
|
-
transport.verbose = 0
|
186
|
-
end
|
187
|
-
|
188
|
-
def stop_server!
|
189
|
-
transport.stop!
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
module ASIR
|
194
|
-
module Test
|
195
|
-
class TestError < ::Exception; end
|
196
|
-
class ResqueTarget
|
197
|
-
include ASIR::Client
|
198
|
-
def raise_error! msg
|
199
|
-
raise TestError, msg
|
200
|
-
end
|
201
|
-
def eval! expr
|
202
|
-
result = eval expr
|
203
|
-
# $stderr.puts " #{self} eval!(#{expr.inspect}) => #{result.inspect}"
|
204
|
-
result
|
205
|
-
end
|
206
|
-
end
|
207
|
-
end
|
208
|
-
end
|
209
|
-
|
210
|
-
|