Created
August 9, 2008 22:10
-
-
Save tmm1/4708 to your computer and use it in GitHub Desktop.
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
require File.dirname(__FILE__) + '/fiber18' | |
require 'bacon' | |
class Bacon::Context | |
unless method_defined? :_it | |
alias :_it :it | |
def it *args | |
_it(*args){ if block_given? then yield; Fiber.yield end } | |
end | |
def done() $bacon_fiber.resume if $bacon_fiber end | |
alias :resume :done | |
end | |
end | |
require 'eventmachine' | |
module EventMachine | |
def self.spec *args, &blk | |
raise ArgumentError, 'block required' unless block_given? | |
raise 'EventMachine reactor already running' if EM.reactor_running? | |
EM.run{ | |
Bacon.summary_on_exit | |
($bacon_fiber = Fiber.new{ | |
Bacon::Context.new(args.join(' '), &blk) | |
EM.stop_event_loop | |
}).resume | |
} | |
end | |
class << self; alias :describe :spec; end | |
end | |
EM.describe EventMachine do | |
should 'have timers' do | |
start = Time.now | |
EM.add_timer(0.5){ | |
(Time.now-start).should.be.close 0.5, 0.1 | |
done | |
} | |
end | |
should 'have periodic timers' do | |
num = 0 | |
start = Time.now | |
timer = EM.add_periodic_timer(0.5){ | |
if (num += 1) == 2 | |
(Time.now-start).should.be.close 1.0, 0.1 | |
EM.__send__ :cancel_timer, timer | |
done | |
end | |
} | |
end | |
end if __FILE__ == $0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment