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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
require "spec/helper/all"
describe EventMachine::Synchrony::Thread::Mutex do
let(:m) { EM::Synchrony::Thread::Mutex.new }
it "should synchronize" do
EM.synchrony do
i = 0
f1 = Fiber.new do
m.synchronize do
f = Fiber.current
EM.next_tick { f.resume }
Fiber.yield
i += 1
end
end.resume
f1 = Fiber.new do
m.synchronize do
i.should eql(1)
EM.stop
end
end.resume
end
end
describe "lock" do
describe "when mutex already locked" do
it "should raise ThreadError" do
f = Fiber.new do
m.lock
Fiber.yield
m.lock
end
f.resume
proc { f.resume }.should raise_error(FiberError)
end
end
end
describe "sleep" do
describe "without timeout" do
it "should sleep until resume" do
EM.synchrony do
m.lock
i = 0
f = Fiber.current
EM.next_tick { i += 1; f.resume }
res = m.sleep
i.should eql(1)
EM.stop
end
end
it "should release lock" do
EM.synchrony do
i = 0
Fiber.new do
m.lock
f = Fiber.current
EM.next_tick { f.resume }
Fiber.yield
i += 1
m.sleep
end.resume
Fiber.new do
m.lock
i.should eql(1)
EM.stop
end.resume
end
end
it "should wait unlock after resume" do
EM.synchrony do
i = 0
f1 = Fiber.new do
m.lock
m.sleep
i.should eql(1)
EM.stop
end
f2 = Fiber.new do
m.lock
f1.resume
i += 1
m.unlock
end
f1.resume
f2.resume
end
end
describe "with timeout" do
it "should sleep for timeout" do
EM.synchrony do
m.lock
i = 0
EM.next_tick { i += 1 }
m.sleep(0.05)
i.should eql(1)
EM.stop
end
end
describe "and resume before timeout" do
it "should not raise any execptions" do
EM.synchrony do
m.lock
f = Fiber.current
EM.next_tick { f.resume }
m.sleep(0.05)
EM.add_timer(0.1) { EM.stop }
end
end
it "should resume in nested Fiber" do
EM.synchrony do
f = Fiber.new do
m.synchronize do
t = m.sleep(0.05)
t.should >= 0.05
end
EM.stop
end
f.resume
end
end
end
end
end
end
describe EventMachine::Synchrony::Thread::ConditionVariable do
let(:c){ EM::Synchrony::Thread::ConditionVariable.new }
it "should wakeup waiter" do
i = ''
EM.synchrony do
f1 = Fiber.new do
m.synchronize do
i << 'a'
c.wait(m)
i << 'c'
end
EM.stop
end.resume
f2 = Fiber.new do
i << 'b'
c.signal
end.resume
end
i.should == 'abc'
end
it 'should allow to play ping-pong' do
i = ''
EM.synchrony do
f1 = Fiber.new do
m.synchronize do
i << 'pi'
c.wait(m)
i << '-po'
c.signal
end
end.resume
f2 = Fiber.new do
m.synchronize do
i << 'ng'
c.signal
c.wait(m)
i << 'ng'
end
EM.stop
end.resume
end
i.should == 'ping-pong'
end
it 'should not raise, when timer wakes up fiber between `signal` and `next_tick`' do
proc {
EM.synchrony do
f = Fiber.new do
m.synchronize do
c.wait(m, 0.0001)
end
EM.add_timer(0.001){ EM.stop }
end
i = 0
f.resume
EM.next_tick{
c.signal
}
end
}.should_not raise_error
end
end
end
|