Skip to content

Commit

Permalink
invert fiber <> em.run loops
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Mar 20, 2010
1 parent ffd7084 commit 9d72fe6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 46 deletions.
11 changes: 6 additions & 5 deletions lib/em-synchrony.rb
Expand Up @@ -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
76 changes: 35 additions & 41 deletions spec/http_spec.rb
Expand Up @@ -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

0 comments on commit 9d72fe6

Please sign in to comment.