File: cancel.rb

package info (click to toggle)
ruby-timers 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 216 kB
  • sloc: ruby: 973; makefile: 8
file content (79 lines) | stat: -rw-r--r-- 1,364 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2014, by Lin Jen-Shin.
# Copyright, 2014-2016, by Tony Arcieri.
# Copyright, 2014-2025, by Samuel Williams.

require "timers/group"

describe Timers::Group do
	let(:group) {subject.new}
	
	it "can cancel a timer" do
		fired = false
		
		timer = group.after(0.1) { fired = true }
		timer.cancel
		
		group.wait
		
		expect(fired).to be == false
	end
	
	it "should be able to cancel twice" do
		fired = false
		
		timer = group.after(0.1) { fired = true }
		
		2.times do
			timer.cancel
			group.wait
		end
		
		expect(fired).to be == false
	end
	
	it "should be possble to reset after cancel" do
		fired = false
		
		timer = group.after(0.1) { fired = true }
		timer.cancel
		
		group.wait
		
		timer.reset
		
		group.wait
		
		expect(fired).to be == true
	end
	
	it "should cancel and remove one shot timers after they fire" do
		x = 0
		
		Timers::Wait.for(2) do |_remaining|
			timer = group.every(0.2) { x += 1 }
			group.after(0.1) { timer.cancel }
			
			group.wait
		end
		
		expect(group.timers).to be(:empty?)
		expect(x).to be == 0
	end
	
	with "#cancel" do
		it "should cancel all timers" do
			timers = 3.times.map do
				group.every(0.1) {}
			end
			
			expect(group.timers).not.to be(:empty?)
			
			group.cancel
			
			expect(group.timers).to be(:empty?)
		end
	end
end