@@ -52,12 +52,11 @@ module Concurrent
52
52
end
53
53
54
54
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 )
61
60
end
62
61
63
62
it 'raises an exception when given a task with a past Time value' do
@@ -85,12 +84,32 @@ module Concurrent
85
84
end
86
85
87
86
it 'executes tasks with different times in schedule order' do
87
+ latch = CountDownLatch . new ( 3 )
88
88
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 )
91
91
expect ( expected ) . to eq [ 0 , 1 , 2 ]
92
92
end
93
93
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
+
94
113
it 'cancels all pending tasks on #shutdown' do
95
114
expected = AtomicFixnum . new ( 0 )
96
115
10 . times { subject . post ( 0.2 ) { expected . increment } }
0 commit comments