backgroundrb-rails3 1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. data/.autotest +17 -0
  2. data/ChangeLog +50 -0
  3. data/Gemfile +11 -0
  4. data/LICENSE +4 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README +22 -0
  7. data/Rakefile +128 -0
  8. data/TODO.org +5 -0
  9. data/app/controller/backgroundrb_status_controller.rb +6 -0
  10. data/backgroundrb-rails3.gemspec +219 -0
  11. data/config/backgroundrb.yml +11 -0
  12. data/doc/Rakefile +5 -0
  13. data/doc/config.yaml +2 -0
  14. data/doc/content/advanced/advanced.txt +76 -0
  15. data/doc/content/advanced/advanced.yaml +4 -0
  16. data/doc/content/bugs/bugs.txt +20 -0
  17. data/doc/content/bugs/bugs.yaml +5 -0
  18. data/doc/content/community/community.txt +36 -0
  19. data/doc/content/community/community.yaml +5 -0
  20. data/doc/content/content.txt +168 -0
  21. data/doc/content/content.yaml +5 -0
  22. data/doc/content/faq/faq.txt +41 -0
  23. data/doc/content/faq/faq.yaml +5 -0
  24. data/doc/content/rails/rails.txt +182 -0
  25. data/doc/content/rails/rails.yaml +5 -0
  26. data/doc/content/scheduling/scheduling.txt +166 -0
  27. data/doc/content/scheduling/scheduling.yaml +5 -0
  28. data/doc/content/workers/workers.txt +178 -0
  29. data/doc/content/workers/workers.yaml +5 -0
  30. data/doc/layouts/default/default.erb +56 -0
  31. data/doc/layouts/default/default.yaml +4 -0
  32. data/doc/lib/default.rb +7 -0
  33. data/doc/output/Assets/BG-Ad-Top.png +0 -0
  34. data/doc/output/Assets/BG-Body.png +0 -0
  35. data/doc/output/Assets/BG-Feed.png +0 -0
  36. data/doc/output/Assets/BG-Menu-Hover.png +0 -0
  37. data/doc/output/Assets/BG-Menu.png +0 -0
  38. data/doc/output/Assets/BG-Sidebar-Bottom.png +0 -0
  39. data/doc/output/Assets/Button-Feed.png +0 -0
  40. data/doc/output/images/bg-ad-top.png +0 -0
  41. data/doc/output/images/bg-body.png +0 -0
  42. data/doc/output/images/bg-feed.gif +0 -0
  43. data/doc/output/images/bg-footer.jpg +0 -0
  44. data/doc/output/images/bg-header.jpg +0 -0
  45. data/doc/output/images/bg-menu-hover.png +0 -0
  46. data/doc/output/images/bg-menu.png +0 -0
  47. data/doc/output/images/bg-sidebar-bottom.gif +0 -0
  48. data/doc/output/images/button-feed.png +0 -0
  49. data/doc/output/images/icon-comment.png +0 -0
  50. data/doc/output/images/more_icon.gif +0 -0
  51. data/doc/output/style.css +299 -0
  52. data/doc/page_defaults.yaml +13 -0
  53. data/doc/tasks/default.rake +3 -0
  54. data/doc/templates/default/default.txt +1 -0
  55. data/doc/templates/default/default.yaml +4 -0
  56. data/examples/backgroundrb.yml +25 -0
  57. data/examples/foo_controller.rb +48 -0
  58. data/examples/god_worker.rb +7 -0
  59. data/examples/worker_tests/god_worker_test.rb +8 -0
  60. data/examples/workers/error_worker.rb +17 -0
  61. data/examples/workers/foo_worker.rb +38 -0
  62. data/examples/workers/god_worker.rb +7 -0
  63. data/examples/workers/model_worker.rb +13 -0
  64. data/examples/workers/renewal_worker.rb +11 -0
  65. data/examples/workers/rss_worker.rb +26 -0
  66. data/examples/workers/server_worker.rb +31 -0
  67. data/examples/workers/world_worker.rb +12 -0
  68. data/examples/workers/xmpp_worker.rb +7 -0
  69. data/init.rb +7 -0
  70. data/install.rb +1 -0
  71. data/know_issues.org +5 -0
  72. data/lib/backgroundrb.rb +1 -0
  73. data/lib/backgroundrb/bdrb_client_helper.rb +8 -0
  74. data/lib/backgroundrb/bdrb_cluster_connection.rb +156 -0
  75. data/lib/backgroundrb/bdrb_config.rb +43 -0
  76. data/lib/backgroundrb/bdrb_conn_error.rb +29 -0
  77. data/lib/backgroundrb/bdrb_connection.rb +179 -0
  78. data/lib/backgroundrb/bdrb_job_queue.rb +79 -0
  79. data/lib/backgroundrb/bdrb_result.rb +19 -0
  80. data/lib/backgroundrb/bdrb_start_stop.rb +146 -0
  81. data/lib/backgroundrb/rails_worker_proxy.rb +181 -0
  82. data/lib/backgroundrb/railtie.rb +48 -0
  83. data/lib/generators/backgroundrb/bdrb_migration/USAGE +12 -0
  84. data/lib/generators/backgroundrb/bdrb_migration/bdrb_migration_generator.rb +15 -0
  85. data/lib/generators/backgroundrb/bdrb_migration/templates/migration.rb +27 -0
  86. data/lib/generators/backgroundrb/worker/USAGE +16 -0
  87. data/lib/generators/backgroundrb/worker/templates/unit_test.rb +12 -0
  88. data/lib/generators/backgroundrb/worker/templates/worker.rb +7 -0
  89. data/lib/generators/backgroundrb/worker/worker_generator.rb +14 -0
  90. data/lib/tasks/backgroundrb_tasks.rake +103 -0
  91. data/release_notes.org +48 -0
  92. data/release_points.org +46 -0
  93. data/script/backgroundrb +52 -0
  94. data/script/bdrb_test_helper.rb +99 -0
  95. data/script/load_worker_env.rb +31 -0
  96. data/script/monitrc +25 -0
  97. data/server/backgroundrb_server.rb +12 -0
  98. data/server/lib/bdrb_result_storage.rb +62 -0
  99. data/server/lib/bdrb_server_helper.rb +24 -0
  100. data/server/lib/bdrb_thread_pool.rb +127 -0
  101. data/server/lib/cron_trigger.rb +197 -0
  102. data/server/lib/invalid_dump_error.rb +4 -0
  103. data/server/lib/log_worker.rb +25 -0
  104. data/server/lib/master_proxy.rb +140 -0
  105. data/server/lib/master_worker.rb +187 -0
  106. data/server/lib/meta_worker.rb +432 -0
  107. data/server/lib/trigger.rb +34 -0
  108. data/test/bdrb_client_test_helper.rb +5 -0
  109. data/test/bdrb_test_helper.rb +35 -0
  110. data/test/client/backgroundrb.yml +17 -0
  111. data/test/client/test_bdrb_client_helper.rb +13 -0
  112. data/test/client/test_bdrb_cluster_connection.rb +162 -0
  113. data/test/client/test_bdrb_config.rb +20 -0
  114. data/test/client/test_bdrb_connection.rb +29 -0
  115. data/test/client/test_bdrb_job_queue.rb +63 -0
  116. data/test/client/test_worker_proxy.rb +130 -0
  117. data/test/server/test_cron_trigger.rb +281 -0
  118. data/test/server/test_master_proxy.rb +54 -0
  119. data/test/server/test_master_worker.rb +157 -0
  120. data/test/server/test_meta_worker.rb +281 -0
  121. data/test/server/test_result_storage.rb +14 -0
  122. data/test/socket_mocker.rb +34 -0
  123. data/test/workers/bar_worker.rb +10 -0
  124. data/test/workers/foo_worker.rb +10 -0
  125. data/uninstall.rb +1 -0
  126. metadata +345 -0
@@ -0,0 +1,281 @@
1
+ require File.join(File.dirname(__FILE__) + "/..","bdrb_test_helper")
2
+
3
+ context "Cront Trigger in general" do
4
+ specify "should let tasks running at given time interval run" do
5
+ # every 5 seconds
6
+ a = BackgrounDRb::CronTrigger.new("*/5 * * * * * *")
7
+ t_time = Time.parse("Wed Feb 13 20:53:43 +0530 2008")
8
+ firetime = a.fire_after_time(t_time)
9
+ firetime.min.should == 53
10
+ firetime.sec.should == 45
11
+ firetime.hour.should == 20
12
+
13
+ # 5 minute of every hour
14
+ a = BackgrounDRb::CronTrigger.new("0 5 * * * * *")
15
+ firetime = a.fire_after_time(t_time)
16
+ firetime.sec.should == 0
17
+ firetime.min.should == 5
18
+ firetime.hour.should == 21
19
+ firetime.day.should == 13
20
+
21
+ # every 5 minute
22
+ a = BackgrounDRb::CronTrigger.new("0 */5 * * * * *")
23
+ firetime = a.fire_after_time(t_time)
24
+ firetime.sec.should == 0
25
+ firetime.min.should == 55
26
+ firetime.hour.should == 20
27
+ firetime.day.should == 13
28
+
29
+ # every 5 AM of every day
30
+ a = BackgrounDRb::CronTrigger.new("0 0 5 * * * *")
31
+ firetime = a.fire_after_time(t_time)
32
+ firetime.hour.should == 5
33
+ firetime.min.should == 0
34
+ firetime.sec.should == 0
35
+ firetime.day.should == 14
36
+ firetime.month.should == 2
37
+
38
+ a = BackgrounDRb::CronTrigger.new("*/10 * * * * * ")
39
+ t_time = Time.parse("Wed Feb 13 23:17:55 +0530 2008")
40
+ firetime = a.fire_after_time(t_time)
41
+ firetime.hour.should == 23
42
+ firetime.min.should == 18
43
+ firetime.sec.should == 0
44
+ firetime.day.should == 13
45
+ firetime.month.should == 2
46
+ end
47
+
48
+ specify "should return correct firetime for hour intervals" do
49
+ # every 5 hour
50
+ t_time = Time.parse("Wed Feb 13 20:53:43 +0530 2008")
51
+ a = BackgrounDRb::CronTrigger.new("0 0 */5 * * * *")
52
+ firetime = a.fire_after_time(t_time)
53
+ firetime.hour.should == 0
54
+ firetime.min.should == 0
55
+ firetime.sec.should == 0
56
+ firetime.day.should == 14
57
+ firetime.month.should == 2
58
+ end
59
+
60
+ specify "should return firetime based on wday restriction" do
61
+ t_time = Time.parse("Wed Feb 13 20:53:43 +0530 2008")
62
+ # on sunday and monday it should run every 5 th hour
63
+ a = BackgrounDRb::CronTrigger.new("0 0 */5 * * 0-1 *")
64
+ firetime = a.fire_after_time(t_time)
65
+ firetime.hour.should == 0
66
+ firetime.min.should == 0
67
+ firetime.sec.should == 0
68
+ firetime.day.should == 17
69
+ firetime.month.should == 2
70
+
71
+ t_time2 = Time.parse("Sun Feb 17 20:53:43 +0530 2008")
72
+ firetime = a.fire_after_time(t_time2)
73
+ firetime.hour.should == 0
74
+ firetime.min.should == 0
75
+ firetime.sec.should == 0
76
+ firetime.day.should == 18
77
+ firetime.month.should == 2
78
+
79
+ end
80
+
81
+ specify "should wrap to next week for wday restirctions" do
82
+ a = BackgrounDRb::CronTrigger.new("0 0 */5 * * 0-1 *")
83
+ t_time = Time.parse("Mon Feb 18 20:53:43 +0530 2008")
84
+ firetime = a.fire_after_time(t_time)
85
+ firetime.hour.should == 0
86
+ firetime.min.should == 0
87
+ firetime.sec.should == 0
88
+ firetime.day.should == 24
89
+ firetime.month.should == 2
90
+ end
91
+
92
+ specify "should return firetime based on day restriction" do
93
+ t_time = Time.parse("Wed Feb 13 20:53:43 +0530 2008")
94
+ # 21st of every month run every 5 hour
95
+ a = BackgrounDRb::CronTrigger.new("0 0 */5 21 * * *")
96
+ firetime = a.fire_after_time(t_time)
97
+ firetime.hour.should == 0
98
+ firetime.min.should == 0
99
+ firetime.sec.should == 0
100
+ firetime.day.should == 21
101
+ firetime.month.should == 2
102
+
103
+ t_time = Time.parse("Wed Feb 22 20:53:43 +0530 2008")
104
+ # 21st of every month run every 5 hour
105
+ a = BackgrounDRb::CronTrigger.new("0 0 */5 21 * * *")
106
+ firetime = a.fire_after_time(t_time)
107
+ firetime.hour.should == 0
108
+ firetime.min.should == 0
109
+ firetime.sec.should == 0
110
+ firetime.day.should == 21
111
+ firetime.month.should == 3
112
+ end
113
+
114
+ specify "for feb month should take into account day count" do
115
+ t_time = Time.parse("Thu Feb 28 20:53:43 +0530 2008")
116
+ # 21st of every month run every 5 hour
117
+ a = BackgrounDRb::CronTrigger.new("0 0 */5 30 * * *")
118
+ firetime = a.fire_after_time(t_time)
119
+ firetime.hour.should == 0
120
+ firetime.min.should == 0
121
+ firetime.sec.should == 0
122
+ firetime.day.should == 30
123
+ firetime.month.should == 3
124
+ end
125
+
126
+ specify "should take care if number of days is not available in month" do
127
+ t_time = Time.parse("Tue Nov 12 20:53:43 +0530 2007")
128
+ # 21st of every month run every 5 hour
129
+ a = BackgrounDRb::CronTrigger.new("0 0 */5 31 * * *")
130
+ firetime = a.fire_after_time(t_time)
131
+ firetime.hour.should == 0
132
+ firetime.min.should == 0
133
+ firetime.sec.should == 0
134
+ firetime.day.should == 31
135
+ firetime.month.should == 12
136
+ end
137
+
138
+ specify "should take care of periodic variations in day restrictions" do
139
+ t_time = Time.parse("Tue Aug 12 20:53:43 +0530 2007")
140
+ a = BackgrounDRb::CronTrigger.new("0 0 */5 */2 1-5 * *")
141
+ firetime = a.fire_after_time(t_time)
142
+ firetime.hour.should == 0
143
+ firetime.min.should == 0
144
+ firetime.sec.should == 0
145
+ firetime.day.should == 1
146
+ firetime.month.should == 1
147
+ firetime.year.should == 2008
148
+
149
+ t_time = Time.parse("Tue Aug 12 20:53:43 +0530 2007")
150
+ a = BackgrounDRb::CronTrigger.new("0 0 */5 */3 1-5 * *")
151
+ firetime = a.fire_after_time(t_time)
152
+ firetime.hour.should == 0
153
+ firetime.min.should == 0
154
+ firetime.sec.should == 0
155
+ firetime.day.should == 1
156
+ firetime.month.should == 1
157
+ firetime.year.should == 2008
158
+
159
+ t_time = Time.parse("Tue Aug 12 20:53:43 +0530 2007")
160
+ a = BackgrounDRb::CronTrigger.new("0 0 */1 */7 1-5 * *")
161
+ firetime = a.fire_after_time(t_time)
162
+ firetime.hour.should == 0
163
+ firetime.min.should == 0
164
+ firetime.sec.should == 0
165
+ firetime.day.should == 1
166
+ firetime.month.should == 1
167
+ firetime.year.should == 2008
168
+ end
169
+
170
+ specify "should return firetime based on hour restriction" do
171
+ t_time = Time.parse("Wed Feb 13 20:53:43 +0530 2008")
172
+ a = BackgrounDRb::CronTrigger.new("0 0 */5 * * * *")
173
+ firetime = a.fire_after_time(t_time)
174
+ firetime.hour.should == 0
175
+ firetime.min.should == 0
176
+ firetime.sec.should == 0
177
+ firetime.day.should == 14
178
+ firetime.month.should == 2
179
+ end
180
+
181
+ specify "should take care of both fuck restrictions" do
182
+
183
+ # in case of conflict between day and wday options, we should chose one closer to current time
184
+ t_time = Time.parse("Tue Aug 12 20:53:43 +0530 2007")
185
+ a = BackgrounDRb::CronTrigger.new("0 0 */5 */3 1-5 3-5 *")
186
+ firetime = a.fire_after_time(t_time)
187
+ firetime.hour.should == 0
188
+ firetime.min.should == 0
189
+ firetime.sec.should == 0
190
+ firetime.day.should == 1
191
+ firetime.wday.should == 2
192
+ firetime.month.should == 1
193
+ firetime.year.should == 2008
194
+ end
195
+
196
+ specify "for user reported trigger" do
197
+ # on friday
198
+ t_time = Time.parse("Fri Jan 18 4:53:43 +0530 2008")
199
+ a = BackgrounDRb::CronTrigger.new("0 0/5 09-17 * * 1,3,5 *")
200
+ firetime = a.fire_after_time(t_time)
201
+ firetime.hour.should == 9
202
+ firetime.min.should == 0
203
+ firetime.sec.should == 0
204
+ firetime.wday.should == 5
205
+ firetime.month.should == 1
206
+ firetime.year.should == 2008
207
+
208
+ t_time = Time.parse("Fri Jan 18 9:53:43 +0530 2008")
209
+ a = BackgrounDRb::CronTrigger.new("0 0/5 09-17 * * 1,3,5 *")
210
+ firetime = a.fire_after_time(t_time)
211
+ firetime.hour.should == 9
212
+ firetime.min.should == 55
213
+ firetime.sec.should == 0
214
+ firetime.wday.should == 5
215
+ firetime.month.should == 1
216
+ firetime.day.should == 18
217
+ firetime.year.should == 2008
218
+
219
+ t_time = Time.parse("Sat Jan 19 4:53:43 +0530 2008")
220
+ a = BackgrounDRb::CronTrigger.new("0 0/5 09-17 * * 1,3,5 *")
221
+ firetime = a.fire_after_time(t_time)
222
+ firetime.hour.should == 9
223
+ firetime.min.should == 0
224
+ firetime.sec.should == 0
225
+ firetime.wday.should == 1
226
+ firetime.month.should == 1
227
+ firetime.day.should == 21
228
+ firetime.year.should == 2008
229
+
230
+ t_time = Time.parse("Mon Jan 21 4:53:43 +0530 2008")
231
+ a = BackgrounDRb::CronTrigger.new("0 0/5 09-17 * * 1,3,5 *")
232
+ firetime = a.fire_after_time(t_time)
233
+ firetime.hour.should == 9
234
+ firetime.min.should == 0
235
+ firetime.sec.should == 0
236
+ firetime.wday.should == 1
237
+ firetime.month.should == 1
238
+ firetime.day.should == 21
239
+ firetime.year.should == 2008
240
+
241
+ t_time = Time.parse("Tue Jan 1 4:53:43 +0530 2008")
242
+ a = BackgrounDRb::CronTrigger.new("0 0/5 09-17 * * 1,3,5 *")
243
+ firetime = a.fire_after_time(t_time)
244
+ firetime.hour.should == 9
245
+ firetime.min.should == 0
246
+ firetime.sec.should == 0
247
+ firetime.wday.should == 3
248
+ firetime.month.should == 1
249
+ firetime.day.should == 2
250
+ firetime.year.should == 2008
251
+ end
252
+
253
+ specify "should return firetime based on month restriction" do
254
+ t_time = Time.parse("Wed Feb 13 20:53:43 +0530 2008")
255
+ a = BackgrounDRb::CronTrigger.new("0 0 */5 * * * *")
256
+ firetime = a.fire_after_time(t_time)
257
+ firetime.hour.should == 0
258
+ firetime.min.should == 0
259
+ firetime.sec.should == 0
260
+ firetime.day.should == 14
261
+ firetime.month.should == 2
262
+ end
263
+
264
+ specify "should run for weekdays " do
265
+ t_time = Time.parse("Wed Feb 13 20:53:43 +0530 2008")
266
+ a = BackgrounDRb::CronTrigger.new("0 0 2 * * 1-5 *")
267
+ firetime = a.fire_after_time(t_time)
268
+ firetime.hour.should == 2
269
+ firetime.min.should == 0
270
+ firetime.sec.should == 0
271
+ firetime.wday.should == 4
272
+
273
+ t_time = Time.parse("Fri June 6 20:53:43 +0530 2008")
274
+ a = BackgrounDRb::CronTrigger.new("0 0 2 * * 1-5 *")
275
+ firetime = a.fire_after_time(t_time)
276
+ firetime.hour.should == 2
277
+ firetime.min.should == 0
278
+ firetime.sec.should == 0
279
+ firetime.wday.should == 1
280
+ end
281
+ end
@@ -0,0 +1,54 @@
1
+ require File.join(File.dirname(__FILE__) + "/..","bdrb_test_helper")
2
+ require "mocha"
3
+
4
+ context "Master proxy for reloadable workers" do
5
+ ENV["RAILS_ENV"] = "production"
6
+
7
+ setup do
8
+ BDRB_CONFIG.set({:schedules=>
9
+ {
10
+ :foo_worker => { :barbar => {:trigger_args=>"*/5 * * * * *", :data =>"Hello World" }},
11
+ :bar_worker => { :do_job => {:trigger_args=>"*/5 * * * * *", :data =>"Hello World" }}
12
+ },
13
+ :backgroundrb=> {:log => "foreground", :debug_log => false, :environment => "production", :port => 11006, :ip => "localhost"}
14
+ })
15
+
16
+ Packet::Reactor.stubs(:run)
17
+ @master_proxy = BackgrounDRb::MasterProxy.new
18
+ end
19
+
20
+ specify "should load schedule of workers which are reloadable" do
21
+ @master_proxy.reloadable_workers.should.not == []
22
+ @master_proxy.reloadable_workers.should.include(BarWorker)
23
+ @master_proxy.reloadable_workers.should.include(FooWorker)
24
+ @master_proxy.worker_triggers.should.not.be {}
25
+ assert @master_proxy.worker_triggers.keys.include?(:bar_worker)
26
+ assert @master_proxy.worker_triggers[:bar_worker].keys.include?(:do_job)
27
+
28
+ @master_proxy.worker_triggers[:bar_worker][:do_job].should.not.be { }
29
+ end
30
+
31
+ specify "load schedule should load schedule of worker specified" do
32
+ sleep(5)
33
+ sheep = mock()
34
+ sheep.expects(:send_request).at_most(4)
35
+ live_workers = Hash.new(sheep)
36
+ crap_reactor = mock()
37
+ crap_reactor.expects(:start_worker).at_most(2)
38
+ crap_reactor.expects(:live_workers).at_most(4).returns(live_workers)
39
+ @master_proxy.reactor = crap_reactor
40
+ @master_proxy.reload_workers
41
+ end
42
+
43
+ specify "should not run worker methods which are not ready to run" do
44
+ sleep(1)
45
+ sheep = mock()
46
+ sheep.expects(:send_request).at_most(4)
47
+ live_workers = Hash.new(sheep)
48
+ crap_reactor = mock()
49
+ crap_reactor.expects(:start_worker).at_most(2)
50
+ crap_reactor.expects(:live_workers).at_most(4).returns(live_workers)
51
+ @master_proxy.reactor = crap_reactor
52
+ @master_proxy.reload_workers
53
+ end
54
+ end
@@ -0,0 +1,157 @@
1
+ require File.join(File.dirname(__FILE__) + "/..","bdrb_test_helper")
2
+
3
+
4
+ context "Master Worker in general should" do
5
+ def packet_dump data
6
+ t = Marshal.dump(data)
7
+ t.length.to_s.rjust(9,'0') + t
8
+ end
9
+
10
+ setup do
11
+ @master_worker = BackgrounDRb::MasterWorker.new
12
+ @master_worker.post_init
13
+ class << @master_worker
14
+ attr_accessor :outgoing_data
15
+ attr_accessor :key,:live_workers,:excpetion_type
16
+ attr_accessor :going_to_user
17
+
18
+ def packet_classify(original_string)
19
+ word_parts = original_string.split('_')
20
+ return word_parts.map { |x| x.capitalize}.join
21
+ end
22
+
23
+ def gen_worker_key(worker_name,worker_key = nil)
24
+ return worker_name if worker_key.nil?
25
+ return "#{worker_name}_#{worker_key}".to_sym
26
+ end
27
+ def ask_worker key,data
28
+ case @excpetion_type
29
+ when :disconnect
30
+ raise Packet::DisconnectError.new("boy")
31
+ when :generic
32
+ raise "Crap"
33
+ else
34
+ @key = key
35
+ @outgoing_data = data
36
+ end
37
+ end
38
+
39
+ def send_object data
40
+ @going_to_user = data
41
+ end
42
+
43
+ def start_worker data
44
+ @outgoing_data = data
45
+ end
46
+
47
+ def ask_for_exception type
48
+ @excpetion_type = type
49
+ end
50
+ end
51
+
52
+ class DummyLogger
53
+ def method_missing method_id,*args;
54
+ puts *args
55
+ end
56
+ end
57
+ logger = DummyLogger.new
58
+ @master_worker.debug_logger = logger
59
+ end
60
+
61
+ specify "read data according to binary protocol and recreate objects" do
62
+ sync_request = {
63
+ :type=>:sync_invoke, :arg=>"boy", :worker=>:foo_worker, :worker_method=>"barbar"
64
+ }
65
+ @master_worker.expects(:method_invoke).with(sync_request).returns(nil)
66
+ @master_worker.receive_data(packet_dump(sync_request))
67
+ end
68
+
69
+ specify "ignore errors while recreating object" do
70
+ sync_request = {
71
+ :type=>:sync_invoke, :arg=>"boy", :worker=>:foo_worker, :worker_method=>"barbar"
72
+ }
73
+ foo = packet_dump(sync_request)
74
+ foo[0] = 'h'
75
+ @master_worker.expects(:method_invoke).never
76
+ @master_worker.receive_data(foo)
77
+ end
78
+
79
+ specify "should route async requests" do
80
+ b = {
81
+ :type=>:async_invoke, :arg=>"boy", :worker=>:foo_worker, :worker_method=>"barbar"
82
+ }
83
+
84
+ @master_worker.expects(:worker_methods).returns(["barbar"])
85
+ @master_worker.receive_data(packet_dump(b))
86
+ @master_worker.outgoing_data.should == {:type=>:request, :data=>{:worker_method=>"barbar", :arg=>"boy"}, :result=>false}
87
+ @master_worker.going_to_user.should == { :result_flag => "ok" }
88
+ @master_worker.key.should == :foo_worker
89
+ end
90
+
91
+ specify "should route sync requests and return results" do
92
+ a = {:type=>:sync_invoke, :arg=>"boy", :worker=>:foo_worker, :worker_method=>"barbar"}
93
+ @master_worker.receive_data(packet_dump(a))
94
+ @master_worker.outgoing_data.should == {:type=>:request, :data=>{:worker_method=>"barbar", :arg=>"boy"}, :result=>true}
95
+ @master_worker.key.should == :foo_worker
96
+ end
97
+
98
+ specify "should route start worker requests" do
99
+ d = {:worker_key=>"boy", :type=>:start_worker, :worker=>:foo_worker}
100
+ @master_worker.receive_data(packet_dump(d))
101
+ @master_worker.outgoing_data.should == {:type=>:start_worker, :worker_key=>"boy", :worker=>:foo_worker}
102
+ end
103
+
104
+ # FIXME: this test should be further broken down
105
+ specify "should run delete worker requests itself" do
106
+ e = {:worker_key=>"boy", :type=>:delete_worker, :worker=>:foo_worker}
107
+ @master_worker.expects(:delete_drb_worker).returns(nil)
108
+ @master_worker.receive_data(packet_dump(e))
109
+ end
110
+
111
+ specify "should route worker info requests" do
112
+ g = {:worker_key=>"boy", :type=>:worker_info, :worker=>:foo_worker}
113
+ t_reactor = stub()
114
+ live_workers = stub()
115
+ live_workers.expects(:[]).returns(nil)
116
+ t_reactor.expects(:live_workers).returns(live_workers)
117
+ @master_worker.expects(:send_object).with({:worker_key=>"boy", :worker=>:foo_worker, :status=>:stopped}).returns(true)
118
+ @master_worker.expects(:reactor).returns(t_reactor)
119
+ @master_worker.receive_data(packet_dump(g))
120
+ end
121
+
122
+ specify "should route all_worker_info requests" do
123
+ f = {:type=>:all_worker_info}
124
+ t_reactor = stub()
125
+ live_workers = stub()
126
+ live_workers.stubs(:each).returns(:foo,mock())
127
+ t_reactor.expects(:live_workers).returns(live_workers)
128
+ @master_worker.expects(:send_object).returns(true)
129
+ @master_worker.expects(:reactor).returns(t_reactor)
130
+
131
+ @master_worker.receive_data(packet_dump(f))
132
+ end
133
+
134
+ specify "should route worker result requests" do
135
+ c = {:type=>:get_result, :worker=>:foo_worker, :job_key=>:start_message}
136
+ @master_worker.receive_data(packet_dump(c))
137
+ @master_worker.outgoing_data.should == {:type=>:get_result, :data=>{:job_key=>:start_message}, :result=>true}
138
+ end
139
+
140
+ specify "should remove the worker from list if error while fetching results" do
141
+ c = {:type=>:get_result, :worker=>:foo_worker, :job_key=>:start_message}
142
+ @master_worker.ask_for_exception(:disconnect)
143
+ t_reactor = mock()
144
+ live_workers = mock()
145
+ live_workers.expects(:delete).returns(true)
146
+ t_reactor.expects(:live_workers).returns(live_workers)
147
+ @master_worker.expects(:reactor).returns(t_reactor)
148
+ @master_worker.receive_data(packet_dump(c))
149
+ end
150
+
151
+ specify "should ignore generic exceptions while fetching results" do
152
+ c = {:type=>:get_result, :worker=>:foo_worker, :job_key=>:start_message}
153
+ @master_worker.ask_for_exception(:generic)
154
+ @master_worker.receive_data(packet_dump(c))
155
+ @master_worker.outgoing_data.should == nil
156
+ end
157
+ end