Skip to content

Commit b84964d

Browse files
author
Bittrance
committed
Merge branch 'robust-default-scheduler-tests'
2 parents 748fc25 + 0606a56 commit b84964d

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

test/rx/concurrency/test_default_scheduler.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
# Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
22

33
require 'test_helper'
4+
require 'rx/subscriptions/helpers/await_helpers'
45

5-
# DefaultScheduler creates new threads in which to run scheduled tasks; a short
6-
# sleep is necessary to allow the thread scheduler to yield to the other
7-
# threads.
86
class TestDefaultScheduler < Minitest::Test
7+
include AwaitHelpers
98

109
def setup
1110
@scheduler = Rx::DefaultScheduler.instance
1211
end
1312

13+
INTERVAL = 0.05
14+
1415
def test_schedule_with_state
1516
state = []
1617
task = ->(_, s) { s << 1 }
1718
@scheduler.schedule_with_state(state, task)
18-
sleep 0.001
19+
await_array_length(state, 1, INTERVAL)
1920

2021
assert_equal([1], state)
2122
end
@@ -24,15 +25,18 @@ def test_schedule_relative_with_state
2425
state = []
2526
task = ->(_, s) { s << 1 }
2627
@scheduler.schedule_relative_with_state(state, 0.05, task)
27-
sleep 0.1
28+
await_array_length(state, 1, INTERVAL)
2829

2930
assert_equal([1], state)
3031
end
3132

3233
def test_default_schedule_runs_in_its_own_thread
34+
state = []
3335
id = Thread.current.object_id
34-
@scheduler.schedule -> { refute_equal(id, Thread.current.object_id) }
35-
sleep 0.001
36+
@scheduler.schedule -> { state << Thread.current.object_id }
37+
await_array_length(state, 1, INTERVAL)
38+
39+
refute_equal([id], state)
3640
end
3741

3842
def test_schedule_action_cancel

test/rx/concurrency/test_periodic_scheduler.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
# Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
22

33
require 'test_helper'
4+
require 'rx/subscriptions/helpers/await_helpers'
45

56
class PeriodicTestClass
67
include Rx::PeriodicScheduler
78
end
89

9-
def await_array_length(array, expected, interval)
10-
sleep (expected * interval) * 0.9
11-
deadline = Time.now + interval * (expected + 1)
12-
while Time.now < deadline
13-
break if array.length == expected
14-
sleep interval / 10
15-
end
16-
end
17-
1810
class TestPeriodicScheduler < Minitest::Test
11+
include AwaitHelpers
12+
1913
def setup
2014
@scheduler = PeriodicTestClass.new
2115
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module AwaitHelpers
2+
def await_array_length(array, expected, interval)
3+
sleep (expected * interval) * 0.9
4+
deadline = Time.now + interval * (expected + 1)
5+
while Time.now < deadline
6+
break if array.length == expected
7+
sleep interval / 10
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)