Guest User

Untitled

a guest
Feb 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. require File.dirname(__FILE__) + '/fiber18'
  2.  
  3. require 'bacon'
  4.  
  5. class Bacon::Context
  6. unless method_defined? :_it
  7. alias :_it :it
  8. def it *args
  9. _it(*args){ if block_given? then yield; Fiber.yield end }
  10. end
  11. def done() $bacon_fiber.resume if $bacon_fiber end
  12. alias :resume :done
  13. end
  14. end
  15.  
  16. require 'eventmachine'
  17.  
  18. module EventMachine
  19. def self.spec *args, &blk
  20. raise ArgumentError, 'block required' unless block_given?
  21. raise 'EventMachine reactor already running' if EM.reactor_running?
  22.  
  23. EM.run{
  24. Bacon.summary_on_exit
  25. ($bacon_fiber = Fiber.new{
  26. Bacon::Context.new(args.join(' '), &blk)
  27. EM.stop_event_loop
  28. }).resume
  29. }
  30. end
  31. class << self; alias :describe :spec; end
  32. end
  33.  
  34. EM.describe EventMachine do
  35.  
  36. should 'have timers' do
  37. start = Time.now
  38.  
  39. EM.add_timer(0.5){
  40. (Time.now-start).should.be.close 0.5, 0.1
  41. done
  42. }
  43. end
  44.  
  45. should 'have periodic timers' do
  46. num = 0
  47. start = Time.now
  48.  
  49. timer = EM.add_periodic_timer(0.5){
  50. if (num += 1) == 2
  51. (Time.now-start).should.be.close 1.0, 0.1
  52. EM.__send__ :cancel_timer, timer
  53. done
  54. end
  55. }
  56. end
  57.  
  58. end if __FILE__ == $0
Add Comment
Please, Sign In to add comment