Skip to content

Ractor test tweaks #14174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions test/ruby/test_ractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,34 +165,33 @@ def test_require_non_string
# [Bug #21398]
def test_port_receive_dnt_with_port_send
omit 'unstable on windows and macos-14' if RUBY_PLATFORM =~ /mswin|darwin/
assert_ractor(<<~'RUBY', timeout: 90)
THREADS = 10
JOBS_PER_THREAD = 50
ARRAY_SIZE = 20_000
assert_ractor(<<~'RUBY', timeout: 30)
Warning[:experimental] = false

THREADS = 2
JOBS_PER_THREAD = 1000
ARRAY_SIZE = 2000
WORKERS = 2
def ractor_job(job_count, array_size)
port = Ractor::Port.new
workers = (1..4).map do |i|
workers = WORKERS.times.map do |i|
Ractor.new(port) do |job_port|
while job = Ractor.receive
result = job.map { |x| x * 2 }.sum
job_port.send result
end
end
end
jobs = Array.new(job_count) { Array.new(array_size) { rand(1000) } }
jobs = Array.new(job_count) { Array.new(array_size) { |i| i } }
jobs.each_with_index do |job, i|
w_idx = i % 4
w_idx = i % WORKERS
workers[w_idx].send(job)
end
results = []
jobs.size.times do
result = port.receive # dnt receive
results << result
jobs.map do
port.receive # dnt receive
end
results
end
threads = []
# creates 40 ractors (THREADSx4)
THREADS.times do
threads << Thread.new do
ractor_job(JOBS_PER_THREAD, ARRAY_SIZE)
Expand All @@ -202,6 +201,27 @@ def ractor_job(job_count, array_size)
RUBY
end

# [Bug #20346]
def test_many_ractors_in_threads
assert_ractor(<<~'RUBY')
Warning[:experimental] = false

N = 100
ractors = N.times.map do
Ractor.new do
Ractor.recv # Ractor doesn't start until explicitly told to
# Do some calculations
fib = ->(x) { x < 2 ? 1 : fib.call(x - 1) + fib.call(x - 2) }
fib.call(20)
end
end

threads = ractors.map { |r| Thread.new { r.value } }
ractors.each { |r| r.send(nil) }
threads.each(&:join)
RUBY
end

def assert_make_shareable(obj)
refute Ractor.shareable?(obj), "object was already shareable"
Ractor.make_shareable(obj)
Expand Down
Loading