1
- {% skip_file unless Crystal ::EventLoop .has_constant?(:Polling ) % }
2
-
3
1
require " spec"
2
+ require " crystal/event_loop/timers"
3
+
4
+ private struct Timer
5
+ include Crystal ::PointerPairingHeap ::Node
6
+
7
+ property! wake_at : Time ::Span
8
+
9
+ def initialize (timeout : Time ::Span ? = nil )
10
+ @wake_at = Time .monotonic + timeout if timeout
11
+ end
12
+
13
+ def heap_compare (other : Pointer (self )) : Bool
14
+ wake_at < other.value.wake_at
15
+ end
16
+ end
4
17
5
- describe Crystal ::EventLoop ::Polling :: Timers do
18
+ describe Crystal ::EventLoop ::Timers do
6
19
it " #empty?" do
7
- timers = Crystal ::EventLoop ::Polling :: Timers .new
20
+ timers = Crystal ::EventLoop ::Timers ( Timer ) .new
8
21
timers.empty?.should be_true
9
22
10
- event = Crystal :: EventLoop :: Polling :: Event .new(:sleep , Fiber .current, timeout: 7 .seconds)
23
+ event = Timer .new(7 .seconds)
11
24
timers.add(pointerof (event))
12
25
timers.empty?.should be_false
13
26
@@ -17,13 +30,13 @@ describe Crystal::EventLoop::Polling::Timers do
17
30
18
31
it " #next_ready?" do
19
32
# empty
20
- timers = Crystal ::EventLoop ::Polling :: Timers .new
33
+ timers = Crystal ::EventLoop ::Timers ( Timer ) .new
21
34
timers.next_ready?.should be_nil
22
35
23
36
# with events
24
- event1s = Crystal :: EventLoop :: Polling :: Event .new(:sleep , Fiber .current, timeout: 1 .second)
25
- event3m = Crystal :: EventLoop :: Polling :: Event .new(:sleep , Fiber .current, timeout: 3 .minutes)
26
- event5m = Crystal :: EventLoop :: Polling :: Event .new(:sleep , Fiber .current, timeout: 5 .minutes)
37
+ event1s = Timer .new(1 .second)
38
+ event3m = Timer .new(3 .minutes)
39
+ event5m = Timer .new(5 .minutes)
27
40
28
41
timers.add(pointerof (event5m))
29
42
timers.next_ready?.should eq(event5m.wake_at?)
@@ -36,24 +49,24 @@ describe Crystal::EventLoop::Polling::Timers do
36
49
end
37
50
38
51
it " #dequeue_ready" do
39
- timers = Crystal ::EventLoop ::Polling :: Timers .new
52
+ timers = Crystal ::EventLoop ::Timers ( Timer ) .new
40
53
41
- event1 = Crystal :: EventLoop :: Polling :: Event .new(:sleep , Fiber .current, timeout: 0 .seconds)
42
- event2 = Crystal :: EventLoop :: Polling :: Event .new(:sleep , Fiber .current, timeout: 0 .seconds)
43
- event3 = Crystal :: EventLoop :: Polling :: Event .new(:sleep , Fiber .current, timeout: 1 .minute)
54
+ event1 = Timer .new(0 .seconds)
55
+ event2 = Timer .new(0 .seconds)
56
+ event3 = Timer .new(1 .minute)
44
57
45
58
# empty
46
59
called = 0
47
60
timers.dequeue_ready { called += 1 }
48
61
called.should eq(0 )
49
62
50
63
# add events in non chronological order
51
- timers = Crystal ::EventLoop ::Polling :: Timers .new
64
+ timers = Crystal ::EventLoop ::Timers ( Timer ) .new
52
65
timers.add(pointerof (event1))
53
66
timers.add(pointerof (event3))
54
67
timers.add(pointerof (event2))
55
68
56
- events = [] of Crystal :: EventLoop :: Polling :: Event *
69
+ events = [] of Timer *
57
70
timers.dequeue_ready { |event | events << event }
58
71
59
72
events.should eq([
@@ -64,12 +77,12 @@ describe Crystal::EventLoop::Polling::Timers do
64
77
end
65
78
66
79
it " #add" do
67
- timers = Crystal ::EventLoop ::Polling :: Timers .new
80
+ timers = Crystal ::EventLoop ::Timers ( Timer ) .new
68
81
69
- event0 = Crystal :: EventLoop :: Polling :: Event .new( :sleep , Fiber .current)
70
- event1 = Crystal :: EventLoop :: Polling :: Event .new(:sleep , Fiber .current, timeout: 0 .seconds)
71
- event2 = Crystal :: EventLoop :: Polling :: Event .new(:sleep , Fiber .current, timeout: 2 .minutes)
72
- event3 = Crystal :: EventLoop :: Polling :: Event .new(:sleep , Fiber .current, timeout: 1 .minute)
82
+ event0 = Timer .new
83
+ event1 = Timer .new(0 .seconds)
84
+ event2 = Timer .new(2 .minutes)
85
+ event3 = Timer .new(1 .minute)
73
86
74
87
# add events in non chronological order
75
88
timers.add(pointerof (event1)).should be_true # added to the head (next ready)
@@ -81,13 +94,13 @@ describe Crystal::EventLoop::Polling::Timers do
81
94
end
82
95
83
96
it " #delete" do
84
- event1 = Crystal :: EventLoop :: Polling :: Event .new(:sleep , Fiber .current, timeout: 0 .seconds)
85
- event2 = Crystal :: EventLoop :: Polling :: Event .new(:sleep , Fiber .current, timeout: 0 .seconds)
86
- event3 = Crystal :: EventLoop :: Polling :: Event .new(:sleep , Fiber .current, timeout: 1 .minute)
87
- event4 = Crystal :: EventLoop :: Polling :: Event .new(:sleep , Fiber .current, timeout: 4 .minutes)
97
+ event1 = Timer .new(0 .seconds)
98
+ event2 = Timer .new(0 .seconds)
99
+ event3 = Timer .new(1 .minute)
100
+ event4 = Timer .new(4 .minutes)
88
101
89
102
# add events in non chronological order
90
- timers = Crystal ::EventLoop ::Polling :: Timers .new
103
+ timers = Crystal ::EventLoop ::Timers ( Timer ) .new
91
104
timers.add(pointerof (event1))
92
105
timers.add(pointerof (event3))
93
106
timers.add(pointerof (event2))
0 commit comments