Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

+ Spec for activity checks

  • Loading branch information...
commit a3ba3ec12be7c3ec89afa05b698fbed1ad3ee172 1 parent a8ecb0b
@kschiess authored
Showing with 43 additions and 5 deletions.
  1. +2 −1  lib/procrastinate/server.rb
  2. +41 −4 spec/acceptance/server_spec.rb
View
3  lib/procrastinate/server.rb
@@ -5,12 +5,13 @@ def initialize
@state = :new
end
- def start(n, &block)
+ def start(n, activity_check_interval=nil, &block)
fail "Already running server." unless @state == :new
@block = block
@strategy = Procrastinate::SpawnStrategy::Throttled.new(n)
@state = :running
+ @check_interval = activity_check_interval
start_thread
end
View
45 spec/acceptance/server_spec.rb
@@ -6,15 +6,52 @@
let(:pipe) { Cod.pipe.split }
after(:each) { pipe.read.close; pipe.write.close; }
+ let(:n) { 5 }
+
+ def read_tokens(n)
+ n.times.map { pipe.read.get }
+ end
it "spawns n workers" do
- server.start(5) {
+ server.start(n) {
+ pipe.write.put Process.pid
+ sleep 10
+ }
+
+ collected_worker_pids = n.times.map { pipe.read.get }.compact
+ collected_worker_pids.should have(n).pids_stored_in_it
+ end
+ it "respawns workers until there are n workers again" do
+ server.start(n) {
pipe.write.put Process.pid
sleep 10
}
- collected_worker_pids = 3.times.map { pipe.read.get }.compact
- p collected_worker_pids
- collected_worker_pids.should have(3).pids_stored_in_it
+ pids = read_tokens(n)
+
+ # Now kill a few pids:
+ pids[0,2].each { |pid| Process.kill('QUIT', pid) }
+
+ new_pids = read_tokens(2)
+ new_pids.should have(2).pids_stored
+ (new_pids & pids).should == []
+ end
+ it "checks activity around the loop, killing processes if they are lazy" do
+ server.start(n, 0.1) { |dead_man_switch|
+ pipe.write.put Process.pid
+ loop do
+ sleep 0.1
+ end }
+
+ # There should be a steady stream of process pids coming in, since they
+ # are all killed after 0.1 seconds of inactivity. After 0.2 seconds,
+ # we should roughly read 2*n pids.
+ pids = []
+ begin
+ timeout(0.2) { loop { pids << pipe.read.get } }
+ rescue Timeout::Error
+ end
+
+ pids.size.should >= 2*n
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.