Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add tests for Task callbacks and failure of individual tasks in a set

git-svn-id: http://code.sixapart.com/svn/gearman/trunk/api/ruby@280 011c6a6d-750f-0410-a5f6-93fdcd050bc4
  • Loading branch information...
commit 23e2bf7fc9c6228b20d2e71aa61e2f446be28abd 1 parent acdfa0a
derat authored
Showing with 96 additions and 2 deletions.
  1. +95 −2 test/mock_client.rb
  2. +1 −0  test/testlib.rb
View
97 test/mock_client.rb
@@ -29,7 +29,7 @@ def test_client
s.wait
s.exec { server.expect_request(sock, :submit_job, "add\000\0005 2") }
- s.exec { server.send_response(sock, :job_created, "a") }
+ s.exec { server.send_response(sock, :job_created, 'a') }
# Create a second task. It should use the same socket as the first.
c.exec { task2 = Gearman::Task.new('add', '10 5') }
@@ -40,7 +40,7 @@ def test_client
# second.
s.exec { server.send_response(sock, :work_complete, "a\0007") }
s.exec { server.expect_request(sock, :submit_job, "add\000\00010 5") }
- s.exec { server.send_response(sock, :job_created, "b") }
+ s.exec { server.send_response(sock, :job_created, 'b') }
# After the client waits on the taskset, send the response to the
# second job.
@@ -52,4 +52,97 @@ def test_client
assert_equal(7, res1)
assert_equal(15, res2)
end
+
+ ##
+ # Test that Gearman::Task's callback's get called when they should.
+ def test_callbacks
+ server = FakeJobServer.new(self)
+ client, task, taskset, sock = nil
+ failed, retries, num, den = nil
+
+ s = TestScript.new
+ c = TestScript.new
+
+ server_thread = Thread.new { s.loop_forever }.run
+ client_thread = Thread.new { c.loop_forever }.run
+
+ c.exec { client = Gearman::Client.new("localhost:#{server.port}") }
+
+ task = Gearman::Task.new('foo', 'bar',
+ { :retry_count => 2 })
+ task.on_fail { failed = true }
+ task.on_retry {|r| retries = r }
+ task.on_status {|n,d| num = n.to_i; den = d.to_i }
+
+ c.exec { taskset = Gearman::TaskSet.new(client) }
+ c.exec { taskset.add_task(task) }
+ s.exec { sock = server.expect_connection }
+ s.wait
+
+ # Send three failures back to the client.
+ c.exec { taskset.wait }
+ s.exec { server.expect_request(sock, :submit_job, "foo\000\000bar") }
+ s.exec { server.send_response(sock, :job_created, 'a') }
+ s.exec { server.send_response(sock, :work_fail, 'a') }
+ s.exec { server.expect_request(sock, :submit_job, "foo\000\000bar") }
+ s.exec { server.send_response(sock, :job_created, 'b') }
+ s.exec { server.send_response(sock, :work_fail, 'b') }
+ s.exec { server.expect_request(sock, :submit_job, "foo\000\000bar") }
+ s.exec { server.send_response(sock, :job_created, 'c') }
+ s.exec { server.send_response(sock, :work_status, "c\0001\0002") }
+ s.exec { server.send_response(sock, :work_fail, 'c') }
+ c.wait
+ s.wait
+
+ assert_equal(true, failed)
+ assert_equal(2, retries)
+ assert_equal(1, num)
+ assert_equal(2, den)
+ end
+
+ def test_failure
+ server = FakeJobServer.new(self)
+ client, task1, task2, taskset, sock = nil
+ res1, res2, fail1, fail2, setres = nil
+
+ s = TestScript.new
+ c = TestScript.new
+
+ server_thread = Thread.new { s.loop_forever }.run
+ client_thread = Thread.new { c.loop_forever }.run
+
+ c.exec { client = Gearman::Client.new("localhost:#{server.port}", 'pre') }
+
+ c.exec { task1 = Gearman::Task.new('func1', 'a') }
+ c.exec { task1.on_complete {|d| res1 = d } }
+ c.exec { task1.on_fail { fail1 = true } }
+ c.exec { taskset = Gearman::TaskSet.new(client) }
+ c.exec { taskset.add_task(task1) }
+ s.exec { sock = server.expect_connection }
+ s.wait
+
+ s.exec { server.expect_request(sock, :submit_job, "pre\tfunc1\000\000a") }
+ s.exec { server.send_response(sock, :job_created, 'a') }
+
+ c.exec { task2 = Gearman::Task.new('func2', 'b') }
+ c.exec { task2.on_complete {|d| res2 = d } }
+ c.exec { task2.on_fail { fail2 = true } }
+ c.exec { taskset.add_task(task2) }
+
+ s.exec { server.expect_request(sock, :submit_job, "pre\tfunc2\000\000b") }
+ s.exec { server.send_response(sock, :job_created, 'b') }
+
+ s.exec { server.send_response(sock, :work_complete, "a\000a1") }
+ s.exec { server.send_response(sock, :work_fail, "b") }
+
+ c.exec { setres = taskset.wait }
+ c.wait
+ s.wait
+
+ assert_equal('a1', res1)
+ assert_equal(nil, res2)
+ assert_equal(nil, fail1)
+ assert_equal(true, fail2)
+ assert_equal(false, setres)
+ end
end
View
1  test/testlib.rb
@@ -39,6 +39,7 @@ def expect_request(sock, exp_type, exp_data='')
def send_response(sock, type, data='')
type_num = Gearman::Util::NUMS[type.to_sym]
+ raise RuntimeError, "Invalid type #{type}" if not type_num
response = "\0RES" + [type_num, data.size].pack('NN') + data
sock.write(response)
end
Please sign in to comment.
Something went wrong with that request. Please try again.