-
Notifications
You must be signed in to change notification settings - Fork 8
/
mock_worker.rb
executable file
·206 lines (174 loc) · 6.98 KB
/
mock_worker.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env ruby
$:.unshift('../lib')
require 'gearman'
require 'gearman/testlib'
require 'test/unit'
require 'thread'
Thread.abort_on_exception = true
class TestWorker < Test::Unit::TestCase
def test_complete
server = FakeJobServer.new(self)
worker = nil
sock = nil
s = TestScript.new
w = TestScript.new
server_thread = Thread.new { s.loop_forever }.run
worker_thread = Thread.new { w.loop_forever }.run
# Create a worker and wait for it to connect to us.
w.exec {
worker = Gearman::Worker.new(
"localhost:#{server.port}", nil, { :client_id => 'test' })
}
s.exec { sock = server.expect_connection }
s.wait
# After it connects, it should send its ID, and it should tell us its
# abilities when we report them.
s.exec { server.expect_request(sock, :set_client_id, 'test') }
w.exec { worker.add_ability('echo') {|d,j| j.report_status(1, 1); d } }
s.exec { server.expect_request(sock, :can_do, 'echo') }
# It should try to grab a job when we tell it to work.
w.exec { worker.work }
s.exec { server.expect_request(sock, :grab_job) }
# If we tell it there aren't any jobs, it should go to sleep.
s.exec { server.send_response(sock, :no_job) }
s.exec { server.expect_request(sock, :pre_sleep) }
# When we send it a noop, it should wake up and ask for a job again.
s.exec { server.send_response(sock, :noop) }
s.exec { server.expect_request(sock, :grab_job) }
# When we give it a job, it should do it.
s.exec { server.send_response(sock, :job_assign, "a\0echo\0foo") }
s.exec { server.expect_request(sock, :work_status, "a\0001\0001") }
s.exec { server.expect_request(sock, :work_complete, "a\0foo") }
# Test that functions are unregistered correctly.
w.exec { worker.remove_ability('echo') }
s.exec { server.expect_request(sock, :cant_do, 'echo') }
s.wait
end
def test_multiple_servers
server1 = FakeJobServer.new(self)
server2 = FakeJobServer.new(self)
# This is cheesy. We want to know the order that Worker#work will
# iterate through the servers, so we make sure that server1 will be the
# first one when the names are lexographically sorted.
if server2.port.to_s < server1.port.to_s
tmp = server1
server1 = server2
server2 = tmp
end
worker = nil
sock1, sock2 = nil
s1 = TestScript.new
s2 = TestScript.new
w = TestScript.new
server1_thread = Thread.new { s1.loop_forever }.run
server2_thread = Thread.new { s2.loop_forever }.run
worker_thread = Thread.new { w.loop_forever }.run
# Create a worker, which should connect to both servers.
w.exec {
worker = Gearman::Worker.new(
nil, nil, { :client_id => 'test', :reconnect_sec => 0.1 }) }
w.exec { worker.add_ability('foo') {|d,j| 'bar' } }
w.exec {
worker.job_servers =
[ "localhost:#{server1.port}", "localhost:#{server2.port}" ]
}
s1.exec { sock1 = server1.expect_connection }
s2.exec { sock2 = server2.expect_connection }
s1.wait
s2.wait
# It should register itself with both.
s1.exec { server1.expect_request(sock1, :set_client_id, 'test') }
s1.exec { server1.expect_request(sock1, :can_do, 'foo') }
s2.exec { server2.expect_request(sock2, :set_client_id, 'test') }
s2.exec { server2.expect_request(sock2, :can_do, 'foo') }
# It should try to get a job from both servers and then sleep.
w.exec { worker.work }
s1.exec { server1.expect_request(sock1, :grab_job) }
s1.exec { server1.send_response(sock1, :no_job) }
s2.exec { server2.expect_request(sock2, :grab_job) }
s2.exec { server2.send_response(sock2, :no_job) }
s1.exec { server1.expect_request(sock1, :pre_sleep) }
s2.exec { server2.expect_request(sock2, :pre_sleep) }
# If the second server wakes it up, it should again try to get a job
# and then do it.
s2.exec { server2.send_response(sock2, :noop) }
s1.exec { server1.expect_request(sock1, :grab_job) }
s1.exec { server1.send_response(sock1, :no_job) }
s2.exec { server2.expect_request(sock2, :grab_job) }
s2.exec { server2.send_response(sock2, :job_assign, "a\0foo\0") }
s2.exec { server2.expect_request(sock2, :work_complete, "a\0bar") }
w.wait
s1.wait
s2.wait
# Stop the first job server and make the worker try to reconnect to
# both.
old_servers = worker.job_servers
server1.stop
worker.job_servers = []
worker.job_servers = old_servers
s2.exec { sock2 = server2.expect_connection }
s2.wait
# It shouldn't have any trouble with the second server. Tell it to go
# to work.
s2.exec { server2.expect_request(sock2, :set_client_id, 'test') }
s2.exec { server2.expect_request(sock2, :can_do, 'foo') }
w.exec { worker.work }
s2.exec { server2.expect_request(sock2, :grab_job) }
s2.exec { server2.send_response(sock2, :no_job) }
s2.exec { server2.expect_request(sock2, :pre_sleep) }
s2.wait
# Start the first server and wait for the worker to connect to it and
# register.
server1.start
s1.exec { sock1 = server1.expect_connection }
s1.wait
s1.exec { server1.expect_request(sock1, :set_client_id, 'test') }
s1.exec { server1.expect_request(sock1, :can_do, 'foo') }
s1.wait
# Let the second server wake the worker up and then give it a job.
s2.exec { server2.send_response(sock2, :noop) }
s1.exec { server1.expect_request(sock1, :grab_job) }
s1.exec { server1.send_response(sock1, :no_job) }
s2.exec { server2.expect_request(sock2, :grab_job) }
s2.exec { server2.send_response(sock2, :job_assign, "a\0foo\0") }
s2.exec { server2.expect_request(sock2, :work_complete, "a\0bar") }
s1.wait
s2.wait
w.wait
end
def test_timeout
server = FakeJobServer.new(self)
worker = nil
sock = nil
s = TestScript.new
w = TestScript.new
server_thread = Thread.new { s.loop_forever }.run
worker_thread = Thread.new { w.loop_forever }.run
w.exec {
worker = Gearman::Worker.new("localhost:#{server.port}", nil,
{ :client_id => 'test',
:reconnect_sec => 0.15,
:network_timeout_sec => 0.1 })
}
s.exec { sock = server.expect_connection }
s.wait
s.exec { server.expect_request(sock, :set_client_id, 'test') }
w.exec { worker.add_ability('foo') {|d,j| 'bar' } }
s.exec { server.expect_request(sock, :can_do, 'foo') }
# Don't do anything after the client asks for a job.
w.exec { worker.work }
s.exec { server.expect_request(sock, :grab_job) }
s.exec { sleep 0.16 }
s.wait
# The client should reconnect and ask for a job again.
s.exec { sock = server.expect_connection }
s.wait
s.exec { server.expect_request(sock, :set_client_id, 'test') }
s.exec { server.expect_request(sock, :can_do, 'foo') }
s.exec { server.expect_request(sock, :grab_job) }
s.exec { server.send_response(sock, :job_assign, "a\0foo\0") }
s.exec { server.expect_request(sock, :work_complete, "a\0bar") }
s.wait
w.wait
end
end