Created
June 29, 2012 18:11
-
-
Save subak/3019732 to your computer and use it in GitHub Desktop.
OneSeconds wrapped at FiberPool with 5 fibers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8; -*- | |
require "eventmachine" | |
require "rack/fiber_pool" | |
require "rack/test" | |
require "logger" | |
def logger | |
Logger.new STDOUT do | |
@level = Logger::DEBUG | |
end | |
end | |
class OneSeconds | |
def initialize | |
logger.debug "OneSeconds#init" | |
end | |
def call(env) | |
fb = Fiber.current | |
EM.add_timer 1 do | |
result = (Time.now.to_i <= env["start"] + 1.5) ? true : false | |
logger.debug "OneSeconds#call: #{result}" | |
fb.resume result | |
end | |
Fiber.yield | |
end | |
end | |
RSpec.configure do | |
include Rack::Test::Methods | |
def app | |
Rack::FiberPool.new(SimpleRack.new, size: 5) | |
end | |
end | |
describe "OneSeconds wrapped at FiberPool with 5 fibers" do | |
before:all do | |
@results = [] | |
EM.run do | |
10.times do | |
catch :async do | |
get("/", nil, { | |
"start" => Time.now.to_i, | |
"async.callback" => proc do |result| | |
@results << result | |
end | |
}) | |
end | |
end | |
EM.add_timer 3 do | |
EM.stop | |
end | |
end | |
end | |
it "should have 5 true items" do | |
@results.select do |result| | |
result == true | |
end.should have(5).items | |
end | |
it "should have 5 false items" do | |
@results.select do |result| | |
result == false | |
end.should have(5).items | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment