File: timer_spec.rb

package info (click to toggle)
ruby-em-synchrony 1.0.5-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 572 kB
  • sloc: ruby: 3,458; sh: 37; sql: 7; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 911 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require "spec/helper/all"

describe EventMachine::Synchrony do

  it "should execute one-shot timer in Fiber" do
    EM.synchrony do
      start = Time.now.to_f

      EM::Synchrony.add_timer(0.1) do
        EM::Synchrony.sleep(0.1)

        (Time.now.to_f - start).should > 0.2
        EventMachine.stop
      end
    end
  end

  it "should execute period timers in Fibers" do
    EM.synchrony do
      start = Time.now.to_f
      num = 0

      EM::Synchrony.add_periodic_timer(0.1) do
        EM::Synchrony.sleep(0.1)
        num += 1

        if num > 1
          (Time.now.to_f - start).should > 0.3
          EventMachine.stop
        end
      end
    end
  end

  it 'should return instance of EventMachine::Timer from add_timer method' do
    EM.synchrony do
      timer = EM::Synchrony.add_timer(0.1){}
      timer.should be_instance_of(EventMachine::Timer)
      EventMachine.stop
    end
  end
end