mirror of
https://github.com/lloeki/normandy.git
synced 2025-12-06 10:04:39 +01:00
51 lines
1.1 KiB
Ruby
51 lines
1.1 KiB
Ruby
require 'test/unit'
|
|
require 'thread'
|
|
require 'channel/waitgroup'
|
|
|
|
# rubocop:disable Metrics/AbcSize
|
|
# rubocop:disable Metrics/MethodLength
|
|
|
|
class TestWaitGroup < Test::Unit::TestCase
|
|
module Util
|
|
def meanwhile(*procs)
|
|
threads = procs.map { |p| Thread.new(&p) }
|
|
yield
|
|
threads.each(&:join)
|
|
end
|
|
end
|
|
|
|
module Assert
|
|
def assert_raise_with_message(exc, msg, &block)
|
|
e = assert_raise(exc, &block)
|
|
assert_match(msg, e.message)
|
|
end
|
|
end
|
|
|
|
include Util
|
|
include Assert
|
|
|
|
def test_waitgroup
|
|
Time.now.tap do |start|
|
|
wg = WaitGroup.new
|
|
wg.add(2)
|
|
ok1 = false
|
|
ok2 = false
|
|
go -> { sleep 0.3; ok1 = true; wg.done }
|
|
go -> { sleep 0.5; ok2 = true; wg.done }
|
|
wg.wait
|
|
duration = Time.now - start
|
|
assert_equal(true, duration > 0.48)
|
|
assert_equal(true, duration < 0.52)
|
|
assert_true(ok1)
|
|
assert_true(ok2)
|
|
assert_raises(RuntimeError) { wg.done }
|
|
end
|
|
end
|
|
|
|
def test_waitgroup_concurrent_add
|
|
wg = WaitGroup.new
|
|
go -> { wg.wait }
|
|
sleep 0.1
|
|
assert_raises(RuntimeError) { wg.add(1) }
|
|
end
|
|
end
|