Skip to content

Commit bd82d62

Browse files
committed
Simplify Monitor#try_enter spec.
1 parent e021561 commit bd82d62

File tree

1 file changed

+11
-30
lines changed

1 file changed

+11
-30
lines changed

spec/ruby/library/monitor/try_enter_spec.rb

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
10.times do
88

99
thread = Thread.new do
10-
begin
11-
val = monitor.try_enter
12-
ensure
13-
monitor.exit if val
14-
end
10+
val = monitor.try_enter
11+
monitor.exit if val
1512
val
1613
end
1714

@@ -23,35 +20,19 @@
2320
it "will not acquire a monitor already held by another thread" do
2421
monitor = Monitor.new
2522
10.times do
26-
locked = false
27-
28-
thread1 = Thread.new do
29-
begin
30-
monitor.enter
31-
locked = true
32-
sleep # wait for wakeup.
33-
ensure
34-
monitor.exit
35-
end
36-
end
37-
38-
Thread.pass until locked
39-
monitor.mon_locked?.should == true
40-
41-
thread2 = Thread.new do
42-
begin
23+
monitor.enter
24+
begin
25+
thread = Thread.new do
4326
val = monitor.try_enter
44-
ensure
4527
monitor.exit if val
28+
val
4629
end
47-
val
48-
end
49-
50-
thread2.join
51-
thread2.value.should == false
5230

53-
thread1.wakeup
54-
thread1.join
31+
thread.join
32+
thread.value.should == false
33+
ensure
34+
monitor.exit
35+
end
5536
monitor.mon_locked?.should == false
5637
end
5738
end

0 commit comments

Comments
 (0)