Skip to content

Commit

Permalink
Fix test setup, minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
noteflakes committed Aug 5, 2023
1 parent e737a1b commit e5b3738
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 24 deletions.
2 changes: 2 additions & 0 deletions test/test_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def setup
def teardown
@backend.finalize
Thread.current.backend = @prev_backend
super
end

def test_sleep
Expand Down Expand Up @@ -443,6 +444,7 @@ def setup
def teardown
@backend.finalize
Thread.current.backend = @prev_backend
super
end

def test_simple_write_chain
Expand Down
6 changes: 4 additions & 2 deletions test/test_fiber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1157,9 +1157,11 @@ def test_select_with_raised_error
end

def test_select_with_interruption
f1 = spin { sleep 0.01; :foo }
f1 = spin { sleep 0.1; :foo }
f2 = spin { sleep 1; :bar }
spin { snooze; f2.interrupt(:baz) }
snooze
f2.interrupt(:baz)

result = Fiber.select(f1, f2)
assert_equal [f2, :baz], result
end
Expand Down
14 changes: 7 additions & 7 deletions test/test_global_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,24 +144,24 @@ def test_nested_move_on_after
skip unless IS_LINUX

t0 = monotonic_clock
o = move_on_after(0.01, with_value: 1) do
move_on_after(0.03, with_value: 2) do
o = move_on_after(0.1, with_value: 1) do
move_on_after(0.3, with_value: 2) do
sleep 1
end
end
t1 = monotonic_clock
assert_equal 1, o
assert_in_range 0.008..0.040, t1 - t0 if IS_LINUX
assert_in_range 0.08..0.40, t1 - t0 if IS_LINUX

t0 = monotonic_clock
o = move_on_after(0.05, with_value: 1) do
move_on_after(0.01, with_value: 2) do
o = move_on_after(0.5, with_value: 1) do
move_on_after(0.1, with_value: 2) do
sleep 1
end
end
t1 = monotonic_clock
assert_equal 2, o
assert_in_range 0.008..0.035, t1 - t0 if IS_LINUX
assert_in_range 0.08..0.35, t1 - t0 if IS_LINUX
end
end

Expand Down Expand Up @@ -256,7 +256,7 @@ def test_cancel_after_with_lots_of_resets
end
end
t1 = monotonic_clock
assert_in_range 0.01..0.2, t1 - t0 if IS_LINUX
assert_in_range 0.01..0.3, t1 - t0 if IS_LINUX
end
end

Expand Down
28 changes: 14 additions & 14 deletions test/test_monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class TestMonitor < MiniTest::Test
Queue = Polyphony::Queue

def setup
super
@monitor = Polyphony::Monitor.new
end

Expand All @@ -21,25 +22,25 @@ def test_enter_in_different_fibers
def test_enter
ary = []
queue = Thread::Queue.new
th = Thread.new {
f1 = spin {
queue.pop
@monitor.enter
for i in 6 .. 10
ary.push(i)
Thread.pass
snooze
end
@monitor.exit
}
th2 = Thread.new {
f2 = spin {
@monitor.enter
queue.enq(nil)
for i in 1 .. 5
ary.push(i)
Thread.pass
snooze
end
@monitor.exit
}
assert_join_threads([th, th2])
Fiber.await(f1, f2)
assert_equal((1..10).to_a, ary)
end

Expand Down Expand Up @@ -271,16 +272,15 @@ def test_timedwait
cond.signal
end
end
th2 = Thread.new do
@monitor.synchronize do
queue2.enq(nil)
assert_equal("foo", b)
result2 = cond.wait(0.1)
assert_equal(true, result2)
assert_equal("bar", b)
end
result2 = nil
@monitor.synchronize do
queue2.enq(nil)
assert_equal("foo", b)
result2 = cond.wait(0.1)
assert_equal(true, result2)
assert_equal("bar", b)
end
assert_join_threads([th, th2])
th.join

c = "foo"
queue3 = Thread::Queue.new
Expand Down
2 changes: 1 addition & 1 deletion test/test_thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_thread_join_with_timeout
def test_thread_await_alias_method
buffer = []
spin { (1..3).each { |i| snooze; buffer << i } }
t = Thread.new { sleep 0.01; buffer << 4; :foo }
t = Thread.new { sleep 0.1; buffer << 4; :foo }
r = t.await
t = nil

Expand Down
6 changes: 6 additions & 0 deletions test/test_timer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

class TimerMoveOnAfterTest < MiniTest::Test
def setup
super
@timer = Polyphony::Timer.new(resolution: 0.01)
end

def teardown
@timer.stop
super
end

def test_timer_move_on_after
Expand Down Expand Up @@ -56,11 +58,13 @@ def test_timer_move_on_after_with_reset

class TimerCancelAfterTest < MiniTest::Test
def setup
super
@timer = Polyphony::Timer.new(resolution: 0.01)
end

def teardown
@timer.stop
super
end

def test_timer_cancel_after
Expand Down Expand Up @@ -134,12 +138,14 @@ def test_timer_cancel_after_with_custom_exception

class TimerMiscTest < MiniTest::Test
def setup
super
@timer = Polyphony::Timer.new(resolution: 0.001)
sleep 0
end

def teardown
@timer.stop
super
end

def test_timer_after
Expand Down

0 comments on commit e5b3738

Please sign in to comment.