File: events_spec.rb

package info (click to toggle)
ruby-timers 4.1.1-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 196 kB
  • sloc: ruby: 668; makefile: 7
file content (57 lines) | stat: -rw-r--r-- 1,123 bytes parent folder | download | duplicates (2)
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

require 'spec_helper'

RSpec.describe Timers::Events do
  it "should register an event" do
    fired = false
    
    callback = proc do |time|
      fired = true
    end
    
    subject.schedule(0.1, callback)
    
    expect(subject.size).to be == 1
    
    subject.fire(0.15)
    
    expect(subject.size).to be == 0
    
    expect(fired).to be true
  end
  
  it "should register events in order" do
    fired = []
    
    times = [0.95, 0.1, 0.3, 0.5, 0.4, 0.2, 0.01, 0.9]
    
    times.each do |requested_time|
      callback = proc do |time|
        fired << requested_time
      end
      
      subject.schedule(requested_time, callback)
    end
    
    subject.fire(0.5)
    expect(fired).to be == times.sort.first(6)
    
    subject.fire(1.0)
    expect(fired).to be == times.sort
  end
  
  it "should fire events with the time they were fired at" do
    fired_at = :not_fired
    
    callback = proc do |time|
      # The time we actually were fired at:
      fired_at = time
    end
    
    subject.schedule(0.5, callback)
    
    subject.fire(1.0)
    
    expect(fired_at).to be == 1.0
  end
end