Skip to content

Commit d29fa58

Browse files
committed
Fix: new_thread spec helper now returns a thread or isolated context
We can't join on a thread because it may not terminate anymore. We must call the `#wait` method of the isolated context to know when the isolated fiber has terminated. The method is conveniently aliased as `#join` so we don't need to handle the type in most cases.
1 parent 8db34f4 commit d29fa58

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

spec/std/fiber/execution_context/global_queue_spec.cr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe Fiber::ExecutionContext::GlobalQueue do
100100
queue = Fiber::ExecutionContext::GlobalQueue.new(Thread::Mutex.new)
101101
ready = Thread::WaitGroup.new(n)
102102

103-
threads = Array(Thread).new(n) do |i|
103+
threads = n.times.map do |i|
104104
new_thread("ONE-#{i}") do
105105
slept = 0
106106
ready.done
@@ -118,7 +118,7 @@ describe Fiber::ExecutionContext::GlobalQueue do
118118
end
119119
end
120120
end
121-
end
121+
end.to_a
122122
ready.wait
123123

124124
fibers.each_with_index do |fc, i|
@@ -143,7 +143,7 @@ describe Fiber::ExecutionContext::GlobalQueue do
143143
queue = Fiber::ExecutionContext::GlobalQueue.new(Thread::Mutex.new)
144144
ready = Thread::WaitGroup.new(n)
145145

146-
threads = Array(Thread).new(n) do |i|
146+
threads = n.times.map do |i|
147147
new_thread("BULK-#{i}") do
148148
slept = 0
149149

@@ -196,7 +196,7 @@ describe Fiber::ExecutionContext::GlobalQueue do
196196
Thread.sleep(1.nanosecond) # don't burn CPU
197197
end
198198
end
199-
end
199+
end.to_a
200200
ready.wait
201201

202202
# enqueue in batches of 5

spec/std/fiber/execution_context/runnables_spec.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ describe Fiber::ExecutionContext::Runnables do
196196
Fiber::ExecutionContext::Runnables(16).new(global_queue)
197197
end
198198

199-
threads = Array(Thread).new(n) do |i|
199+
threads = n.times.map do |i|
200200
new_thread("RUN-#{i}") do
201201
runnables = all_runnables[i]
202202
slept = 0
@@ -240,7 +240,7 @@ describe Fiber::ExecutionContext::Runnables do
240240
Thread.sleep(1.nanosecond) # don't burn CPU
241241
end
242242
end
243-
end
243+
end.to_a
244244
ready.wait
245245

246246
# enqueue in batches

spec/std/thread_spec.cr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ pending_interpreted describe: Thread do
2121
current = nil
2222
thread = new_thread { current = Thread.current }
2323
thread.join
24-
current.should be(thread)
24+
{% if flag?(:execution_context) %}
25+
current.should be(thread.@thread)
26+
{% else %}
27+
current.should be(thread)
28+
{% end %}
2529
current.should_not be(Thread.current)
2630
ensure
2731
# avoids a "GC Warning: Finalization cycle" caused by *current*

spec/support/thread.cr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{% begin %}
2-
def new_thread(name = nil, &block) : Thread
2+
def new_thread(name = nil, &block)
33
{% if flag?(:execution_context) %}
4-
ctx = Fiber::ExecutionContext::Isolated.new(name: name || "SPEC") { block.call }
5-
ctx.@thread
4+
Fiber::ExecutionContext::Isolated.new(name: name || "SPEC") { block.call }
65
{% else %}
76
Thread.new(name) { block.call }
87
{% end %}

0 commit comments

Comments
 (0)