File: pause.rb

package info (click to toggle)
ruby-timers 4.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 216 kB
  • sloc: ruby: 973; makefile: 6
file content (71 lines) | stat: -rw-r--r-- 1,201 bytes parent folder | download
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
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2022-2025, by Samuel Williams.

require "timers/group"

describe Timers::Group do
	let(:group) {subject.new}
	let(:interval) {0.01}
	
	def before
		@fired = false
		@timer = group.after(interval) {@fired = true}
		
		@fired2 = false
		@timer2 = group.after(interval) {@fired2 = true}
		
		super
	end
	
	it "does not fire when paused" do
		@timer.pause
		group.wait
		expect(@fired).to be == false
	end
	
	it "fires when continued after pause" do
		@timer.pause
		group.wait
		@timer.resume
		
		sleep(interval)
		group.wait
		
		expect(@fired).to be == true
	end
	
	it "can pause all timers at once" do
		group.pause
		group.wait
		
		expect(@fired).to be == false
		expect(@fired2).to be == false
	end
	
	it "can continue all timers at once" do
		group.pause
		group.wait
		group.resume
		
		sleep(interval + TIMER_QUANTUM)
		group.wait
		
		expect(@fired).to be == true
		expect(@fired2).to be == true
	end
	
	it "can fire the timer directly" do
		@timer.pause
		
		group.wait
		expect(@fired).not.to be == true
		
		@timer.resume
		expect(@fired).not.to be == true

		@timer.fire
		expect(@fired).to be == true
	end
end