Skip to content

Commit 3cce072

Browse files
committed
More fun with timers in tests.
1 parent f3b4b3f commit 3cce072

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ group :testing do
1313
gem 'simplecov', '~> 0.9.2', :require => false
1414
gem 'coveralls', '~> 0.7.11', :require => false
1515
gem 'timecop', '~> 0.7.3'
16-
gem 'hitimes', '~> 1.2.2'
1716
end
1817

1918
group :documentation do

lib/concurrent/utility/monotonic_time.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ def monotonic_time
5555
GLOBAL_MONOTONIC_CLOCK.get_time
5656
end
5757
module_function :monotonic_time
58+
59+
# Runs the given block and returns the number of seconds that elapsed.
60+
#
61+
# @yield the block to run and time
62+
# @return [Float] the number of seconds the block took to run
63+
#
64+
# @raise [ArgumentError] when no block given
65+
#
66+
# @!macro monotonic_clock_warning
67+
def monotonic_interval
68+
raise ArgumentError.new('no block given') unless block_given?
69+
start_time = GLOBAL_MONOTONIC_CLOCK.get_time
70+
yield
71+
GLOBAL_MONOTONIC_CLOCK.get_time - start_time
72+
end
73+
module_function :monotonic_interval
5874
end
5975

6076
__END__

spec/concurrent/exchanger_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'hitimes'
2-
31
module Concurrent
42

53
describe Exchanger do
@@ -58,7 +56,7 @@ module Concurrent
5856
context 'with timeout' do
5957

6058
it 'should block until timeout' do
61-
duration = Hitimes::Interval.measure do
59+
duration = Concurrent.monotonic_interval do
6260
subject.exchange(2, 0.1)
6361
end
6462
expect(duration).to be_within(0.05).of(0.1)

spec/concurrent/utility/timer_spec.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'hitimes'
2-
31
module Concurrent
42

53
describe '#timer' do
@@ -17,12 +15,8 @@ module Concurrent
1715
end
1816

1917
it 'executes the block after the given number of seconds' do
20-
latch = CountDownLatch.new(1)
21-
duration = Hitimes::Interval.measure do
22-
Concurrent::timer(0.2){ latch.count_down }
23-
latch.wait(1)
24-
end
25-
expect(duration).to be_within(0.1).of(0.2)
18+
expect(Concurrent.configuration.global_timer_set).to receive(:post).with(0.1)
19+
Concurrent::timer(0.1){ nil }
2620
end
2721

2822
it 'suppresses exceptions thrown by the block' do

0 commit comments

Comments
 (0)