File: timer_watcher_spec.rb

package info (click to toggle)
ruby-cool.io 1.9.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 716 kB
  • sloc: ansic: 6,851; ruby: 1,730; makefile: 6
file content (59 lines) | stat: -rw-r--r-- 1,481 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require File.expand_path('../spec_helper', __FILE__)

describe Cool.io::TimerWatcher do

  interval = 0.010

  let :loop do
    Cool.io::Loop.new
  end

  it "can have the on_timer callback defined after creation" do
    @watcher = Cool.io::TimerWatcher.new(interval, true)
    nr = '0'
    expect(@watcher.on_timer { nr.succ! }).to be_nil
    expect(@watcher.attach(loop)).to eq(@watcher)
    expect(nr).to eq('0')
    sleep interval
    loop.run_once
    expect(nr).to eq('1')
  end

  it "can be subclassed" do
    class MyTimerWatcher < Cool.io::TimerWatcher
      TMP = '0'

      def on_timer
        TMP.succ!
      end
    end
    @watcher = MyTimerWatcher.new(interval, true)
    expect(@watcher.attach(loop)).to eq(@watcher)
    expect(MyTimerWatcher::TMP).to eq('0')
    sleep interval
    loop.run_once
    expect(MyTimerWatcher::TMP).to eq('1')
  end

  it "can have the on_timer callback redefined between runs" do
    @watcher = Cool.io::TimerWatcher.new(interval, true)
    nr = '0'
    expect(@watcher.on_timer { nr.succ! }).to be_nil
    expect(@watcher.attach(loop)).to eq(@watcher)
    expect(nr).to eq('0')
    sleep interval
    loop.run_once
    expect(nr).to eq('1')
    @watcher.detach
    expect(@watcher.on_timer { nr = :foo }).to be_nil
    expect(@watcher.attach(loop)).to eq(@watcher)
    expect(nr).to eq('1')
    sleep interval
    loop.run_once
    expect(nr).to eq(:foo)
  end

  after :each do
    @watcher.detach if defined?(@watcher)
  end
end