diff --git a/lib/em-synchrony.rb b/lib/em-synchrony.rb index 7f61009..2e2b403 100644 --- a/lib/em-synchrony.rb +++ b/lib/em-synchrony.rb @@ -12,14 +12,15 @@ require "em-synchrony/connection_pool" module EventMachine - + # A convenience method for wrapping EM.run body within # a Ruby Fiber such that async operations can be transparently # paused and resumed based on IO scheduling. def self.synchrony(blk=nil, tail=nil, &block) - Fiber.new { - self.run(blk, tail, &block) - }.resume + blk ||= block + context = Proc.new { Fiber.new { blk.call }.resume } + + self.run(context, tail) end - + end diff --git a/spec/http_spec.rb b/spec/http_spec.rb index e077852..38273dd 100644 --- a/spec/http_spec.rb +++ b/spec/http_spec.rb @@ -5,51 +5,45 @@ describe EventMachine::HttpRequest do it "should fire sequential requests" do - EventMachine.run do - - Fiber.new { - @s = StubServer.new("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\nFoo", DELAY) - - start = now - order = [] - order.push :get if EventMachine::HttpRequest.new(URL).get - order.push :post if EventMachine::HttpRequest.new(URL).post - order.push :head if EventMachine::HttpRequest.new(URL).head - order.push :post if EventMachine::HttpRequest.new(URL).delete - order.push :put if EventMachine::HttpRequest.new(URL).put - - (now - start.to_f).should be_within(DELAY * order.size * 0.15).of(DELAY * order.size) - order.should == [:get, :post, :head, :post, :put] - - @s.stop - EventMachine.stop - }.resume + EventMachine.synchrony do + s = StubServer.new("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\nFoo", DELAY) + + start = now + order = [] + order.push :get if EventMachine::HttpRequest.new(URL).get + order.push :post if EventMachine::HttpRequest.new(URL).post + order.push :head if EventMachine::HttpRequest.new(URL).head + order.push :post if EventMachine::HttpRequest.new(URL).delete + order.push :put if EventMachine::HttpRequest.new(URL).put + + (now - start.to_f).should be_within(DELAY * order.size * 0.15).of(DELAY * order.size) + order.should == [:get, :post, :head, :post, :put] + + s.stop + EventMachine.stop end end it "should fire simultaneous requests via Multi interface" do - EventMachine.run do - - Fiber.new { - @s = StubServer.new("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\nFoo", DELAY) - - start = now - - multi = EventMachine::Synchrony::Multi.new - multi.add :a, EventMachine::HttpRequest.new(URL).aget - multi.add :b, EventMachine::HttpRequest.new(URL).apost - multi.add :c, EventMachine::HttpRequest.new(URL).ahead - multi.add :d, EventMachine::HttpRequest.new(URL).adelete - multi.add :e, EventMachine::HttpRequest.new(URL).aput - res = multi.perform - - (now - start.to_f).should be_within(DELAY * 0.15).of(DELAY) - res.responses[:callback].size.should == 5 - res.responses[:errback].size.should == 0 - - @s.stop - EventMachine.stop - }.resume + EventMachine.synchrony do + s = StubServer.new("HTTP/1.0 200 OK\r\nConnection: close\r\n\r\nFoo", DELAY) + + start = now + + multi = EventMachine::Synchrony::Multi.new + multi.add :a, EventMachine::HttpRequest.new(URL).aget + multi.add :b, EventMachine::HttpRequest.new(URL).apost + multi.add :c, EventMachine::HttpRequest.new(URL).ahead + multi.add :d, EventMachine::HttpRequest.new(URL).adelete + multi.add :e, EventMachine::HttpRequest.new(URL).aput + res = multi.perform + + (now - start.to_f).should be_within(DELAY * 0.15).of(DELAY) + res.responses[:callback].size.should == 5 + res.responses[:errback].size.should == 0 + + s.stop + EventMachine.stop end end end