File: wait.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 (56 lines) | stat: -rw-r--r-- 1,092 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
# frozen_string_literal: true

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

require "timers/wait"
require "timer_quantum"

describe Timers::Wait do
	let(:interval) {0.1}
	let(:repeats) {10}
	
	it "repeats until timeout expired" do
		timeout = Timers::Wait.new(interval*repeats)
		count = 0
		previous_remaining = nil
		
		timeout.while_time_remaining do |remaining|
			if previous_remaining
				expect(remaining).to be_within(TIMER_QUANTUM).of(previous_remaining - interval)
			end
			
			previous_remaining = remaining
			
			count += 1
			sleep(interval)
		end
		
		expect(count).to be == repeats
	end
	
	it "yields results as soon as possible" do
		timeout = Timers::Wait.new(5)
		
		result = timeout.while_time_remaining do |_remaining|
			break :done
		end
		
		expect(result).to be == :done
	end
	
	with "#for" do
		with "no duration" do
			it "waits forever" do
				count = 0
				Timers::Wait.for(nil) do
					count += 1
					break if count > 10
				end
				
				expect(count).to be > 10
			end
		end
	end
end