infopark-politics 0.3.1 → 0.3.2pg55d4620

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,14 @@
1
+ # encoding: utf-8
1
2
  # FIXME oberen Teil in spec_helper.rb auslagern
2
3
  require 'rubygems'
3
4
  $:.unshift(File.dirname(__FILE__) + '/../lib')
4
5
  require File.dirname(__FILE__) + '/../lib/init'
5
6
  Politics::log.level = Logger::FATAL
6
7
 
7
- @@memcache_client = nil
8
-
9
8
  class UninitializedWorker
10
9
  include Politics::StaticQueueWorker
11
- def initialize
10
+ def initialize(memcache_client)
11
+ @memcache_client = memcache_client
12
12
  log.level = Logger::FATAL
13
13
  end
14
14
 
@@ -19,126 +19,157 @@ class UninitializedWorker
19
19
  end
20
20
 
21
21
  def client_for(servers)
22
- @@memcache_client
22
+ @memcache_client
23
23
  end
24
24
 
25
25
  def local_ip
26
26
  IPAddr.new("127.0.0.1")
27
27
  end
28
- end
29
28
 
30
- class Worker < UninitializedWorker
31
29
  def at_exit
32
30
  end
31
+ end
33
32
 
34
- def initialize
33
+ class Worker < UninitializedWorker
34
+ def initialize(memcache_client)
35
35
  super
36
36
  register_worker 'worker', 10, :iteration_length => 10
37
37
  end
38
38
  end
39
39
 
40
40
  describe UninitializedWorker do
41
- before do
42
- @@memcache_client = mock('memcache', :set => nil, :get => nil)
43
- @worker = UninitializedWorker.new
44
- end
41
+ let(:memcache_client) { double('memcache', :set => nil, :get => nil) }
42
+ let(:worker) { UninitializedWorker.new memcache_client}
43
+
44
+ describe "when initializing" do
45
+ it "should register the removal of the leadership as exit handler" do
46
+ worker.should_receive(:at_exit).ordered.and_return {|&h| h}
47
+ handler = worker.register_worker('worker', 10, :iteration_length => 10)
48
+
49
+ worker.should_receive(:cleanup).ordered
50
+ handler.call
51
+ end
52
+
53
+ it "should have a druby url" do
54
+ worker.register_worker('worker', 10, :iteration_length => 10)
55
+ worker.uri.should =~ %r|druby://.*:[0-9]+|
56
+ end
57
+
58
+ it "should not have a hostname" do
59
+ worker.hostname.should be_nil
60
+ end
45
61
 
46
- it "should register the removal of the leadership as exit handler" do
47
- @worker.should_receive(:at_exit).ordered.and_return {|h| h}
48
- handler = @worker.register_worker('worker', 10, :iteration_length => 10)
62
+ context "when it has a hostname" do
63
+ before do
64
+ worker.stub(:hostname).and_return '127.0.0.1'
65
+ end
49
66
 
50
- @worker.should_receive(:cleanup).ordered
51
- handler.call
67
+ it "should use it" do
68
+ worker.register_worker('worker', 10, :iteration_length => 10)
69
+ worker.uri.should =~ %r|druby://127.0.0.1:[0-9]+|
70
+ end
71
+ end
72
+
73
+ context "when it does not have a hostname" do
74
+ before do
75
+ worker.stub(:hostname).and_return nil
76
+ end
77
+
78
+ it "should use the systems hostname" do
79
+ worker.register_worker('worker', 10, :iteration_length => 10)
80
+ worker.uri.should =~ %r|druby://localhost:[0-9]+|
81
+ end
82
+ end
52
83
  end
53
84
  end
54
85
 
55
86
  describe Worker do
56
- before do
57
- @@memcache_client = mock('memcache', :set => nil, :get => nil)
58
- @worker = Worker.new
59
- end
87
+ let(:memcache_client) { double('memcache', :set => nil, :get => nil) }
88
+ let(:worker) { Worker.new memcache_client }
60
89
 
61
90
  it "should provide 'until_next_iteration' even if nominate was not completed" do
62
- @worker.until_next_iteration
91
+ worker.until_next_iteration
63
92
  end
64
93
 
65
94
  it "should return time to next iteration even if nominate was not completed" do
66
- @worker.until_next_iteration.should > 0
67
- @worker.until_next_iteration.should <= 10
95
+ worker.until_next_iteration.should > 0
96
+ worker.until_next_iteration.should <= 10
68
97
  end
69
98
 
70
99
  it "should give access to the uri" do
71
- @worker.uri.should =~ %r(^druby://)
100
+ worker.uri.should =~ %r(^druby://)
72
101
  end
73
102
 
74
103
  it "should be alive" do
75
- @worker.should be_alive
104
+ worker.should be_alive
76
105
  end
77
106
 
78
107
  describe "when processing bucket" do
79
108
  before do
80
- DRbObject.stub!(:new).with(nil, @worker.uri).
81
- and_return(@worker_drb = mock('drb', :alive? => true))
109
+ DRbObject.stub(:new).with(nil, worker.uri).
110
+ and_return(@worker_drb = double('drb', :alive? => true))
82
111
  end
83
112
 
84
113
  it "should raise an error if it is not alive via Drb" do
85
- @worker_drb.stub!(:alive?).and_raise("drb error")
86
- lambda {@worker.start}.should raise_error(/cannot reach self/)
87
- @worker_drb.stub!(:alive?).and_return(false)
88
- lambda {@worker.start}.should raise_error(/not alive/)
114
+ @worker_drb.stub(:alive?).and_raise("drb error")
115
+ lambda {worker.start}.should raise_error(/cannot reach self/)
116
+ @worker_drb.stub(:alive?).and_return(false)
117
+ lambda {worker.start}.should raise_error(/not alive/)
89
118
  end
90
119
 
91
120
  describe "" do
92
121
  before do
93
- @worker.stub!(:until_next_iteration).and_return 666
94
- @worker.stub!(:nominate)
95
- @worker.should_receive(:loop?).and_return true, true, true, false
122
+ worker.stub(:until_next_iteration).and_return 666
123
+ worker.stub(:nominate)
124
+ worker.stub(:loop?).and_return true, true, true, false
96
125
  end
97
126
 
98
127
  it "should relax until next iteration on MemCache errors during nomination" do
99
- @worker.should_receive(:nominate).exactly(4).and_raise MemCache::MemCacheError.new("Buh!")
100
- @worker.should_receive(:relax).with(666).exactly(4).times
128
+ worker.should_receive(:nominate).exactly(4).and_raise MemCache::MemCacheError.new("Buh!")
129
+ worker.should_receive(:relax).with(666).exactly(4).times
101
130
 
102
- @worker.start
131
+ worker.start
103
132
  end
104
133
 
105
134
  it "should relax until next iteration on MemCache errors during request for leader" do
106
- @worker.should_receive(:leader_uri).exactly(4).and_raise(MemCache::MemCacheError.new("Buh"))
107
- @worker.should_receive(:relax).with(666).exactly(4).times
135
+ worker.should_receive(:leader_uri).exactly(4).and_raise(MemCache::MemCacheError.new("Buh"))
136
+ worker.should_receive(:relax).with(666).exactly(4).times
108
137
 
109
- @worker.start
138
+ worker.start
110
139
  end
111
140
 
112
141
  describe "as leader" do
113
142
  before do
114
- @worker.stub!(:leader?).and_return true
143
+ worker.stub(:leader?).and_return true
115
144
  end
116
145
 
117
146
  it "should do leader duties" do
118
- @worker.should_receive(:perform_leader_duties).exactly(4).times
119
- @worker.start
147
+ worker.should_receive(:perform_leader_duties).exactly(4).times
148
+ worker.start
120
149
  end
121
150
  end
122
151
 
123
152
  describe "as follower" do
124
153
  before do
125
- @worker.stub!(:leader?).and_return false
126
- @worker.stub!(:leader_uri).and_return "the leader"
127
- @worker.stub!(:leader).and_return(@leader = mock('leader'))
128
- @leader.stub!(:bucket_request).and_return([1, 0])
154
+ worker.stub(:leader?).and_return false
155
+ worker.stub(:leader_uri).and_return "the leader"
156
+ worker.stub(:leader).and_return(@leader = double('leader'))
157
+ @leader.stub(:bucket_request).and_return([1, 0])
129
158
  end
130
159
 
131
160
  it "should get the bucket to process from the leader at every iteration" do
132
- @worker.should_receive(:leader).exactly(4).times.and_return @leader
133
- @leader.should_receive(:bucket_request).with(@worker.uri).exactly(4).times.
161
+ worker.should_receive(:leader).exactly(4).times.and_return @leader
162
+ @leader.should_receive(:bucket_request).with(worker.uri).exactly(4).times.
134
163
  and_return([1, 2])
135
- @worker.start
164
+ worker.start
136
165
  end
137
166
 
138
167
  it "should exit on :stop bucket" do
139
168
  @leader.should_receive(:bucket_request).ordered.once.and_return([:stop, 0])
140
- @worker.should_receive(:exit).with(0).ordered
141
- @worker.start
169
+ worker.should_receive(:exit).with(0).ordered.and_return do
170
+ worker.should_receive(:loop?).and_return false
171
+ end
172
+ worker.start
142
173
  end
143
174
  end
144
175
  end
@@ -147,166 +178,166 @@ describe Worker do
147
178
  describe "when handling a bucket request" do
148
179
  describe "as leader" do
149
180
  before do
150
- @worker.stub!(:leader?).and_return true
181
+ worker.stub(:leader?).and_return true
151
182
  end
152
183
 
153
184
  it "should deliver the bucket" do
154
- @worker.should_receive(:next_bucket).with("requestor").and_return "the bucket"
155
- @worker.bucket_request("requestor").should == "the bucket"
185
+ worker.should_receive(:next_bucket).with("requestor").and_return "the bucket"
186
+ worker.bucket_request("requestor").should == "the bucket"
156
187
  end
157
188
 
158
189
  describe "when no buckets are left" do
159
190
  before do
160
- @worker.stub!(:find_workers).and_return(%w(1 2 3))
161
- @worker.populate_followers_to_stop
162
- DRbObject.stub(:new).and_return(mock('o', :alive? => true))
191
+ worker.stub(:find_workers).and_return(%w(1 2 3))
192
+ worker.populate_followers_to_stop
193
+ DRbObject.stub(:new).and_return(double('o', :alive? => true))
163
194
  end
164
195
 
165
196
  it "should deliver the :stop bucket if requestor is in followers_to_stop list" do
166
- @worker.bucket_request("1").should == [:stop, 0]
197
+ worker.bucket_request("1").should == [:stop, 0]
167
198
  end
168
199
 
169
200
  it "should not deliver the :stop bucket if requestor is not in followers_to_stop list" do
170
- @worker.bucket_request("requestor")[0].should be_nil
201
+ worker.bucket_request("requestor")[0].should be_nil
171
202
  end
172
203
 
173
204
  it "should remove the requestor from the followers_to_stop list" do
174
- @worker.bucket_request("2")
175
- @worker.followers_to_stop.should == %w(1 3)
205
+ worker.bucket_request("2")
206
+ worker.followers_to_stop.should =~ %w(1 3)
176
207
  end
177
208
  end
178
209
  end
179
210
 
180
211
  describe "as follower" do
181
212
  before do
182
- @worker.stub!(:leader?).and_return false
213
+ worker.stub(:leader?).and_return false
183
214
  end
184
215
 
185
216
  it "should deliver the :not_leader bucket" do
186
- @worker.bucket_request("requestor")[0].should == :not_leader
217
+ worker.bucket_request("requestor")[0].should == :not_leader
187
218
  end
188
219
  end
189
220
  end
190
221
 
191
222
  describe "when determining if restart is wanted" do
192
223
  it "should return true if the restart flag is set in memcache" do
193
- @@memcache_client.should_receive(:get).with('worker_restart').and_return true
194
- @worker.should be_restart_wanted
224
+ memcache_client.should_receive(:get).with('worker_restart').and_return true
225
+ worker.should be_restart_wanted
195
226
  end
196
227
 
197
228
  it "should return false if the restart flag is not set in memcache" do
198
- @@memcache_client.should_receive(:get).with('worker_restart').and_return false
199
- @worker.should_not be_restart_wanted
200
- @@memcache_client.should_receive(:get).with('worker_restart').and_return nil
201
- @worker.should_not be_restart_wanted
229
+ memcache_client.should_receive(:get).with('worker_restart').and_return false
230
+ worker.should_not be_restart_wanted
231
+ memcache_client.should_receive(:get).with('worker_restart').and_return nil
232
+ worker.should_not be_restart_wanted
202
233
  end
203
234
  end
204
235
 
205
236
  describe "when performing leader duties" do
206
237
  before do
207
- @worker.stub!(:until_next_iteration).and_return 0
208
- @worker.stub!(:leader?).and_return true
209
- @worker.stub!(:dictatorship_length).and_return 666
210
- @worker.stub!(:iteration_length).and_return 5
211
- @worker.stub!(:find_workers).and_return []
238
+ worker.stub(:until_next_iteration).and_return 0
239
+ worker.stub(:leader?).and_return true
240
+ worker.stub(:dictatorship_length).and_return 666
241
+ worker.stub(:iteration_length).and_return 5
242
+ worker.stub(:find_workers).and_return []
212
243
  end
213
244
 
214
245
  it "should initialize buckets as dictator" do
215
- @worker.should_receive(:seize_leadership).with(666).ordered
216
- @worker.should_receive(:initialize_buckets).ordered
217
- @worker.should_receive(:seize_leadership).ordered
218
- @worker.perform_leader_duties
246
+ worker.should_receive(:seize_leadership).with(666).ordered
247
+ worker.should_receive(:initialize_buckets).ordered
248
+ worker.should_receive(:seize_leadership).ordered
249
+ worker.perform_leader_duties
219
250
  end
220
251
 
221
252
  describe "as long as there are buckets" do
222
253
  before do
223
- @worker.stub!(:buckets).and_return([1], [2], [3], [4], [])
224
- @worker.stub!(:relax)
254
+ worker.stub(:buckets).and_return([1], [2], [3], [4], [])
255
+ worker.stub(:relax)
225
256
  end
226
257
 
227
258
  it "should update buckets periodically" do
228
- @worker.should_receive(:update_buckets).exactly(4).times
229
- @worker.perform_leader_duties
259
+ worker.should_receive(:update_buckets).exactly(4).times
260
+ worker.perform_leader_duties
230
261
  end
231
262
 
232
263
  it "should relax half of the time to the next iteration" do
233
- @worker.stub!(:until_next_iteration).and_return(6)
234
- @worker.should_receive(:relax).with(3).exactly(4).times
235
- @worker.perform_leader_duties
264
+ worker.stub(:until_next_iteration).and_return(6)
265
+ worker.should_receive(:relax).with(3).exactly(4).times
266
+ worker.perform_leader_duties
236
267
  end
237
268
 
238
269
  it "should seize the leadership periodically" do
239
- @worker.should_receive(:seize_leadership).at_least(4).times
240
- @worker.perform_leader_duties
270
+ worker.should_receive(:seize_leadership).at_least(4).times
271
+ worker.perform_leader_duties
241
272
  end
242
273
 
243
274
  it "should seize the leadership periodically even if restart is wanted" do
244
- @worker.stub!(:restart_wanted?).and_return true
245
- @worker.stub!(:exit)
246
- @worker.should_receive(:seize_leadership).at_least(4).times
247
- @worker.perform_leader_duties
275
+ worker.stub(:restart_wanted?).and_return true
276
+ worker.stub(:exit)
277
+ worker.should_receive(:seize_leadership).at_least(4).times
278
+ worker.perform_leader_duties
248
279
  end
249
280
 
250
281
  it "should not update buckets if restart is wanted" do
251
- @worker.stub!(:restart_wanted?).and_return true
252
- @worker.stub!(:exit)
253
- @worker.should_not_receive(:update_buckets)
254
- @worker.perform_leader_duties
282
+ worker.stub(:restart_wanted?).and_return true
283
+ worker.stub(:exit)
284
+ worker.should_not_receive(:update_buckets)
285
+ worker.perform_leader_duties
255
286
  end
256
287
  end
257
288
 
258
289
  describe "if there are no more buckets" do
259
290
  before do
260
- @worker.stub!(:buckets).and_return([])
291
+ worker.stub(:buckets).and_return([])
261
292
  end
262
293
 
263
294
  it "should populate the followers_to_stop list before evaluating it if restart is wanted" do
264
- @worker.stub!(:restart_wanted?).and_return true
265
- @worker.stub!(:exit)
266
- @worker.should_receive(:populate_followers_to_stop).ordered.once
267
- @worker.should_receive(:followers_to_stop).ordered.and_return []
268
- @worker.perform_leader_duties
295
+ worker.stub(:restart_wanted?).and_return true
296
+ worker.stub(:exit)
297
+ worker.should_receive(:populate_followers_to_stop).ordered.once
298
+ worker.should_receive(:followers_to_stop).ordered.and_return []
299
+ worker.perform_leader_duties
269
300
  end
270
301
 
271
302
  it "should not populate the followers_to_stop list if restart is not wanted" do
272
- @worker.stub!(:restart_wanted?).and_return false
273
- @worker.should_not_receive(:populate_followers_to_stop)
274
- @worker.perform_leader_duties
303
+ worker.stub(:restart_wanted?).and_return false
304
+ worker.should_not_receive(:populate_followers_to_stop)
305
+ worker.perform_leader_duties
275
306
  end
276
307
 
277
308
  describe "as long as there are followers to stop" do
278
309
  before do
279
- @worker.stub!(:followers_to_stop).and_return([1], [2], [3], [4], [])
280
- @worker.stub!(:relax)
310
+ worker.stub(:followers_to_stop).and_return([1], [2], [3], [4], [])
311
+ worker.stub(:relax)
281
312
  end
282
313
 
283
314
  it "should relax half of the time to the next iteration" do
284
- @worker.stub!(:until_next_iteration).and_return(6)
285
- @worker.should_receive(:relax).with(3).exactly(4).times
286
- @worker.perform_leader_duties
315
+ worker.stub(:until_next_iteration).and_return(6)
316
+ worker.should_receive(:relax).with(3).exactly(4).times
317
+ worker.perform_leader_duties
287
318
  end
288
319
 
289
320
  it "should seize the leadership periodically" do
290
- @worker.should_receive(:seize_leadership).at_least(4).times
291
- @worker.perform_leader_duties
321
+ worker.should_receive(:seize_leadership).at_least(4).times
322
+ worker.perform_leader_duties
292
323
  end
293
324
  end
294
325
 
295
326
  describe "if there are no more followers to stop" do
296
327
  before do
297
- @worker.stub!(:followers_to_stop).and_return([])
328
+ worker.stub(:followers_to_stop).and_return([])
298
329
  end
299
330
 
300
331
  it "should relax until next iteration" do
301
- @worker.stub!(:until_next_iteration).and_return(6)
302
- @worker.should_receive(:relax).with(6).once
303
- @worker.perform_leader_duties
332
+ worker.stub(:until_next_iteration).and_return(6)
333
+ worker.should_receive(:relax).with(6).once
334
+ worker.perform_leader_duties
304
335
  end
305
336
 
306
337
  it "should exit if restart is wanted" do
307
- @worker.stub!(:restart_wanted?).and_return true
308
- @worker.should_receive(:exit).with(0)
309
- @worker.perform_leader_duties
338
+ worker.stub(:restart_wanted?).and_return true
339
+ worker.should_receive(:exit).with(0)
340
+ worker.perform_leader_duties
310
341
  end
311
342
  end
312
343
  end
@@ -314,140 +345,134 @@ describe Worker do
314
345
 
315
346
  describe "when seizing leadership" do
316
347
  before do
317
- @worker.stub!(:uri).and_return('myself')
318
- @worker.stub!(:iteration_length).and_return 123
319
- @worker.stub!(:token).and_return('dcc-group')
348
+ worker.stub(:uri).and_return('myself')
349
+ worker.stub(:iteration_length).and_return 123
350
+ worker.stub(:token).and_return('dcc-group')
320
351
  end
321
352
 
322
353
  it "should set itself to leader" do
323
- @@memcache_client.should_receive(:set).with(anything(), 'myself', anything())
324
- @worker.seize_leadership
354
+ memcache_client.should_receive(:set).with(anything(), 'myself', anything())
355
+ worker.seize_leadership
325
356
  end
326
357
 
327
358
  it "should seize the leadership for the amount of seconds given" do
328
- @@memcache_client.should_receive(:set).with(anything(), anything(), 666)
329
- @worker.seize_leadership 666
359
+ memcache_client.should_receive(:set).with(anything(), anything(), 666)
360
+ worker.seize_leadership 666
330
361
  end
331
362
 
332
363
  it "should seize the leadership for iteration_length if no duration is given" do
333
- @@memcache_client.should_receive(:set).with(anything(), anything(), 123)
334
- @worker.seize_leadership
364
+ memcache_client.should_receive(:set).with(anything(), anything(), 123)
365
+ worker.seize_leadership
335
366
  end
336
367
 
337
368
  it "should seize the leadership for the worker's group" do
338
- @@memcache_client.should_receive(:set).with('dcc-group', anything(), anything())
339
- @worker.seize_leadership
369
+ memcache_client.should_receive(:set).with('dcc-group', anything(), anything())
370
+ worker.seize_leadership
340
371
  end
341
372
 
342
373
  it "should have the next iteration exactly when the seized leadership ends" do
343
374
  now = Time.now
344
- Time.stub!(:now).and_return now
375
+ Time.stub(:now).and_return now
345
376
  end_of_leadership = now + 666
346
377
 
347
- @worker.seize_leadership 666
348
- @worker.until_next_iteration.should == 666
378
+ worker.seize_leadership 666
379
+ worker.until_next_iteration.should == 666
349
380
 
350
- @worker.seize_leadership
351
- @worker.until_next_iteration.should == 123
381
+ worker.seize_leadership
382
+ worker.until_next_iteration.should == 123
352
383
 
353
- @worker.seize_leadership 6
354
- @worker.until_next_iteration.should == 6
384
+ worker.seize_leadership 6
385
+ worker.until_next_iteration.should == 6
355
386
  end
356
387
  end
357
388
 
358
389
  describe "when creating next bucket" do
359
390
  it "should set the sleep time to sleep_until_next_bucket_time" do
360
- @worker.should_receive(:sleep_until_next_bucket_time).and_return 'the sleep time'
361
- @worker.next_bucket('')[1].should == 'the sleep time'
391
+ worker.should_receive(:sleep_until_next_bucket_time).and_return 'the sleep time'
392
+ worker.next_bucket('')[1].should == 'the sleep time'
362
393
  end
363
394
  end
364
395
 
365
396
  describe "when computing the sleep_until_next_bucket_time" do
366
397
  before do
367
- @worker.stub!(:iteration_length).and_return 10
368
- @worker.stub!(:until_next_iteration).and_return 6
398
+ worker.stub(:iteration_length).and_return 10
399
+ worker.stub(:until_next_iteration).and_return 6
369
400
  end
370
401
 
371
402
  it "should set the sleep time to half the time until_next_iteration" do
372
- @worker.sleep_until_next_bucket_time.should == 3
403
+ worker.sleep_until_next_bucket_time.should == 3
373
404
  end
374
405
 
375
406
  it "should set the sleep time to at least 1 second" do
376
- @worker.stub!(:until_next_iteration).and_return 0.6
377
- @worker.sleep_until_next_bucket_time.should == 1
407
+ worker.stub(:until_next_iteration).and_return 0.6
408
+ worker.sleep_until_next_bucket_time.should == 1
378
409
  end
379
410
 
380
411
  it "should set the sleep time to at most a half of the interation_length" do
381
- @worker.stub!(:until_next_iteration).and_return 60
382
- @worker.sleep_until_next_bucket_time.should == 5
412
+ worker.stub(:until_next_iteration).and_return 60
413
+ worker.sleep_until_next_bucket_time.should == 5
383
414
  end
384
415
  end
385
416
 
386
417
  describe "when providing leader object" do
387
418
  before do
388
- @worker.stub!(:until_next_iteration).and_return 0
419
+ worker.stub(:until_next_iteration).and_return 0
389
420
  end
390
421
 
391
422
  it "should return a drb object with the leader uri" do
392
- @worker.stub!(:leader_uri).and_return("leader's uri")
423
+ worker.stub(:leader_uri).and_return("leader's uri")
393
424
  DRbObject.should_receive(:new).with(nil, "leader's uri").and_return "leader"
394
- @worker.leader.should == "leader"
425
+ worker.leader.should == "leader"
395
426
  end
396
427
 
397
428
  it "should try three times to get the leader on anarchy (no leader)" do
398
- @worker.should_receive(:leader_uri).at_least(3).times.and_return nil
399
- begin
400
- @worker.leader
401
- rescue
402
- end
429
+ worker.should_receive(:leader_uri).at_least(3).times.and_return nil
430
+ worker.leader rescue nil
403
431
  end
404
432
 
405
433
  it "should raise an error when leader cannot be determined during anarchy" do
406
- @worker.stub!(:leader_uri).and_return nil
407
- lambda {@worker.leader}.should raise_error(/cannot determine leader/)
434
+ worker.stub(:leader_uri).and_return nil
435
+ lambda {worker.leader}.should raise_error(/cannot determine leader/)
408
436
  end
409
437
 
410
438
  it "should sleep until next iteration before retrying to get leader" do
411
- @worker.stub!(:leader_uri).and_return nil
412
- @worker.stub!(:until_next_iteration).and_return 666
413
- @worker.should_receive(:relax).with(666).exactly(2).times
414
- begin
415
- @worker.leader
416
- rescue
417
- end
439
+ worker.stub(:leader_uri).and_return nil
440
+ worker.stub(:until_next_iteration).and_return 666
441
+ worker.should_receive(:relax).with(666).exactly(2).times
442
+ worker.leader rescue nil
418
443
  end
419
444
  end
420
445
 
421
446
  describe "when cleaning up" do
422
447
  before do
423
- @worker.stub!(:group_name).and_return('the group')
448
+ worker.stub(:group_name).and_return('the group')
424
449
  end
425
450
 
426
451
  describe "as leader" do
427
452
  before do
428
- @worker.stub!(:leader?).and_return true
429
- @@memcache_client.stub!(:delete)
453
+ worker.stub(:leader?).and_return true
454
+ memcache_client.stub(:delete)
430
455
  end
431
456
 
432
457
  it "should remove the leadership token from memcache" do
433
- @@memcache_client.should_receive(:delete).with('the group_token')
434
- @worker.send(:cleanup)
458
+ memcache_client.should_receive(:delete).with('the group_token')
459
+ worker.send(:cleanup)
435
460
  end
436
461
 
437
462
  it "should remove the restart wanted flag from memcache" do
438
- @@memcache_client.should_receive(:delete).with('the group_restart')
439
- @worker.send(:cleanup)
463
+ memcache_client.should_receive(:delete).with('the group_restart')
464
+ worker.send(:cleanup)
440
465
  end
441
466
  end
442
467
 
443
468
  describe "as follower" do
444
469
  before do
445
- @worker.stub!(:leader?).and_return false
470
+ worker.stub(:leader?).and_return false
446
471
  end
447
472
 
448
473
  it "should not remove anything from memcache" do
449
- @@memcache_client.should_not_receive(:delete)
450
- @worker.send(:cleanup)
474
+ memcache_client.should_not_receive(:delete)
475
+ worker.send(:cleanup)
451
476
  end
452
477
  end
453
478
  end
@@ -455,65 +480,65 @@ describe Worker do
455
480
  describe "when finding workers" do
456
481
  before do
457
482
  Net::DNS::MDNSSD.stub(:browse).
458
- and_yield(mock('response1', :name => 'w1')).
459
- and_yield(mock('response2', :name => 'w2')).
460
- and_yield(mock('response3', :name => 'w3')).
461
- and_yield(mock('response4', :name => 'w4')).
462
- and_return(@browser = mock('browser', :stop => nil))
463
- @worker.stub!(:sleep)
483
+ and_yield(double('response1', :name => 'w1')).
484
+ and_yield(double('response2', :name => 'w2')).
485
+ and_yield(double('response3', :name => 'w3')).
486
+ and_yield(double('response4', :name => 'w4')).
487
+ and_return(@browser = double('browser', :stop => nil))
488
+ worker.stub(:sleep)
464
489
  end
465
490
 
466
491
  it "should browse mdns group and return workers found" do
467
- @worker.find_workers.should == %w(w1 w2 w3 w4)
492
+ worker.find_workers.should == %w(w1 w2 w3 w4)
468
493
  end
469
494
 
470
495
  it "should not add itself to the result list" do
471
- @worker.stub!(:uri).and_return('w3')
472
- @worker.find_workers.should_not include('w3')
496
+ worker.stub(:uri).and_return('w3')
497
+ worker.find_workers.should_not include('w3')
473
498
  end
474
499
 
475
500
  it "should stop browser thread after five seconds" do
476
- @worker.should_receive(:sleep).with(5).ordered
501
+ worker.should_receive(:sleep).with(5).ordered
477
502
  @browser.should_receive(:stop)
478
- @worker.find_workers
503
+ worker.find_workers
479
504
  end
480
505
  end
481
506
 
482
507
  describe "when populating followers_to_stop" do
483
508
  before do
484
- @worker.stub(:find_workers).and_return(%w(a b c))
485
- DRbObject.stub(:new).and_return(mock('o', :alive? => true))
509
+ worker.stub(:find_workers).and_return(%w(a b c))
510
+ DRbObject.stub(:new).and_return(double('o', :alive? => true))
486
511
  end
487
512
 
488
513
  it "should add all visible workers" do
489
- @worker.populate_followers_to_stop
490
- @worker.followers_to_stop.should == %w(a b c)
514
+ worker.populate_followers_to_stop
515
+ worker.followers_to_stop.should =~ %w(a b c)
491
516
  end
492
517
  end
493
518
 
494
519
  describe "when delivering followers_to_stop" do
495
520
  before do
496
- @worker.stub(:find_workers).and_return(%w(a b c))
497
- @worker.populate_followers_to_stop
498
- DRbObject.stub(:new).and_return(mock('o', :alive? => true))
521
+ worker.stub(:find_workers).and_return(%w(a b c))
522
+ worker.populate_followers_to_stop
523
+ DRbObject.stub(:new).and_return(double('o', :alive? => true))
499
524
  end
500
525
 
501
526
  it "should return the actual followers_to_stop" do
502
- @worker.followers_to_stop.should == %w(a b c)
527
+ worker.followers_to_stop.should =~ %w(a b c)
503
528
  end
504
529
 
505
530
  it "should not deliver entries that are not reachable at the moment" do
506
- DRbObject.stub!(:new).with(nil, 'a').and_return(mock('o', :alive? => false))
507
- DRbObject.stub!(:new).with(nil, 'b').and_return(x = mock('o'))
531
+ DRbObject.stub(:new).with(nil, 'a').and_return(double('o', :alive? => false))
532
+ DRbObject.stub(:new).with(nil, 'b').and_return(x = double('o'))
508
533
  x.stub(:alive?).and_raise DRb::DRbConnError.new('nix da')
509
- @worker.followers_to_stop.should == %w(c)
534
+ worker.followers_to_stop.should == %w(c)
510
535
  end
511
536
 
512
537
  it "should not remove unreachable entries from the list - maybe they reappear" do
513
- DRbObject.stub!(:new).with(nil, 'a').and_return(mock('o', :alive? => false))
514
- @worker.followers_to_stop.should == %w(b c)
515
- DRbObject.stub!(:new).with(nil, 'a').and_return(mock('o', :alive? => true))
516
- @worker.followers_to_stop.should == %w(a b c)
538
+ DRbObject.stub(:new).with(nil, 'a').and_return(double('o', :alive? => false))
539
+ worker.followers_to_stop.should =~ %w(b c)
540
+ DRbObject.stub(:new).with(nil, 'a').and_return(double('o', :alive? => true))
541
+ worker.followers_to_stop.should =~ %w(a b c)
517
542
  end
518
543
  end
519
544
  end