File: strict.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 (43 lines) | stat: -rw-r--r-- 1,012 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
# frozen_string_literal: true

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

require "timers/group"
require "timer_quantum"

describe Timers::Group do
	let(:group) {subject.new}
	
	it "should not diverge too much" do
		fired = :not_fired_yet
		count = 0
		quantum = 0.01
		
		start_offset = group.current_offset
		Timers::Timer.new(group, quantum, :strict, start_offset) do |offset|
			fired = offset
			count += 1
		end
		
		iterations = 100
		group.wait while count < iterations
		
		# In my testing on the JVM, without the :strict recurring, I noticed 60ms of error here.
		expect(fired - start_offset).to be_within(quantum + TIMER_QUANTUM).of(iterations * quantum)
	end
	
	it "should only fire 0-interval timer once per iteration" do
		count = 0
		
		start_offset = group.current_offset
		Timers::Timer.new(group, 0, :strict, start_offset) do |offset, timer|
			count += 1
		end
		
		group.wait
		
		expect(count).to be == 1
	end
end