Skip to content

Commit 45c0ca4

Browse files
committed
Updated specs for TimerSet.
1 parent 75455b6 commit 45c0ca4

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

spec/concurrent/executor/timer_set_spec.rb

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,11 @@ module Concurrent
5252
end
5353

5454
it 'does not execute tasks early' do
55-
expected = AtomicFixnum.new(0)
56-
subject.post(0.2){ expected.increment }
57-
sleep(0.15)
58-
expect(expected.value).to eq 0
59-
sleep(0.10)
60-
expect(expected.value).to eq 1
55+
latch = Concurrent::CountDownLatch.new(1)
56+
start = Time.now.to_f
57+
subject.post(0.2){ latch.count_down }
58+
expect(latch.wait(1)).to be true
59+
expect(Time.now.to_f - start).to be_within(0.1).of(0.2)
6160
end
6261

6362
it 'raises an exception when given a task with a past Time value' do
@@ -85,12 +84,32 @@ module Concurrent
8584
end
8685

8786
it 'executes tasks with different times in schedule order' do
87+
latch = CountDownLatch.new(3)
8888
expected = []
89-
3.times{|i| subject.post(i/10){ expected << i } }
90-
sleep(0.3)
89+
3.times{|i| subject.post(i/10){ expected << i; latch.count_down } }
90+
latch.wait(1)
9191
expect(expected).to eq [0, 1, 2]
9292
end
9393

94+
it 'executes tasks with different times in schedule time' do
95+
tests = 3
96+
interval = 0.1
97+
latch = CountDownLatch.new(tests)
98+
expected = Queue.new
99+
start = Time.now
100+
101+
(1..tests).each do |i|
102+
subject.post(interval * i) { expected << Time.now - start; latch.count_down }
103+
end
104+
105+
expect(latch.wait((tests * interval) + 1)).to be true
106+
107+
(1..tests).each do |i|
108+
delta = expected.pop
109+
expect(delta).to be_within(0.1).of((i * interval) + 0.05)
110+
end
111+
end
112+
94113
it 'cancels all pending tasks on #shutdown' do
95114
expected = AtomicFixnum.new(0)
96115
10.times{ subject.post(0.2){ expected.increment } }

0 commit comments

Comments
 (0)