infopark-politics 0.2.7 → 0.2.8
Sign up to get free protection for your applications and to get access to all the features.
@@ -60,7 +60,7 @@ module Politics
|
|
60
60
|
|
61
61
|
def self.included(model) #:nodoc:
|
62
62
|
model.class_eval do
|
63
|
-
attr_accessor :group_name, :iteration_length
|
63
|
+
attr_accessor :group_name, :iteration_length, :uri
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
@@ -117,7 +117,7 @@ module Politics
|
|
117
117
|
# Get a bucket from the leader and process it
|
118
118
|
begin
|
119
119
|
log.debug "getting bucket request from leader (#{leader_uri}) and processing it"
|
120
|
-
bucket_process(*leader.bucket_request, &block)
|
120
|
+
bucket_process(*leader.bucket_request(uri), &block)
|
121
121
|
rescue DRb::DRbError => dre
|
122
122
|
log.error { "Error talking to leader: #{dre.message}" }
|
123
123
|
relax until_next_iteration
|
@@ -130,16 +130,20 @@ module Politics
|
|
130
130
|
end while loop?
|
131
131
|
end
|
132
132
|
|
133
|
-
def bucket_request
|
133
|
+
def bucket_request(requestor_uri)
|
134
134
|
if leader?
|
135
135
|
log.debug "delivering bucket request"
|
136
|
-
|
136
|
+
next_bucket requestor_uri
|
137
137
|
else
|
138
138
|
log.debug "received request for bucket but am not leader - delivering :not_leader"
|
139
139
|
[:not_leader, 0]
|
140
140
|
end
|
141
141
|
end
|
142
142
|
|
143
|
+
def next_bucket(requestor_uri)
|
144
|
+
[@buckets.pop, until_next_iteration]
|
145
|
+
end
|
146
|
+
|
143
147
|
def until_next_iteration
|
144
148
|
left = iteration_length - (Time.now - @nominated_at)
|
145
149
|
left > 0 ? left : 0
|
@@ -13,7 +13,7 @@ class Worker
|
|
13
13
|
|
14
14
|
def start
|
15
15
|
process_bucket do |bucket|
|
16
|
-
sleep 1
|
16
|
+
sleep 0.1
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
@@ -31,9 +31,14 @@ describe Worker do
|
|
31
31
|
@worker.until_next_iteration.should == 0
|
32
32
|
end
|
33
33
|
|
34
|
+
it "should give access to the uri" do
|
35
|
+
@worker.uri.should =~ %r(^druby://)
|
36
|
+
end
|
37
|
+
|
34
38
|
describe Worker, "when processing bucket" do
|
35
39
|
before do
|
36
40
|
@worker.stub!(:until_next_iteration).and_return 666
|
41
|
+
@worker.stub!(:nominate)
|
37
42
|
@worker.should_receive(:loop?).and_return true, true, true, false
|
38
43
|
end
|
39
44
|
|
@@ -50,5 +55,42 @@ describe Worker do
|
|
50
55
|
|
51
56
|
@worker.start
|
52
57
|
end
|
58
|
+
|
59
|
+
describe "as follower" do
|
60
|
+
before do
|
61
|
+
@worker.stub!(:leader?).and_return false
|
62
|
+
@worker.stub!(:leader_uri).and_return "the leader"
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should get the bucket to process from the leader at every iteration" do
|
66
|
+
@worker.stub!(:uri).and_return("my uri")
|
67
|
+
@worker.should_receive(:leader).exactly(4).times.and_return(leader = mock('leader'))
|
68
|
+
leader.should_receive(:bucket_request).with("my uri").exactly(4).times.and_return([1, 2])
|
69
|
+
@worker.start
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe Worker, "when handling a bucket request" do
|
75
|
+
describe "as leader" do
|
76
|
+
before do
|
77
|
+
@worker.stub!(:leader?).and_return true
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should deliver the bucket" do
|
81
|
+
@worker.should_receive(:next_bucket).with("requestor").and_return "the bucket"
|
82
|
+
@worker.bucket_request("requestor").should == "the bucket"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "as follower" do
|
87
|
+
before do
|
88
|
+
@worker.stub!(:leader?).and_return false
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should deliver the :not_leader bucket" do
|
92
|
+
@worker.bucket_request("requestor")[0].should == :not_leader
|
93
|
+
end
|
94
|
+
end
|
53
95
|
end
|
54
96
|
end
|