1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
# frozen_string_literal: true
# rubocop:todo all
require 'lite_spec_helper'
describe Mongo::ConditionVariable do
let(:lock) { Mutex.new }
let(:condition_variable) do
described_class.new(lock)
end
it 'waits until signaled' do
result = nil
consumer = Thread.new do
lock.synchronize do
result = condition_variable.wait(3)
end
end
# Context switch to start the thread
sleep 0.1
start_time = Mongo::Utils.monotonic_time
lock.synchronize do
condition_variable.signal
end
consumer.join
(Mongo::Utils.monotonic_time - start_time).should < 1
end
it 'waits until broadcast' do
result = nil
consumer = Thread.new do
lock.synchronize do
result = condition_variable.wait(3)
end
end
# Context switch to start the thread
sleep 0.1
start_time = Mongo::Utils.monotonic_time
lock.synchronize do
condition_variable.broadcast
end
consumer.join
(Mongo::Utils.monotonic_time - start_time).should < 1
end
it 'times out' do
result = nil
consumer = Thread.new do
lock.synchronize do
result = condition_variable.wait(2)
end
end
# Context switch to start the thread
sleep 0.1
start_time = Mongo::Utils.monotonic_time
consumer.join
(Mongo::Utils.monotonic_time - start_time).should > 1
end
context "when acquiring the lock and waiting" do
it "releases the lock while waiting" do
lock_acquired = false
Timeout::timeout(1) do
thread = Thread.new do
until lock_acquired
sleep 0.1
end
lock.synchronize do
condition_variable.signal
end
end
lock.synchronize do
lock_acquired = true
condition_variable.wait(10)
end
end
end
end
context "when waiting but not signaling" do
it "waits until timeout" do
lock.synchronize do
start = Mongo::Utils.monotonic_time
condition_variable.wait(1)
duration = Mongo::Utils.monotonic_time - start
expect(duration).to be > 1
end
end
end
end
|