Permalink
Browse files

test for add_io_handler

  • Loading branch information...
telent committed May 20, 2011
1 parent 979ed70 commit 81308f99683f4eea79ac44902adf82a4c73c6816
Showing with 58 additions and 11 deletions.
  1. +14 −9 lib/thin/prefork.rb
  2. +44 −2 spec/prefork_spec.rb
View
@@ -32,6 +32,10 @@ def initialize(args)
@io_handlers={}
end
+ # add a handler to watch for file i/o in the parent process. +io+
+ # is an object which responds to #to_io withan IO object: when it
+ # becomes available for read/write/exceptions, the supplied block will
+ # be called with +io+ as an argument
def add_io_handler(io,&blk)
@io_handlers[io]=blk
end
@@ -50,22 +54,23 @@ def run!
end
until @workers.empty?
- a=@io_handlers.keys.dup
+ a=@io_handlers.keys.map(&:to_io)
fin,fout,fe=IO.select(a,a,a,1)
- @io_handlers.keys.each do |io|
- if fin && fin.member?(io) && blk=@io_handlers[io] then
- blk.call(:in)
+ @io_handlers.keys.each do |o|
+ io=o.to_io
+ if fin && fin.member?(io) && blk=@io_handlers[o] then
+ blk.call(o,:in)
end
- if fout && fout.member?(io) && blk=@io_handlers[io] then
- blk.call(:out)
+ if fout && fout.member?(io) && blk=@io_handlers[o] then
+ blk.call(o,:out)
end
- if fe && fe.member?(io) && blk=@io_handlers[io] then
- blk.call(:exception)
+ if fe && fe.member?(io) && blk=@io_handlers[o] then
+ blk.call(o,:exception)
end
end
if died=Process.wait(-1,Process::WNOHANG)
if @respawn then
- w=@workers.delete { |w| w.pid==died }
+ w=@workers.find { |w| w.pid==died }
if w then w.start end
end
end
View
@@ -51,16 +51,48 @@ def on_zebedee
end
end
+
+class TestIOServer < Thin::Prefork
+ $io=nil
+ class TimedRead
+ def initialize(timeout)
+ @io,client=Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM, 0)
+ Kernel.fork do
+ @io.close
+ sleep timeout
+ client.puts "wake up"
+ exit
+ end
+ end
+ def to_io
+ @io
+ end
+ end
+
+ def initialize(args)
+ super
+ self.add_io_handler(TimedRead.new(4)) do |handler,direction|
+ if direction==:in then
+ warn [:got_io_thing,handler,direction]
+ File.open("/tmp/#{Process.pid}","w") do |f|
+ f.puts handler.to_io.read
+ end
+ end
+ end
+ end
+end
+
describe Thin::Prefork do
def get(url)
Net::HTTP.get(URI.parse(url))
end
def start(args={})
+ clss=args.delete(:class) || Thin::Prefork
args={:app=>App,
:num_workers=>1,:stderr=>$stderr}.merge(args)
@kid=Kernel.fork do
- s=Thin::Prefork.new(args)
+ s=clss.new(args)
Signal.trap("TERM") do
s.stop!
end
@@ -85,7 +117,10 @@ def start(args={})
end
after(:each) do
- @kid and Process.kill("TERM",@kid)
+ if @kid then
+ Process.kill("TERM",@kid)
+ File.exists?("/tmp/#{@kid}") and File.unlink("/tmp/#{@kid}")
+ end
end
it "answers requests on address w.x.y.z port n when started on that endpoint" do
@@ -156,4 +191,11 @@ def start(args={})
v.should match /child/
v.should match /hook/
end
+
+ it "add_io_handler causes callbacks" do
+ start :class=>TestIOServer,:port=>3000
+ File.should_not exist("/tmp/#{@kid}")
+ sleep 5
+ File.should exist("/tmp/#{@kid}")
+ end
end

0 comments on commit 81308f9

Please sign in to comment.