gearman-ruby 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/.gitignore +1 -0
  2. data/HOWTO +146 -0
  3. data/LICENSE +20 -0
  4. data/README +9 -0
  5. data/Rakefile +41 -0
  6. data/TODO +8 -0
  7. data/VERSION.yml +4 -0
  8. data/examples/calculus_client.rb +41 -0
  9. data/examples/calculus_worker.rb +42 -0
  10. data/examples/client.rb +19 -0
  11. data/examples/client_background.rb +14 -0
  12. data/examples/client_data.rb +16 -0
  13. data/examples/client_echo.rb +16 -0
  14. data/examples/client_exception.rb +17 -0
  15. data/examples/client_prefix.rb +15 -0
  16. data/examples/evented_client.rb +23 -0
  17. data/examples/evented_worker.rb +26 -0
  18. data/examples/gearman_environment.sh +25 -0
  19. data/examples/scale_image.rb +30 -0
  20. data/examples/scale_image_worker.rb +34 -0
  21. data/examples/server.rb +15 -0
  22. data/examples/worker.rb +23 -0
  23. data/examples/worker_data.rb +16 -0
  24. data/examples/worker_echo.rb +20 -0
  25. data/examples/worker_echo_pprof.rb +5 -0
  26. data/examples/worker_exception.rb +14 -0
  27. data/examples/worker_prefix.rb +25 -0
  28. data/gearman-ruby.gemspec +111 -0
  29. data/lib/gearman.rb +29 -0
  30. data/lib/gearman/client.rb +80 -0
  31. data/lib/gearman/evented/client.rb +99 -0
  32. data/lib/gearman/evented/reactor.rb +86 -0
  33. data/lib/gearman/evented/worker.rb +118 -0
  34. data/lib/gearman/job.rb +38 -0
  35. data/lib/gearman/protocol.rb +110 -0
  36. data/lib/gearman/server.rb +94 -0
  37. data/lib/gearman/task.rb +99 -0
  38. data/lib/gearman/taskset.rb +11 -0
  39. data/lib/gearman/util.rb +52 -0
  40. data/lib/gearman/worker.rb +39 -0
  41. data/test/basic_integration_test.rb +121 -0
  42. data/test/crash_test.rb +69 -0
  43. data/test/job_test.rb +30 -0
  44. data/test/protocol_test.rb +132 -0
  45. data/test/test_helper.rb +31 -0
  46. data/test/util_test.rb +12 -0
  47. data/test/worker_test.rb +45 -0
  48. metadata +133 -0
data/test/job_test.rb ADDED
@@ -0,0 +1,30 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class JobTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @handle = "H:foo:1"
7
+ @mock_client = mock()
8
+ @job = Gearman::Job.new(@mock_client, @handle)
9
+ end
10
+
11
+ def test_supports_report_status
12
+ @mock_client.expects(:send).with(:work_status, [@handle, 1, 5].join("\0"))
13
+ @job.report_status(1, 5)
14
+ end
15
+
16
+ def test_supports_send_partial
17
+ @mock_client.expects(:send).with(:work_data, [@handle, "bar"].join("\0"))
18
+ @job.send_partial("bar")
19
+ end
20
+
21
+ def test_supports_send_data
22
+ @mock_client.expects(:send).with(:work_data, [@handle, "bar"].join("\0"))
23
+ @job.send_data("bar")
24
+ end
25
+
26
+ def test_supports_report_warning
27
+ @mock_client.expects(:send).with(:work_warning, [@handle, "danger"].join("\0"))
28
+ @job.report_warning("danger")
29
+ end
30
+ end
@@ -0,0 +1,132 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ProtocolTest < Test::Unit::TestCase
4
+
5
+ def test_encode_request
6
+ payload = ["foo", "123", "bar"].join("\0")
7
+ expected = "\0REQ" + [Gearman::Protocol::COMMANDS_NUMERIC[:submit_job], payload.size].pack("NN") + payload
8
+ assert_equal expected, Gearman::Protocol.encode_request(:submit_job, payload)
9
+ end
10
+
11
+ def test_decode_response
12
+ response = "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:work_data], 3].pack("NN") + handle + "\0foo"
13
+ packets = Gearman::Protocol.decode_response(response)
14
+ assert_equal 1, packets.size
15
+ assert_equal [:work_data, "H:wrk.acme:1", "foo"], packets.first
16
+ end
17
+
18
+ def test_decodes_multiple_response_packets
19
+ response = "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:work_data], 3].pack("NN") + handle + "\0foo"
20
+ response << "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:work_data], 3].pack("NN") + handle + "\0bar"
21
+ response << "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:work_data], 3].pack("NN") + handle + "\0baz"
22
+
23
+ assert_equal 3, Gearman::Protocol.decode_response(response).size
24
+ end
25
+
26
+ def test_response_packet
27
+ packet = [:work_data, handle, "foo"]
28
+ assert_equal packet, Gearman::Protocol.response_packet(*packet)
29
+ end
30
+
31
+ def test_decodes_work_complete
32
+ data = "esta complet"
33
+ response = "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:work_complete], data.size].pack("NN") + [handle, data].join("\0")
34
+ assert_equal [:work_complete, handle, data], Gearman::Protocol.decode_response(response).first
35
+ end
36
+
37
+ def test_decodes_work_exception
38
+ data = "{native perl exception object}"
39
+ response = "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:work_exception], data.size].pack("NN") + [handle, data].join("\0")
40
+ assert_equal [:work_exception, handle, data], Gearman::Protocol.decode_response(response).first
41
+ end
42
+
43
+ def test_decodes_work_warning
44
+ data = "I warn you, dude"
45
+ response = "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:work_warning], data.size].pack("NN") + [handle, data].join("\0")
46
+ assert_equal [:work_warning, handle, data], Gearman::Protocol.decode_response(response).first
47
+ end
48
+
49
+ def test_decodes_work_data
50
+ data = "foo"
51
+ response = "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:work_data], data.size].pack("NN") + [handle, data].join("\0")
52
+ assert_equal [:work_data, handle, data], Gearman::Protocol.decode_response(response).first
53
+ end
54
+
55
+ def test_decodes_error
56
+ data = "error"
57
+ response = "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:error], data.size].pack("NN") + [handle, data].join("\0")
58
+ assert_equal [:error, handle, data], Gearman::Protocol.decode_response(response).first
59
+ end
60
+
61
+ def test_decodes_job_assign
62
+ function = "foo_function"
63
+ arguments = "arguments"
64
+
65
+ payload = [handle, function, arguments].join("\0")
66
+ response = "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:job_assign], payload.size].pack("NN") + payload
67
+ assert_equal [:job_assign, handle, function, arguments], Gearman::Protocol.decode_response(response).first
68
+ end
69
+
70
+ def test_decodes_job_assign_uniq
71
+ function = "foo_function"
72
+ arguments = "arguments"
73
+ unique_id = "123-657"
74
+
75
+ payload = [handle, function, unique_id, arguments].join("\0")
76
+ response = "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:job_assign_uniq], payload.size].pack("NN") + payload
77
+ assert_equal [:job_assign_uniq, handle, function, arguments, unique_id], Gearman::Protocol.decode_response(response).first
78
+ end
79
+
80
+ def test_decodes_work_fail
81
+ response = "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:work_fail], 0].pack("NN") + handle
82
+ assert_equal [:work_fail, handle], Gearman::Protocol.decode_response(response).first
83
+ end
84
+
85
+ def test_decodes_job_created
86
+ response = "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:job_created], 0].pack("NN") + handle
87
+ assert_equal [:job_created, handle], Gearman::Protocol.decode_response(response).first
88
+ end
89
+
90
+ def test_decodes_no_job
91
+ response = "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:no_job], 0].pack("NN") + handle
92
+ assert_equal [:no_job, handle], Gearman::Protocol.decode_response(response).first
93
+ end
94
+
95
+ def test_decodes_noop
96
+ response = "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:noop], 0].pack("NN") + handle
97
+ assert_equal [:noop, handle], Gearman::Protocol.decode_response(response).first
98
+ end
99
+
100
+ def test_decodes_work_status
101
+ numerator = "1"
102
+ denominator = "5"
103
+
104
+ payload = [handle, numerator, denominator].join("\0")
105
+ response = "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:work_status], payload.size].pack("NN") + payload
106
+
107
+ assert_equal [:work_status, handle, numerator, denominator], Gearman::Protocol.decode_response(response).first
108
+ end
109
+
110
+ def test_decodes_status_res
111
+ known = "1"
112
+ running = "0"
113
+ numerator = "1"
114
+ denominator = "4"
115
+
116
+ payload = [handle, known, running, numerator, denominator].join("\0")
117
+ response = "\0RES" + [Gearman::Protocol::COMMANDS_NUMERIC[:status_res], payload.size].pack("NN") + payload
118
+
119
+ assert_equal [:status_res, handle, known, running, numerator, denominator], Gearman::Protocol.decode_response(response).first
120
+ end
121
+
122
+ def test_raises_on_invalid_command
123
+ response = "\0RES" + [6969, 0].pack("NN")
124
+ assert_raises(Gearman::ProtocolError) { Gearman::Protocol.decode_response(response) }
125
+ assert_raises(Gearman::ProtocolError) { Gearman::Protocol.response_packet(*[6969, handle, ''])}
126
+ end
127
+
128
+ private
129
+ def handle
130
+ "H:wrk.acme:1"
131
+ end
132
+ end
@@ -0,0 +1,31 @@
1
+ $:.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
2
+ require 'gearman'
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ def start_gearmand(port = 4730, debug = false)
7
+ log = debug ? "-l /tmp/gearmand.log -vvv" : ""
8
+ system "gearmand -d -p #{port} --pid-file=#{gearmand_pidfile(port)} #{log}"
9
+ gearmand_pid(port)
10
+ end
11
+
12
+ def stop_gearmand(port = 4730)
13
+ Process.kill "KILL", gearmand_pid(port)
14
+ File.unlink gearmand_pidfile(port)
15
+ end
16
+
17
+ def teardown_gearmands
18
+ glob = "/tmp/gearmand_*_#{$$}.pid"
19
+ Dir[glob].each do |pidfile|
20
+ Process.kill "KILL", `cat #{pidfile}`.to_i
21
+ File.unlink pidfile
22
+ end
23
+ end
24
+
25
+ def gearmand_pid(port = 4730)
26
+ `cat #{gearmand_pidfile(port)}`.to_i
27
+ end
28
+
29
+ def gearmand_pidfile(port)
30
+ "/tmp/gearmand_#{port}_#{$$}.pid"
31
+ end
data/test/util_test.rb ADDED
@@ -0,0 +1,12 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class UtilTest < Test::Unit::TestCase
4
+
5
+ def test_ability_prefix_name_builder
6
+ assert_equal(Gearman::Util.ability_name_with_prefix("test","a"),"test\ta")
7
+ end
8
+
9
+ def test_ability_name_for_perl
10
+ assert_equal(Gearman::Util.ability_name_for_perl("test","a"),"test\ta")
11
+ end
12
+ end
@@ -0,0 +1,45 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class WorkerTest < Test::Unit::TestCase
4
+
5
+ def test_accepts_multiple_job_servers
6
+ Gearman::Evented::WorkerReactor.expects(:connect).times(2)
7
+
8
+ EM.run do
9
+ EM.add_timer(0.1) { EM.stop_event_loop }
10
+ Gearman::Worker.new(["localhost:4730", "localhost:4731"]).work
11
+ end
12
+ end
13
+
14
+ def test_accepts_exactly_one_job_server
15
+ Gearman::Evented::WorkerReactor.expects(:connect).with("localhost", "4730", {:abilities => {}}).times(1)
16
+
17
+ EM.run do
18
+ EM.add_timer(0.1) { EM.stop_event_loop }
19
+ Gearman::Worker.new("localhost:4730").work
20
+ end
21
+ end
22
+
23
+ def test_passes_abilities_to_reactor
24
+ worker = Gearman::Worker.new("localhost:4730")
25
+ worker.add_ability("foo") {|data, job| "noop!"}
26
+ worker.add_ability("bar") {|data, job| "nothing to see here!" }
27
+ worker.remove_ability("bar")
28
+
29
+ assert_equal true, worker.has_ability?("foo")
30
+ assert_equal false, worker.has_ability?("bar")
31
+
32
+ Gearman::Evented::WorkerReactor.expects(:connect).with do |host, port, opts|
33
+ assert_equal "localhost", host
34
+ assert_equal 4730, port.to_i
35
+ assert_equal 1, opts[:abilities].size
36
+ assert_equal true, opts[:abilities]['foo'][:callback].is_a?(Proc)
37
+ true
38
+ end
39
+
40
+ EM.run do
41
+ EM.add_timer(0.1) { EM.stop_event_loop }
42
+ worker.work
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gearman-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kim Altintop
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-11 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: eventmachine
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.12.8
24
+ version:
25
+ description: Library for the Gearman distributed job system
26
+ email: kim.altintop@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README
34
+ files:
35
+ - .gitignore
36
+ - HOWTO
37
+ - LICENSE
38
+ - README
39
+ - Rakefile
40
+ - TODO
41
+ - VERSION.yml
42
+ - examples/calculus_client.rb
43
+ - examples/calculus_worker.rb
44
+ - examples/client.rb
45
+ - examples/client_background.rb
46
+ - examples/client_data.rb
47
+ - examples/client_exception.rb
48
+ - examples/client_prefix.rb
49
+ - examples/evented_client.rb
50
+ - examples/evented_worker.rb
51
+ - examples/gearman_environment.sh
52
+ - examples/scale_image.rb
53
+ - examples/scale_image_worker.rb
54
+ - examples/server.rb
55
+ - examples/worker.rb
56
+ - examples/worker_data.rb
57
+ - examples/worker_exception.rb
58
+ - examples/worker_prefix.rb
59
+ - gearman-ruby.gemspec
60
+ - lib/gearman.rb
61
+ - lib/gearman/client.rb
62
+ - lib/gearman/evented/client.rb
63
+ - lib/gearman/evented/reactor.rb
64
+ - lib/gearman/evented/worker.rb
65
+ - lib/gearman/job.rb
66
+ - lib/gearman/protocol.rb
67
+ - lib/gearman/server.rb
68
+ - lib/gearman/task.rb
69
+ - lib/gearman/taskset.rb
70
+ - lib/gearman/util.rb
71
+ - lib/gearman/worker.rb
72
+ - test/basic_integration_test.rb
73
+ - test/crash_test.rb
74
+ - test/job_test.rb
75
+ - test/protocol_test.rb
76
+ - test/test_helper.rb
77
+ - test/util_test.rb
78
+ - test/worker_test.rb
79
+ has_rdoc: true
80
+ homepage: http://github.com/kim/gearman-ruby
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options:
85
+ - --charset=UTF-8
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: "0"
93
+ version:
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: "0"
99
+ version:
100
+ requirements: []
101
+
102
+ rubyforge_project:
103
+ rubygems_version: 1.3.5
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Library for the Gearman distributed job system
107
+ test_files:
108
+ - test/basic_integration_test.rb
109
+ - test/crash_test.rb
110
+ - test/job_test.rb
111
+ - test/protocol_test.rb
112
+ - test/test_helper.rb
113
+ - test/util_test.rb
114
+ - test/worker_test.rb
115
+ - examples/calculus_client.rb
116
+ - examples/calculus_worker.rb
117
+ - examples/client.rb
118
+ - examples/client_background.rb
119
+ - examples/client_data.rb
120
+ - examples/client_echo.rb
121
+ - examples/client_exception.rb
122
+ - examples/client_prefix.rb
123
+ - examples/evented_client.rb
124
+ - examples/evented_worker.rb
125
+ - examples/scale_image.rb
126
+ - examples/scale_image_worker.rb
127
+ - examples/server.rb
128
+ - examples/worker.rb
129
+ - examples/worker_data.rb
130
+ - examples/worker_echo.rb
131
+ - examples/worker_echo_pprof.rb
132
+ - examples/worker_exception.rb
133
+ - examples/worker_prefix.rb