Skip to content

Commit

Permalink
std specs: use isolate execution context instead of bare thread
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbaddaden committed Dec 20, 2024
1 parent 1831886 commit 18b679b
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 225 deletions.
21 changes: 7 additions & 14 deletions spec/std/execution_context/global_queue_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% skip_file if flag?(:interpreted) %}

require "./spec_helper"
require "../../support/thread"

describe ExecutionContext::GlobalQueue do
it "#initialize" do
Expand Down Expand Up @@ -99,10 +100,10 @@ describe ExecutionContext::GlobalQueue do
increments = 15
queue = ExecutionContext::GlobalQueue.new(Thread::Mutex.new)
ready = Thread::WaitGroup.new(n)
shutdown = Thread::WaitGroup.new(n)
threads = Array(Thread).new(n)

n.times do |i|
Thread.new(name: "ONE-#{i}") do |thread|
threads << new_thread(name: "ONE-#{i}") do
slept = 0
ready.done

Expand All @@ -118,10 +119,6 @@ describe ExecutionContext::GlobalQueue do
break
end
end
rescue exception
Crystal::System.print_error "\nthread: #{thread.name}: exception: #{exception}"
ensure
shutdown.done
end
end
ready.wait
Expand All @@ -131,7 +128,7 @@ describe ExecutionContext::GlobalQueue do
Thread.sleep(10.nanoseconds) if i % 10 == 9
end

shutdown.wait
threads.each(&.join)

# must have dequeued each fiber exactly X times
fibers.each { |fc| fc.counter.should eq(increments) }
Expand All @@ -147,10 +144,10 @@ describe ExecutionContext::GlobalQueue do

queue = ExecutionContext::GlobalQueue.new(Thread::Mutex.new)
ready = Thread::WaitGroup.new(n)
shutdown = Thread::WaitGroup.new(n)
threads = Array(Thread).new(n)

n.times do |i|
Thread.new(name: "BULK-#{i}") do |thread|
threads << new_thread("BULK-#{i}") do
slept = 0

r = ExecutionContext::Runnables(3).new(queue)
Expand Down Expand Up @@ -201,10 +198,6 @@ describe ExecutionContext::GlobalQueue do
slept += 1
Thread.sleep(1.nanosecond) # don't burn CPU
end
rescue exception
Crystal::System.print_error "\nthread #{thread.name} raised: #{exception}"
ensure
shutdown.done
end
end
ready.wait
Expand All @@ -217,7 +210,7 @@ describe ExecutionContext::GlobalQueue do
Thread.sleep(10.nanoseconds) if i % 4 == 3
end

shutdown.wait
threads.each(&.join)

# must have dequeued each fiber exactly X times (no less, no more)
fibers.each { |fc| fc.counter.should eq(increments) }
Expand Down
Loading

0 comments on commit 18b679b

Please sign in to comment.