Skip to content
Newer
Older
100644 192 lines (184 sloc) 4.2 KB
e1198ff @prepor Fiber aware Mutex
prepor authored Dec 7, 2011
1 require "spec/helper/all"
2
3 describe EventMachine::Synchrony::Thread::Mutex do
4 let(:m) { EM::Synchrony::Thread::Mutex.new }
5 it "should synchronize" do
6 EM.synchrony do
7 i = 0
8 f1 = Fiber.new do
9 m.synchronize do
10 f = Fiber.current
11 EM.next_tick { f.resume }
12 Fiber.yield
13 i += 1
14 end
15 end.resume
16 f1 = Fiber.new do
17 m.synchronize do
18 i.should eql(1)
19 EM.stop
20 end
21 end.resume
22 end
23 end
24
25 describe "lock" do
26 describe "when mutex already locked" do
27
28 it "should raise ThreadError" do
29 f = Fiber.new do
30 m.lock
31 Fiber.yield
32 m.lock
33 end
34 f.resume
35 proc { f.resume }.should raise_error(FiberError)
36 end
37 end
38 end
39
40 describe "sleep" do
41 describe "without timeout" do
42 it "should sleep until resume" do
43 EM.synchrony do
44 m.lock
45 i = 0
46 f = Fiber.current
47 EM.next_tick { i += 1; f.resume }
48 res = m.sleep
49 i.should eql(1)
50 EM.stop
51 end
52 end
53
54 it "should release lock" do
55 EM.synchrony do
56 i = 0
57 Fiber.new do
58 m.lock
59 f = Fiber.current
60 EM.next_tick { f.resume }
61 Fiber.yield
62 i += 1
63 m.sleep
64 end.resume
65 Fiber.new do
66 m.lock
67 i.should eql(1)
68 EM.stop
69 end.resume
70 end
71 end
72
73 it "should wait unlock after resume" do
74 EM.synchrony do
75 i = 0
76 f1 = Fiber.new do
77 m.lock
78 m.sleep
79 i.should eql(1)
80 EM.stop
81 end
82 f2 = Fiber.new do
83 m.lock
84 f1.resume
85 i += 1
86 m.unlock
87 end
88 f1.resume
89 f2.resume
90 end
91 end
92
93 describe "with timeout" do
94 it "should sleep for timeout" do
95 EM.synchrony do
96 m.lock
97 i = 0
98 EM.next_tick { i += 1 }
99 m.sleep(0.05)
100 i.should eql(1)
101 EM.stop
102 end
103 end
104 describe "and resume before timeout" do
105 it "should not raise any execptions" do
106 EM.synchrony do
107 m.lock
108 f = Fiber.current
109 EM.next_tick { f.resume }
110 m.sleep(0.05)
111 EM.add_timer(0.1) { EM.stop }
112 end
113 end
924bdbb @funny-falcon ... jat (just another test)
funny-falcon authored Mar 14, 2012
114 it "should resume in nested Fiber" do
115 EM.synchrony do
116 f = Fiber.new do
117 m.synchronize do
118 t = m.sleep(0.05)
119 t.should >= 0.05
120 end
121 EM.stop
122 end
123 f.resume
124 end
125 end
e1198ff @prepor Fiber aware Mutex
prepor authored Dec 7, 2011
126 end
127 end
128 end
129 end
67501ed @funny-falcon add specs for ConditionVariable
funny-falcon authored Mar 13, 2012
130 describe EventMachine::Synchrony::Thread::ConditionVariable do
131 let(:c){ EM::Synchrony::Thread::ConditionVariable.new }
132 it "should wakeup waiter" do
133 i = ''
134 EM.synchrony do
135 f1 = Fiber.new do
136 m.synchronize do
137 i << 'a'
138 c.wait(m)
139 i << 'c'
140 end
141 EM.stop
142 end.resume
143 f2 = Fiber.new do
144 i << 'b'
145 c.signal
146 end.resume
147 end
148 i.should == 'abc'
149 end
150 it 'should allow to play ping-pong' do
151 i = ''
152 EM.synchrony do
153 f1 = Fiber.new do
154 m.synchronize do
155 i << 'pi'
156 c.wait(m)
157 i << '-po'
158 c.signal
159 end
160 end.resume
161 f2 = Fiber.new do
162 m.synchronize do
163 i << 'ng'
164 c.signal
165 c.wait(m)
166 i << 'ng'
167 end
168 EM.stop
169 end.resume
170 end
171 i.should == 'ping-pong'
172 end
3512d60 @funny-falcon add failing test for race condition between ConditionVariable and timer
funny-falcon authored Mar 14, 2012
173 it 'should not raise, when timer wakes up fiber between `signal` and `next_tick`' do
174 proc {
175 EM.synchrony do
176 f = Fiber.new do
177 m.synchronize do
178 c.wait(m, 0.0001)
179 end
180 EM.add_timer(0.001){ EM.stop }
181 end
182 i = 0
183 f.resume
184 EM.next_tick{
185 c.signal
186 }
187 end
188 }.should_not raise_error
189 end
67501ed @funny-falcon add specs for ConditionVariable
funny-falcon authored Mar 13, 2012
190 end
191 end
Something went wrong with that request. Please try again.