Skip to content

Instantly share code, notes, and snippets.

@careo
Created February 7, 2009 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save careo/59808 to your computer and use it in GitHub Desktop.
Save careo/59808 to your computer and use it in GitHub Desktop.
require File.dirname(__FILE__) + '/../ext/fiber18'
require 'bacon'
class Bacon::EventMachineContext < Bacon::Context
alias :_it :it
def it *args
_it(*args){ if block_given? then yield; Fiber.yield end }
end
def done
EM.next_tick{
$bacon_fiber.resume if $bacon_fiber
}
end
end #unless Bacon::EventMachineContext.method_defined? :_it
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::EventMachineContext.new(args.join(' '), &blk).run
EM.stop_event_loop
}).resume
}
end
class << self; alias :describe :spec; end
end
EM.describe EventMachine do
should 'work' do
1.should == 1
done
end
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