2020-03-09 20:22:11 +03:00
|
|
|
# Ractor.current returns a current ractor
|
|
|
|
assert_equal 'Ractor', %q{
|
|
|
|
Ractor.current.class
|
|
|
|
}
|
|
|
|
|
|
|
|
# Ractor.new returns new Ractor
|
|
|
|
assert_equal 'Ractor', %q{
|
|
|
|
Ractor.new{}.class
|
|
|
|
}
|
|
|
|
|
2021-02-18 11:59:40 +03:00
|
|
|
# Ractor.allocate is not supported
|
|
|
|
assert_equal "[:ok, :ok]", %q{
|
|
|
|
rs = []
|
|
|
|
begin
|
|
|
|
Ractor.allocate
|
|
|
|
rescue => e
|
|
|
|
rs << :ok if e.message == 'allocator undefined for Ractor'
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
Ractor.new{}.dup
|
|
|
|
rescue
|
|
|
|
rs << :ok if e.message == 'allocator undefined for Ractor'
|
|
|
|
end
|
|
|
|
|
|
|
|
rs
|
|
|
|
}
|
|
|
|
|
2020-09-18 09:02:14 +03:00
|
|
|
# A Ractor can have a name
|
|
|
|
assert_equal 'test-name', %q{
|
|
|
|
r = Ractor.new name: 'test-name' do
|
|
|
|
end
|
|
|
|
r.name
|
|
|
|
}
|
|
|
|
|
|
|
|
# If Ractor doesn't have a name, Ractor#name returns nil.
|
|
|
|
assert_equal 'nil', %q{
|
|
|
|
r = Ractor.new do
|
|
|
|
end
|
|
|
|
r.name.inspect
|
|
|
|
}
|
|
|
|
|
2020-09-20 09:17:18 +03:00
|
|
|
# Raises exceptions if initialize with an invalid name
|
|
|
|
assert_equal 'ok', %q{
|
2020-09-18 09:02:14 +03:00
|
|
|
begin
|
|
|
|
r = Ractor.new(name: [{}]) {}
|
|
|
|
rescue TypeError => e
|
2020-09-20 09:17:18 +03:00
|
|
|
'ok'
|
2020-09-18 09:02:14 +03:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:22:11 +03:00
|
|
|
# Ractor.new must call with a block
|
|
|
|
assert_equal "must be called with a block", %q{
|
|
|
|
begin
|
|
|
|
Ractor.new
|
|
|
|
rescue ArgumentError => e
|
|
|
|
e.message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-09-18 08:15:32 +03:00
|
|
|
# Ractor#inspect
|
2020-09-20 09:17:18 +03:00
|
|
|
# Return only id and status for main ractor
|
2020-09-18 08:15:32 +03:00
|
|
|
assert_equal "#<Ractor:#1 running>", %q{
|
|
|
|
Ractor.current.inspect
|
|
|
|
}
|
|
|
|
|
2020-09-20 09:17:18 +03:00
|
|
|
# Return id, loc, and status for no-name ractor
|
|
|
|
assert_match /^#<Ractor:#([^ ]*?) .+:[0-9]+ terminated>$/, %q{
|
2020-09-18 08:15:32 +03:00
|
|
|
r = Ractor.new { '' }
|
|
|
|
r.take
|
2020-09-20 09:17:18 +03:00
|
|
|
sleep 0.1 until r.inspect =~ /terminated/
|
2020-09-18 08:15:32 +03:00
|
|
|
r.inspect
|
|
|
|
}
|
|
|
|
|
2020-09-20 09:17:18 +03:00
|
|
|
# Return id, name, loc, and status for named ractor
|
|
|
|
assert_match /^#<Ractor:#([^ ]*?) Test Ractor .+:[0-9]+ terminated>$/, %q{
|
|
|
|
r = Ractor.new(name: 'Test Ractor') { '' }
|
|
|
|
r.take
|
|
|
|
sleep 0.1 until r.inspect =~ /terminated/
|
2020-09-18 08:15:32 +03:00
|
|
|
r.inspect
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:22:11 +03:00
|
|
|
# A return value of a Ractor block will be a message from the Ractor.
|
|
|
|
assert_equal 'ok', %q{
|
|
|
|
# join
|
|
|
|
r = Ractor.new do
|
|
|
|
'ok'
|
|
|
|
end
|
|
|
|
r.take
|
|
|
|
}
|
|
|
|
|
|
|
|
# Passed arguments to Ractor.new will be a block parameter
|
|
|
|
# The values are passed with Ractor-communication pass.
|
|
|
|
assert_equal 'ok', %q{
|
|
|
|
# ping-pong with arg
|
|
|
|
r = Ractor.new 'ok' do |msg|
|
|
|
|
msg
|
|
|
|
end
|
|
|
|
r.take
|
|
|
|
}
|
|
|
|
|
2020-10-25 23:44:53 +03:00
|
|
|
# Pass multiple arguments to Ractor.new
|
2020-03-09 20:22:11 +03:00
|
|
|
assert_equal 'ok', %q{
|
|
|
|
# ping-pong with two args
|
|
|
|
r = Ractor.new 'ping', 'pong' do |msg, msg2|
|
|
|
|
[msg, msg2]
|
|
|
|
end
|
|
|
|
'ok' if r.take == ['ping', 'pong']
|
|
|
|
}
|
|
|
|
|
|
|
|
# Ractor#send passes an object with copy to a Ractor
|
2020-10-03 15:05:15 +03:00
|
|
|
# and Ractor.receive in the Ractor block can receive the passed value.
|
2020-03-09 20:22:11 +03:00
|
|
|
assert_equal 'ok', %q{
|
|
|
|
r = Ractor.new do
|
2020-10-03 15:05:15 +03:00
|
|
|
msg = Ractor.receive
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|
|
|
|
r.send 'ok'
|
|
|
|
r.take
|
|
|
|
}
|
|
|
|
|
2020-12-08 08:04:18 +03:00
|
|
|
# Ractor#receive_if can filter the message
|
|
|
|
assert_equal '[2, 3, 1]', %q{
|
|
|
|
r = Ractor.new Ractor.current do |main|
|
|
|
|
main << 1
|
|
|
|
main << 2
|
|
|
|
main << 3
|
|
|
|
end
|
|
|
|
a = []
|
|
|
|
a << Ractor.receive_if{|msg| msg == 2}
|
|
|
|
a << Ractor.receive_if{|msg| msg == 3}
|
|
|
|
a << Ractor.receive
|
|
|
|
}
|
|
|
|
|
|
|
|
# Ractor#receive_if with break
|
|
|
|
assert_equal '[2, [1, :break], 3]', %q{
|
|
|
|
r = Ractor.new Ractor.current do |main|
|
|
|
|
main << 1
|
|
|
|
main << 2
|
|
|
|
main << 3
|
|
|
|
end
|
|
|
|
|
|
|
|
a = []
|
|
|
|
a << Ractor.receive_if{|msg| msg == 2}
|
|
|
|
a << Ractor.receive_if{|msg| break [msg, :break]}
|
|
|
|
a << Ractor.receive
|
|
|
|
}
|
|
|
|
|
|
|
|
# Ractor#receive_if can't be called recursively
|
|
|
|
assert_equal '[[:e1, 1], [:e2, 2]]', %q{
|
|
|
|
r = Ractor.new Ractor.current do |main|
|
|
|
|
main << 1
|
|
|
|
main << 2
|
|
|
|
main << 3
|
|
|
|
end
|
|
|
|
|
|
|
|
a = []
|
2020-12-16 14:33:31 +03:00
|
|
|
|
2020-12-08 08:04:18 +03:00
|
|
|
Ractor.receive_if do |msg|
|
|
|
|
begin
|
|
|
|
Ractor.receive
|
|
|
|
rescue Ractor::Error
|
|
|
|
a << [:e1, msg]
|
|
|
|
end
|
|
|
|
true # delete 1 from queue
|
|
|
|
end
|
|
|
|
|
|
|
|
Ractor.receive_if do |msg|
|
|
|
|
begin
|
|
|
|
Ractor.receive_if{}
|
|
|
|
rescue Ractor::Error
|
|
|
|
a << [:e2, msg]
|
|
|
|
end
|
|
|
|
true # delete 2 from queue
|
|
|
|
end
|
|
|
|
|
2020-12-16 14:33:31 +03:00
|
|
|
a #
|
2020-12-08 08:04:18 +03:00
|
|
|
}
|
|
|
|
|
2021-02-10 09:24:23 +03:00
|
|
|
# dtoa race condition
|
|
|
|
assert_equal '[:ok, :ok, :ok]', %q{
|
|
|
|
n = 3
|
|
|
|
n.times.map{
|
|
|
|
Ractor.new{
|
|
|
|
10_000.times{ rand.to_s }
|
|
|
|
:ok
|
|
|
|
}
|
|
|
|
}.map(&:take)
|
|
|
|
}
|
|
|
|
|
2021-10-06 22:38:33 +03:00
|
|
|
# Ractor.make_shareable issue for locals in proc [Bug #18023]
|
|
|
|
assert_equal '[:a, :b, :c, :d, :e]', %q{
|
|
|
|
v1, v2, v3, v4, v5 = :a, :b, :c, :d, :e
|
2021-12-08 21:58:44 +03:00
|
|
|
closure = Ractor.current.instance_eval{ Proc.new { [v1, v2, v3, v4, v5] } }
|
2021-10-06 22:38:33 +03:00
|
|
|
|
|
|
|
Ractor.make_shareable(closure).call
|
|
|
|
}
|
|
|
|
|
|
|
|
# Ractor.make_shareable issue for locals in proc [Bug #18023]
|
|
|
|
assert_equal '[:a, :b, :c, :d, :e, :f, :g]', %q{
|
|
|
|
a = :a
|
2021-12-08 21:58:44 +03:00
|
|
|
closure = Ractor.current.instance_eval do
|
2021-10-06 22:38:33 +03:00
|
|
|
-> {
|
2021-12-08 21:58:44 +03:00
|
|
|
b, c, d = :b, :c, :d
|
|
|
|
-> {
|
|
|
|
e, f, g = :e, :f, :g
|
|
|
|
-> { [a, b, c, d, e, f, g] }
|
|
|
|
}.call
|
2021-10-06 22:38:33 +03:00
|
|
|
}.call
|
2021-12-08 21:58:44 +03:00
|
|
|
end
|
2021-10-06 22:38:33 +03:00
|
|
|
|
|
|
|
Ractor.make_shareable(closure).call
|
|
|
|
}
|
|
|
|
|
2021-12-14 19:11:45 +03:00
|
|
|
# Now autoload in non-main Ractor is not supported
|
|
|
|
assert_equal 'ok', %q{
|
|
|
|
autoload :Foo, 'foo.rb'
|
|
|
|
r = Ractor.new do
|
|
|
|
p Foo
|
|
|
|
rescue Ractor::UnsafeError
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
r.take
|
|
|
|
}
|
|
|
|
|
2020-09-03 12:04:36 +03:00
|
|
|
###
|
|
|
|
###
|
|
|
|
# Ractor still has several memory corruption so skip huge number of tests
|
2020-09-03 22:07:34 +03:00
|
|
|
if ENV['GITHUB_WORKFLOW'] &&
|
|
|
|
ENV['GITHUB_WORKFLOW'] == 'Compilations'
|
|
|
|
# ignore the follow
|
|
|
|
else
|
2020-09-03 12:04:36 +03:00
|
|
|
|
2020-03-09 20:22:11 +03:00
|
|
|
# Ractor.select(*ractors) receives a values from a ractors.
|
|
|
|
# It is similar to select(2) and Go's select syntax.
|
|
|
|
# The return value is [ch, received_value]
|
|
|
|
assert_equal 'ok', %q{
|
|
|
|
# select 1
|
|
|
|
r1 = Ractor.new{'r1'}
|
|
|
|
r, obj = Ractor.select(r1)
|
|
|
|
'ok' if r == r1 and obj == 'r1'
|
|
|
|
}
|
|
|
|
|
2020-10-25 23:44:53 +03:00
|
|
|
# Ractor.select from two ractors.
|
2020-03-09 20:22:11 +03:00
|
|
|
assert_equal '["r1", "r2"]', %q{
|
|
|
|
# select 2
|
|
|
|
r1 = Ractor.new{'r1'}
|
|
|
|
r2 = Ractor.new{'r2'}
|
|
|
|
rs = [r1, r2]
|
|
|
|
as = []
|
|
|
|
r, obj = Ractor.select(*rs)
|
|
|
|
rs.delete(r)
|
|
|
|
as << obj
|
|
|
|
r, obj = Ractor.select(*rs)
|
|
|
|
as << obj
|
|
|
|
as.sort #=> ["r1", "r2"]
|
|
|
|
}
|
|
|
|
|
2020-10-25 23:44:53 +03:00
|
|
|
# Ractor.select from multiple ractors.
|
2020-11-25 06:50:41 +03:00
|
|
|
assert_equal 30.times.map { 'ok' }.to_s, %q{
|
2020-03-09 20:22:11 +03:00
|
|
|
def test n
|
|
|
|
rs = (1..n).map do |i|
|
|
|
|
Ractor.new(i) do |i|
|
|
|
|
"r#{i}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
as = []
|
|
|
|
all_rs = rs.dup
|
|
|
|
|
|
|
|
n.times{
|
|
|
|
r, obj = Ractor.select(*rs)
|
|
|
|
as << [r, obj]
|
|
|
|
rs.delete(r)
|
|
|
|
}
|
|
|
|
|
2020-09-20 09:17:18 +03:00
|
|
|
if as.map{|r, o| r.object_id}.sort == all_rs.map{|r| r.object_id}.sort &&
|
2020-03-09 20:22:11 +03:00
|
|
|
as.map{|r, o| o}.sort == (1..n).map{|i| "r#{i}"}.sort
|
|
|
|
'ok'
|
|
|
|
else
|
|
|
|
'ng'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
30.times.map{|i|
|
|
|
|
test i
|
2020-11-25 06:50:41 +03:00
|
|
|
}
|
2022-11-15 10:38:52 +03:00
|
|
|
} unless ENV['RUN_OPTS'] =~ /--mjit-call-threshold=5/ || # This always fails with --mjit-wait --mjit-call-threshold=5
|
2022-12-02 21:18:49 +03:00
|
|
|
(ENV.key?('TRAVIS') && ENV['TRAVIS_CPU_ARCH'] == 'arm64') || # https://bugs.ruby-lang.org/issues/17878
|
|
|
|
true # too flaky everywhere http://ci.rvm.jp/results/trunk@ruby-sp1/4321096
|
2020-03-09 20:22:11 +03:00
|
|
|
|
2020-12-05 08:04:48 +03:00
|
|
|
# Exception for empty select
|
|
|
|
assert_match /specify at least one ractor/, %q{
|
|
|
|
begin
|
|
|
|
Ractor.select
|
|
|
|
rescue ArgumentError => e
|
|
|
|
e.message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:22:11 +03:00
|
|
|
# Outgoing port of a ractor will be closed when the Ractor is terminated.
|
|
|
|
assert_equal 'ok', %q{
|
|
|
|
r = Ractor.new do
|
|
|
|
'finish'
|
|
|
|
end
|
|
|
|
|
|
|
|
r.take
|
2020-09-20 09:17:18 +03:00
|
|
|
sleep 0.1 until r.inspect =~ /terminated/
|
2020-03-09 20:22:11 +03:00
|
|
|
|
|
|
|
begin
|
|
|
|
o = r.take
|
|
|
|
rescue Ractor::ClosedError
|
|
|
|
'ok'
|
|
|
|
else
|
|
|
|
"ng: #{o}"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-10-25 23:44:53 +03:00
|
|
|
# Raise Ractor::ClosedError when try to send into a terminated ractor
|
2020-03-09 20:22:11 +03:00
|
|
|
assert_equal 'ok', %q{
|
|
|
|
r = Ractor.new do
|
|
|
|
end
|
|
|
|
|
|
|
|
r.take # closed
|
2020-09-20 09:17:18 +03:00
|
|
|
sleep 0.1 until r.inspect =~ /terminated/
|
|
|
|
|
|
|
|
begin
|
|
|
|
r.send(1)
|
|
|
|
rescue Ractor::ClosedError
|
|
|
|
'ok'
|
|
|
|
else
|
|
|
|
'ng'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# Raise Ractor::ClosedError when try to send into a closed actor
|
|
|
|
assert_equal 'ok', %q{
|
2020-10-03 15:05:15 +03:00
|
|
|
r = Ractor.new { Ractor.receive }
|
2020-11-11 10:15:04 +03:00
|
|
|
r.close_incoming
|
2020-09-20 09:17:18 +03:00
|
|
|
|
|
|
|
begin
|
|
|
|
r.send(1)
|
|
|
|
rescue Ractor::ClosedError
|
|
|
|
'ok'
|
|
|
|
else
|
|
|
|
'ng'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# Raise Ractor::ClosedError when try to take from closed actor
|
|
|
|
assert_equal 'ok', %q{
|
|
|
|
r = Ractor.new do
|
|
|
|
Ractor.yield 1
|
2020-10-03 15:05:15 +03:00
|
|
|
Ractor.receive
|
2020-09-20 09:17:18 +03:00
|
|
|
end
|
|
|
|
|
2020-11-11 10:15:04 +03:00
|
|
|
r.close_outgoing
|
2020-09-20 09:17:18 +03:00
|
|
|
begin
|
|
|
|
r.take
|
|
|
|
rescue Ractor::ClosedError
|
|
|
|
'ok'
|
|
|
|
else
|
|
|
|
'ng'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-12-07 10:01:35 +03:00
|
|
|
# Can mix with Thread#interrupt and Ractor#take [Bug #17366]
|
|
|
|
assert_equal 'err', %q{
|
|
|
|
Ractor.new{
|
|
|
|
t = Thread.current
|
|
|
|
begin
|
|
|
|
Thread.new{ t.raise "err" }.join
|
|
|
|
rescue => e
|
|
|
|
e.message
|
|
|
|
end
|
|
|
|
}.take
|
|
|
|
}
|
|
|
|
|
|
|
|
# Killed Ractor's thread yields nil
|
|
|
|
assert_equal 'nil', %q{
|
|
|
|
Ractor.new{
|
|
|
|
t = Thread.current
|
|
|
|
Thread.new{ t.kill }.join
|
|
|
|
}.take.inspect #=> nil
|
|
|
|
}
|
|
|
|
|
2020-09-25 05:39:15 +03:00
|
|
|
# Ractor.yield raises Ractor::ClosedError when outgoing port is closed.
|
|
|
|
assert_equal 'ok', %q{
|
|
|
|
r = Ractor.new Ractor.current do |main|
|
2020-10-03 15:05:15 +03:00
|
|
|
Ractor.receive
|
2020-09-25 05:39:15 +03:00
|
|
|
main << true
|
|
|
|
Ractor.yield 1
|
|
|
|
end
|
|
|
|
|
|
|
|
r.close_outgoing
|
|
|
|
r << true
|
2020-10-03 15:05:15 +03:00
|
|
|
Ractor.receive
|
2020-09-25 05:39:15 +03:00
|
|
|
|
|
|
|
begin
|
|
|
|
r.take
|
|
|
|
rescue Ractor::ClosedError
|
|
|
|
'ok'
|
|
|
|
else
|
|
|
|
'ng'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-09-20 09:17:18 +03:00
|
|
|
# Raise Ractor::ClosedError when try to send into a ractor with closed incoming port
|
|
|
|
assert_equal 'ok', %q{
|
2020-10-03 15:05:15 +03:00
|
|
|
r = Ractor.new { Ractor.receive }
|
2020-09-20 09:17:18 +03:00
|
|
|
r.close_incoming
|
2020-03-09 20:22:11 +03:00
|
|
|
|
|
|
|
begin
|
|
|
|
r.send(1)
|
|
|
|
rescue Ractor::ClosedError
|
|
|
|
'ok'
|
|
|
|
else
|
|
|
|
'ng'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-09-20 09:17:18 +03:00
|
|
|
# A ractor with closed incoming port still can send messages out
|
|
|
|
assert_equal '[1, 2]', %q{
|
|
|
|
r = Ractor.new do
|
|
|
|
Ractor.yield 1
|
|
|
|
2
|
|
|
|
end
|
|
|
|
r.close_incoming
|
|
|
|
|
|
|
|
[r.take, r.take]
|
|
|
|
}
|
|
|
|
|
|
|
|
# Raise Ractor::ClosedError when try to take from a ractor with closed outgoing port
|
|
|
|
assert_equal 'ok', %q{
|
|
|
|
r = Ractor.new do
|
|
|
|
Ractor.yield 1
|
2020-10-03 15:05:15 +03:00
|
|
|
Ractor.receive
|
2020-09-20 09:17:18 +03:00
|
|
|
end
|
|
|
|
|
2020-09-24 11:41:10 +03:00
|
|
|
sleep 0.01 # wait for Ractor.yield in r
|
2020-09-20 09:17:18 +03:00
|
|
|
r.close_outgoing
|
|
|
|
begin
|
|
|
|
r.take
|
|
|
|
rescue Ractor::ClosedError
|
|
|
|
'ok'
|
|
|
|
else
|
|
|
|
'ng'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# A ractor with closed outgoing port still can receive messages from incoming port
|
|
|
|
assert_equal 'ok', %q{
|
|
|
|
r = Ractor.new do
|
2020-10-03 15:05:15 +03:00
|
|
|
Ractor.receive
|
2020-09-20 09:17:18 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
r.close_outgoing
|
|
|
|
begin
|
|
|
|
r.send(1)
|
|
|
|
rescue Ractor::ClosedError
|
|
|
|
'ng'
|
|
|
|
else
|
|
|
|
'ok'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-12-21 20:22:51 +03:00
|
|
|
# Ractor.main returns main ractor
|
|
|
|
assert_equal 'true', %q{
|
|
|
|
Ractor.new{
|
|
|
|
Ractor.main
|
|
|
|
}.take == Ractor.current
|
|
|
|
}
|
|
|
|
|
2020-11-10 19:55:28 +03:00
|
|
|
# a ractor with closed outgoing port should terminate
|
|
|
|
assert_equal 'ok', %q{
|
|
|
|
Ractor.new do
|
|
|
|
close_outgoing
|
|
|
|
end
|
|
|
|
|
|
|
|
true until Ractor.count == 1
|
|
|
|
:ok
|
|
|
|
}
|
|
|
|
|
2020-10-03 15:05:15 +03:00
|
|
|
# multiple Ractors can receive (wait) from one Ractor
|
2022-12-02 21:18:49 +03:00
|
|
|
yjit_enabled = ENV.key?('RUBY_YJIT_ENABLE') || ENV.fetch('RUN_OPTS', '').include?('yjit')
|
2020-03-09 20:22:11 +03:00
|
|
|
assert_equal '[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]', %q{
|
|
|
|
pipe = Ractor.new do
|
|
|
|
loop do
|
2020-10-03 15:05:15 +03:00
|
|
|
Ractor.yield Ractor.receive
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RN = 10
|
|
|
|
rs = RN.times.map{|i|
|
|
|
|
Ractor.new pipe, i do |pipe, i|
|
|
|
|
msg = pipe.take
|
|
|
|
msg # ping-pong
|
|
|
|
end
|
|
|
|
}
|
|
|
|
RN.times{|i|
|
|
|
|
pipe << i
|
|
|
|
}
|
|
|
|
RN.times.map{
|
|
|
|
r, n = Ractor.select(*rs)
|
|
|
|
rs.delete r
|
|
|
|
n
|
|
|
|
}.sort
|
2022-12-02 21:18:49 +03:00
|
|
|
} unless yjit_enabled # flaky with YJIT https://github.com/ruby/ruby/actions/runs/3603398545/jobs/6071549328#step:18:33
|
2020-03-09 20:22:11 +03:00
|
|
|
|
2020-10-03 15:05:15 +03:00
|
|
|
# Ractor.select also support multiple take, receive and yield
|
2020-03-09 20:22:11 +03:00
|
|
|
assert_equal '[true, true, true]', %q{
|
|
|
|
RN = 10
|
|
|
|
CR = Ractor.current
|
|
|
|
|
|
|
|
rs = (1..RN).map{
|
|
|
|
Ractor.new do
|
|
|
|
CR.send 'send' + CR.take #=> 'sendyield'
|
|
|
|
'take'
|
|
|
|
end
|
|
|
|
}
|
2020-10-03 15:05:15 +03:00
|
|
|
received = []
|
2020-03-09 20:22:11 +03:00
|
|
|
take = []
|
2020-10-03 15:05:15 +03:00
|
|
|
yielded = []
|
2020-03-09 20:22:11 +03:00
|
|
|
until rs.empty?
|
|
|
|
r, v = Ractor.select(CR, *rs, yield_value: 'yield')
|
|
|
|
case r
|
2020-10-03 15:05:15 +03:00
|
|
|
when :receive
|
|
|
|
received << v
|
2020-03-09 20:22:11 +03:00
|
|
|
when :yield
|
2020-10-03 15:05:15 +03:00
|
|
|
yielded << v
|
2020-03-09 20:22:11 +03:00
|
|
|
else
|
|
|
|
take << v
|
|
|
|
rs.delete r
|
|
|
|
end
|
|
|
|
end
|
2020-10-03 15:05:15 +03:00
|
|
|
[received.all?('sendyield'), yielded.all?(nil), take.all?('take')]
|
2020-03-09 20:22:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# multiple Ractors can send to one Ractor
|
|
|
|
assert_equal '[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]', %q{
|
|
|
|
pipe = Ractor.new do
|
|
|
|
loop do
|
2020-10-03 15:05:15 +03:00
|
|
|
Ractor.yield Ractor.receive
|
2020-03-09 20:22:11 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RN = 10
|
|
|
|
RN.times.map{|i|
|
|
|
|
Ractor.new pipe, i do |pipe, i|
|
|
|
|
pipe << i
|
|
|
|
end
|
|
|
|
}
|
|
|
|
RN.times.map{
|
|
|
|
pipe.take
|
|
|
|
}.sort
|
|
|
|
}
|
|
|
|
|
2020-10-03 15:05:15 +03:00
|
|
|
# an exception in a Ractor will be re-raised at Ractor#receive
|
2020-03-09 20:22:11 +03:00
|
|
|
assert_equal '[RuntimeError, "ok", true]', %q{
|
|
|
|
r = Ractor.new do
|
|
|
|
raise 'ok' # exception will be transferred receiver
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
r.take
|
|
|
|
rescue Ractor::RemoteError => e
|
|
|
|
[e.cause.class, #=> RuntimeError
|
|
|
|
e.cause.message, #=> 'ok'
|
|
|
|
e.ractor == r] #=> true
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:21:11 +03:00
|
|
|
# threads in a ractor will killed
|
|
|
|
assert_equal '{:ok=>3}', %q{
|
|
|
|
Ractor.new Ractor.current do |main|
|
2021-06-28 17:01:53 +03:00
|
|
|
q = Thread::Queue.new
|
2020-11-10 12:21:11 +03:00
|
|
|
Thread.new do
|
|
|
|
q << true
|
|
|
|
loop{}
|
|
|
|
ensure
|
|
|
|
main << :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
Thread.new do
|
|
|
|
q << true
|
|
|
|
while true
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
main << :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
Thread.new do
|
|
|
|
q << true
|
|
|
|
sleep 1
|
|
|
|
ensure
|
|
|
|
main << :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
# wait for the start of all threads
|
|
|
|
3.times{q.pop}
|
|
|
|
end
|
|
|
|
|
|
|
|
3.times.map{Ractor.receive}.tally
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:22:11 +03:00
|
|
|
# unshareable object are copied
|
|
|
|
assert_equal 'false', %q{
|
|
|
|
obj = 'str'.dup
|
|
|
|
r = Ractor.new obj do |msg|
|
|
|
|
msg.object_id
|
|
|
|
end
|
2020-09-18 09:02:14 +03:00
|
|
|
|
2020-03-09 20:22:11 +03:00
|
|
|
obj.object_id == r.take
|
|
|
|
}
|
|
|
|
|
|
|
|
# To copy the object, now Marshal#dump is used
|
2020-11-01 04:20:26 +03:00
|
|
|
assert_equal "allocator undefined for Thread", %q{
|
2020-03-09 20:22:11 +03:00
|
|
|
obj = Thread.new{}
|
|
|
|
begin
|
|
|
|
r = Ractor.new obj do |msg|
|
|
|
|
msg
|
|
|
|
end
|
|
|
|
rescue TypeError => e
|
|
|
|
e.message #=> no _dump_data is defined for class Thread
|
|
|
|
else
|
|
|
|
'ng'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-10-26 01:09:51 +03:00
|
|
|
# send shareable and unshareable objects
|
2020-09-25 07:07:07 +03:00
|
|
|
assert_equal "ok", %q{
|
|
|
|
echo_ractor = Ractor.new do
|
|
|
|
loop do
|
2020-10-03 15:05:15 +03:00
|
|
|
v = Ractor.receive
|
2020-03-09 20:22:11 +03:00
|
|
|
Ractor.yield v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-25 07:07:07 +03:00
|
|
|
class C; end
|
|
|
|
module M; end
|
2020-09-25 08:06:32 +03:00
|
|
|
S = Struct.new(:a, :b, :c, :d)
|
2020-09-25 07:07:07 +03:00
|
|
|
|
|
|
|
shareable_objects = [
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
nil,
|
|
|
|
1,
|
|
|
|
1.1, # Float
|
|
|
|
1+2r, # Rational
|
|
|
|
3+4i, # Complex
|
|
|
|
2**128, # Bignum
|
|
|
|
:sym, # Symbol
|
|
|
|
'xyzzy'.to_sym, # dynamic symbol
|
|
|
|
'frozen'.freeze, # frozen String
|
|
|
|
/regexp/, # regexp literal
|
|
|
|
/reg{true}exp/.freeze, # frozen dregexp
|
|
|
|
[1, 2].freeze, # frozen Array which only refers to shareable
|
|
|
|
{a: 1}.freeze, # frozen Hash which only refers to shareable
|
|
|
|
[{a: 1}.freeze, 'str'.freeze].freeze, # nested frozen container
|
2020-09-25 08:06:32 +03:00
|
|
|
S.new(1, 2).freeze, # frozen Struct
|
|
|
|
S.new(1, 2, 3, 4).freeze, # frozen Struct
|
2020-09-25 12:05:55 +03:00
|
|
|
(1..2), # Range on Struct
|
2020-09-27 03:49:16 +03:00
|
|
|
(1..), # Range on Struct
|
|
|
|
(..1), # Range on Struct
|
2020-09-25 07:07:07 +03:00
|
|
|
C, # class
|
|
|
|
M, # module
|
|
|
|
Ractor.current, # Ractor
|
|
|
|
]
|
|
|
|
|
|
|
|
unshareable_objects = [
|
|
|
|
'mutable str'.dup,
|
|
|
|
[:array],
|
|
|
|
{hash: true},
|
2020-09-25 08:06:32 +03:00
|
|
|
S.new(1, 2),
|
|
|
|
S.new(1, 2, 3, 4),
|
|
|
|
S.new("a", 2).freeze, # frozen, but refers to an unshareable object
|
2020-09-25 07:07:07 +03:00
|
|
|
]
|
2020-03-09 20:22:11 +03:00
|
|
|
|
2020-09-25 07:07:07 +03:00
|
|
|
results = []
|
2020-03-09 20:22:11 +03:00
|
|
|
|
2020-09-25 07:07:07 +03:00
|
|
|
shareable_objects.map{|o|
|
|
|
|
echo_ractor << o
|
|
|
|
o2 = echo_ractor.take
|
|
|
|
results << "#{o} is copied" unless o.object_id == o2.object_id
|
2020-03-09 20:22:11 +03:00
|
|
|
}
|
|
|
|
|
2020-09-25 07:07:07 +03:00
|
|
|
unshareable_objects.map{|o|
|
|
|
|
echo_ractor << o
|
|
|
|
o2 = echo_ractor.take
|
|
|
|
results << "#{o.inspect} is not copied" if o.object_id == o2.object_id
|
2020-03-09 20:22:11 +03:00
|
|
|
}
|
2020-09-25 07:07:07 +03:00
|
|
|
|
|
|
|
if results.empty?
|
|
|
|
:ok
|
|
|
|
else
|
|
|
|
results.inspect
|
|
|
|
end
|
2020-03-09 20:22:11 +03:00
|
|
|
}
|
|
|
|
|
2020-09-24 21:19:27 +03:00
|
|
|
# frozen Objects are shareable
|
|
|
|
assert_equal [false, true, false].inspect, %q{
|
|
|
|
class C
|
|
|
|
def initialize freeze
|
|
|
|
@a = 1
|
|
|
|
@b = :sym
|
|
|
|
@c = 'frozen_str'
|
|
|
|
@c.freeze if freeze
|
|
|
|
@d = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check obj1
|
|
|
|
obj2 = Ractor.new obj1 do |obj|
|
|
|
|
obj
|
|
|
|
end.take
|
|
|
|
|
|
|
|
obj1.object_id == obj2.object_id
|
|
|
|
end
|
|
|
|
|
|
|
|
results = []
|
|
|
|
results << check(C.new(true)) # false
|
|
|
|
results << check(C.new(true).freeze) # true
|
|
|
|
results << check(C.new(false).freeze) # false
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:22:11 +03:00
|
|
|
# move example2: String
|
|
|
|
# touching moved object causes an error
|
|
|
|
assert_equal 'hello world', %q{
|
|
|
|
# move
|
|
|
|
r = Ractor.new do
|
2020-10-03 15:05:15 +03:00
|
|
|
obj = Ractor.receive
|
2020-03-09 20:22:11 +03:00
|
|
|
obj << ' world'
|
|
|
|
end
|
|
|
|
|
|
|
|
str = 'hello'
|
|
|
|
r.send str, move: true
|
|
|
|
modified = r.take
|
|
|
|
|
|
|
|
begin
|
|
|
|
str << ' exception' # raise Ractor::MovedError
|
|
|
|
rescue Ractor::MovedError
|
|
|
|
modified #=> 'hello world'
|
|
|
|
else
|
|
|
|
raise 'unreachable'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# move example2: Array
|
|
|
|
assert_equal '[0, 1]', %q{
|
|
|
|
r = Ractor.new do
|
2020-10-03 15:05:15 +03:00
|
|
|
ary = Ractor.receive
|
2020-03-09 20:22:11 +03:00
|
|
|
ary << 1
|
|
|
|
end
|
|
|
|
|
|
|
|
a1 = [0]
|
|
|
|
r.send a1, move: true
|
|
|
|
a2 = r.take
|
|
|
|
begin
|
|
|
|
a1 << 2 # raise Ractor::MovedError
|
|
|
|
rescue Ractor::MovedError
|
|
|
|
a2.inspect
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# move with yield
|
|
|
|
assert_equal 'hello', %q{
|
|
|
|
r = Ractor.new do
|
|
|
|
Thread.current.report_on_exception = false
|
|
|
|
obj = 'hello'
|
|
|
|
Ractor.yield obj, move: true
|
|
|
|
obj << 'world'
|
|
|
|
end
|
|
|
|
|
|
|
|
str = r.take
|
|
|
|
begin
|
2020-09-18 09:02:14 +03:00
|
|
|
r.take
|
2020-03-09 20:22:11 +03:00
|
|
|
rescue Ractor::RemoteError
|
|
|
|
str #=> "hello"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2021-01-21 22:38:50 +03:00
|
|
|
# yield/move should not make moved object when the yield is not succeeded
|
|
|
|
assert_equal '"str"', %q{
|
|
|
|
R = Ractor.new{}
|
|
|
|
M = Ractor.current
|
|
|
|
r = Ractor.new do
|
|
|
|
s = 'str'
|
|
|
|
selected_r, v = Ractor.select R, yield_value: s, move: true
|
|
|
|
raise if selected_r != R # taken from R
|
|
|
|
M.send s.inspect # s should not be a moved object
|
|
|
|
end
|
|
|
|
|
|
|
|
Ractor.receive
|
|
|
|
}
|
|
|
|
|
|
|
|
# yield/move can fail
|
|
|
|
assert_equal "allocator undefined for Thread", %q{
|
|
|
|
r = Ractor.new do
|
|
|
|
obj = Thread.new{}
|
|
|
|
Ractor.yield obj
|
|
|
|
rescue => e
|
|
|
|
e.message
|
|
|
|
end
|
|
|
|
r.take
|
|
|
|
}
|
|
|
|
|
2020-09-13 06:35:06 +03:00
|
|
|
# Access to global-variables are prohibited
|
2020-03-09 20:22:11 +03:00
|
|
|
assert_equal 'can not access global variables $gv from non-main Ractors', %q{
|
|
|
|
$gv = 1
|
|
|
|
r = Ractor.new do
|
|
|
|
$gv
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
r.take
|
|
|
|
rescue Ractor::RemoteError => e
|
|
|
|
e.cause.message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-09-13 06:35:06 +03:00
|
|
|
# Access to global-variables are prohibited
|
2020-03-09 20:22:11 +03:00
|
|
|
assert_equal 'can not access global variables $gv from non-main Ractors', %q{
|
|
|
|
r = Ractor.new do
|
|
|
|
$gv = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
r.take
|
|
|
|
rescue Ractor::RemoteError => e
|
|
|
|
e.cause.message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# $stdin,out,err is Ractor local, but shared fds
|
|
|
|
assert_equal 'ok', %q{
|
|
|
|
r = Ractor.new do
|
|
|
|
[$stdin, $stdout, $stderr].map{|io|
|
|
|
|
[io.object_id, io.fileno]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
[$stdin, $stdout, $stderr].zip(r.take){|io, (oid, fno)|
|
|
|
|
raise "should not be different object" if io.object_id == oid
|
|
|
|
raise "fd should be same" unless io.fileno == fno
|
|
|
|
}
|
|
|
|
'ok'
|
|
|
|
}
|
|
|
|
|
2021-03-06 18:58:28 +03:00
|
|
|
# $stdin,out,err belong to Ractor
|
|
|
|
assert_equal 'ok', %q{
|
|
|
|
r = Ractor.new do
|
|
|
|
$stdin.itself
|
|
|
|
$stdout.itself
|
|
|
|
$stderr.itself
|
|
|
|
'ok'
|
|
|
|
end
|
|
|
|
|
|
|
|
r.take
|
|
|
|
}
|
|
|
|
|
Some global variables can be accessed from ractors
Some global variables should be used from non-main Ractors.
[Bug #17268]
```ruby
# ractor-local (derived from created ractor): debug
'$DEBUG' => $DEBUG,
'$-d' => $-d,
# ractor-local (derived from created ractor): verbose
'$VERBOSE' => $VERBOSE,
'$-w' => $-w,
'$-W' => $-W,
'$-v' => $-v,
# process-local (readonly): other commandline parameters
'$-p' => $-p,
'$-l' => $-l,
'$-a' => $-a,
# process-local (readonly): getpid
'$$' => $$,
# thread local: process result
'$?' => $?,
# scope local: match
'$~' => $~.inspect,
'$&' => $&,
'$`' => $`,
'$\'' => $',
'$+' => $+,
'$1' => $1,
# scope local: last line
'$_' => $_,
# scope local: last backtrace
'$@' => $@,
'$!' => $!,
# ractor local: stdin, out, err
'$stdin' => $stdin.inspect,
'$stdout' => $stdout.inspect,
'$stderr' => $stderr.inspect,
```
2020-10-20 04:46:43 +03:00
|
|
|
# $DEBUG, $VERBOSE are Ractor local
|
|
|
|
assert_equal 'true', %q{
|
|
|
|
$DEBUG = true
|
|
|
|
$VERBOSE = true
|
|
|
|
|
|
|
|
def ractor_local_globals
|
|
|
|
/a(b)(c)d/ =~ 'abcd' # for $~
|
2020-10-20 17:48:20 +03:00
|
|
|
`echo foo` unless /solaris/ =~ RUBY_PLATFORM
|
Some global variables can be accessed from ractors
Some global variables should be used from non-main Ractors.
[Bug #17268]
```ruby
# ractor-local (derived from created ractor): debug
'$DEBUG' => $DEBUG,
'$-d' => $-d,
# ractor-local (derived from created ractor): verbose
'$VERBOSE' => $VERBOSE,
'$-w' => $-w,
'$-W' => $-W,
'$-v' => $-v,
# process-local (readonly): other commandline parameters
'$-p' => $-p,
'$-l' => $-l,
'$-a' => $-a,
# process-local (readonly): getpid
'$$' => $$,
# thread local: process result
'$?' => $?,
# scope local: match
'$~' => $~.inspect,
'$&' => $&,
'$`' => $`,
'$\'' => $',
'$+' => $+,
'$1' => $1,
# scope local: last line
'$_' => $_,
# scope local: last backtrace
'$@' => $@,
'$!' => $!,
# ractor local: stdin, out, err
'$stdin' => $stdin.inspect,
'$stdout' => $stdout.inspect,
'$stderr' => $stderr.inspect,
```
2020-10-20 04:46:43 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
# ractor-local (derived from created ractor): debug
|
|
|
|
'$DEBUG' => $DEBUG,
|
|
|
|
'$-d' => $-d,
|
|
|
|
|
|
|
|
# ractor-local (derived from created ractor): verbose
|
|
|
|
'$VERBOSE' => $VERBOSE,
|
|
|
|
'$-w' => $-w,
|
|
|
|
'$-W' => $-W,
|
|
|
|
'$-v' => $-v,
|
|
|
|
|
|
|
|
# process-local (readonly): other commandline parameters
|
|
|
|
'$-p' => $-p,
|
|
|
|
'$-l' => $-l,
|
|
|
|
'$-a' => $-a,
|
|
|
|
|
|
|
|
# process-local (readonly): getpid
|
|
|
|
'$$' => $$,
|
|
|
|
|
|
|
|
# thread local: process result
|
|
|
|
'$?' => $?,
|
|
|
|
|
|
|
|
# scope local: match
|
|
|
|
'$~' => $~.inspect,
|
|
|
|
'$&' => $&,
|
|
|
|
'$`' => $`,
|
|
|
|
'$\'' => $',
|
|
|
|
'$+' => $+,
|
|
|
|
'$1' => $1,
|
|
|
|
|
|
|
|
# scope local: last line
|
|
|
|
'$_' => $_,
|
|
|
|
|
|
|
|
# scope local: last backtrace
|
|
|
|
'$@' => $@,
|
|
|
|
'$!' => $!,
|
|
|
|
|
|
|
|
# ractor local: stdin, out, err
|
|
|
|
'$stdin' => $stdin.inspect,
|
|
|
|
'$stdout' => $stdout.inspect,
|
|
|
|
'$stderr' => $stderr.inspect,
|
|
|
|
}
|
|
|
|
end
|
2020-10-25 23:38:37 +03:00
|
|
|
|
Some global variables can be accessed from ractors
Some global variables should be used from non-main Ractors.
[Bug #17268]
```ruby
# ractor-local (derived from created ractor): debug
'$DEBUG' => $DEBUG,
'$-d' => $-d,
# ractor-local (derived from created ractor): verbose
'$VERBOSE' => $VERBOSE,
'$-w' => $-w,
'$-W' => $-W,
'$-v' => $-v,
# process-local (readonly): other commandline parameters
'$-p' => $-p,
'$-l' => $-l,
'$-a' => $-a,
# process-local (readonly): getpid
'$$' => $$,
# thread local: process result
'$?' => $?,
# scope local: match
'$~' => $~.inspect,
'$&' => $&,
'$`' => $`,
'$\'' => $',
'$+' => $+,
'$1' => $1,
# scope local: last line
'$_' => $_,
# scope local: last backtrace
'$@' => $@,
'$!' => $!,
# ractor local: stdin, out, err
'$stdin' => $stdin.inspect,
'$stdout' => $stdout.inspect,
'$stderr' => $stderr.inspect,
```
2020-10-20 04:46:43 +03:00
|
|
|
h = Ractor.new do
|
|
|
|
ractor_local_globals
|
|
|
|
end.take
|
|
|
|
ractor_local_globals == h #=> true
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:22:11 +03:00
|
|
|
# selfs are different objects
|
|
|
|
assert_equal 'false', %q{
|
|
|
|
r = Ractor.new do
|
|
|
|
self.object_id
|
|
|
|
end
|
|
|
|
r.take == self.object_id #=> false
|
|
|
|
}
|
|
|
|
|
|
|
|
# self is a Ractor instance
|
|
|
|
assert_equal 'true', %q{
|
|
|
|
r = Ractor.new do
|
|
|
|
self.object_id
|
|
|
|
end
|
|
|
|
r.object_id == r.take #=> true
|
|
|
|
}
|
|
|
|
|
|
|
|
# given block Proc will be isolated, so can not access outer variables.
|
|
|
|
assert_equal 'ArgumentError', %q{
|
|
|
|
begin
|
|
|
|
a = true
|
|
|
|
r = Ractor.new do
|
|
|
|
a
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
e.class
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-10-26 01:09:51 +03:00
|
|
|
# ivar in shareable-objects are not allowed to access from non-main Ractor
|
2021-10-22 11:24:34 +03:00
|
|
|
assert_equal "can not get unshareable values from instance variables of classes/modules from non-main Ractors", %q{
|
2020-03-09 20:22:11 +03:00
|
|
|
class C
|
|
|
|
@iv = 'str'
|
|
|
|
end
|
|
|
|
|
|
|
|
r = Ractor.new do
|
|
|
|
class C
|
|
|
|
p @iv
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
begin
|
|
|
|
r.take
|
|
|
|
rescue Ractor::RemoteError => e
|
|
|
|
e.cause.message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-10-26 01:09:51 +03:00
|
|
|
# ivar in shareable-objects are not allowed to access from non-main Ractor
|
2020-03-09 20:22:11 +03:00
|
|
|
assert_equal 'can not access instance variables of shareable objects from non-main Ractors', %q{
|
|
|
|
shared = Ractor.new{}
|
|
|
|
shared.instance_variable_set(:@iv, 'str')
|
|
|
|
|
|
|
|
r = Ractor.new shared do |shared|
|
|
|
|
p shared.instance_variable_get(:@iv)
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
r.take
|
|
|
|
rescue Ractor::RemoteError => e
|
|
|
|
e.cause.message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-12-11 10:37:20 +03:00
|
|
|
# ivar in shareable-objects are not allowed to access from non-main Ractor, by @iv (get)
|
|
|
|
assert_equal 'can not access instance variables of shareable objects from non-main Ractors', %q{
|
|
|
|
class Ractor
|
|
|
|
def setup
|
|
|
|
@foo = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def foo
|
|
|
|
@foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared = Ractor.new{}
|
|
|
|
shared.setup
|
|
|
|
|
|
|
|
r = Ractor.new shared do |shared|
|
|
|
|
p shared.foo
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
r.take
|
|
|
|
rescue Ractor::RemoteError => e
|
|
|
|
e.cause.message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# ivar in shareable-objects are not allowed to access from non-main Ractor, by @iv (set)
|
|
|
|
assert_equal 'can not access instance variables of shareable objects from non-main Ractors', %q{
|
|
|
|
class Ractor
|
|
|
|
def setup
|
|
|
|
@foo = ''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared = Ractor.new{}
|
|
|
|
|
|
|
|
r = Ractor.new shared do |shared|
|
|
|
|
p shared.setup
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
r.take
|
|
|
|
rescue Ractor::RemoteError => e
|
|
|
|
e.cause.message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-10-26 01:09:51 +03:00
|
|
|
# But a shareable object is frozen, it is allowed to access ivars from non-main Ractor
|
2020-10-21 18:36:53 +03:00
|
|
|
assert_equal '11', %q{
|
|
|
|
[Object.new, [], ].map{|obj|
|
|
|
|
obj.instance_variable_set('@a', 1)
|
|
|
|
Ractor.make_shareable obj = obj.freeze
|
|
|
|
|
|
|
|
Ractor.new obj do |obj|
|
|
|
|
obj.instance_variable_get('@a')
|
|
|
|
end.take.to_s
|
|
|
|
}.join
|
|
|
|
}
|
|
|
|
|
2021-10-22 11:24:34 +03:00
|
|
|
# and instance variables of classes/modules are accessible if they refer shareable objects
|
|
|
|
assert_equal '333', %q{
|
|
|
|
class C
|
|
|
|
@int = 1
|
|
|
|
@str = '-1000'.dup
|
|
|
|
@fstr = '100'.freeze
|
|
|
|
|
|
|
|
def self.int = @int
|
|
|
|
def self.str = @str
|
|
|
|
def self.fstr = @fstr
|
|
|
|
end
|
|
|
|
|
|
|
|
module M
|
|
|
|
@int = 2
|
|
|
|
@str = '-2000'.dup
|
|
|
|
@fstr = '200'.freeze
|
|
|
|
|
|
|
|
def self.int = @int
|
|
|
|
def self.str = @str
|
|
|
|
def self.fstr = @fstr
|
|
|
|
end
|
|
|
|
|
|
|
|
a = Ractor.new{ C.int }.take
|
|
|
|
b = Ractor.new do
|
|
|
|
C.str.to_i
|
|
|
|
rescue Ractor::IsolationError
|
|
|
|
10
|
|
|
|
end.take
|
|
|
|
c = Ractor.new do
|
|
|
|
C.fstr.to_i
|
|
|
|
end.take
|
|
|
|
|
|
|
|
d = Ractor.new{ M.int }.take
|
|
|
|
e = Ractor.new do
|
|
|
|
M.str.to_i
|
|
|
|
rescue Ractor::IsolationError
|
|
|
|
20
|
|
|
|
end.take
|
|
|
|
f = Ractor.new do
|
|
|
|
M.fstr.to_i
|
|
|
|
end.take
|
|
|
|
|
|
|
|
|
|
|
|
# 1 + 10 + 100 + 2 + 20 + 200
|
|
|
|
a + b + c + d + e + f
|
|
|
|
}
|
|
|
|
|
2020-10-26 01:09:51 +03:00
|
|
|
# cvar in shareable-objects are not allowed to access from non-main Ractor
|
2020-03-09 20:22:11 +03:00
|
|
|
assert_equal 'can not access class variables from non-main Ractors', %q{
|
|
|
|
class C
|
|
|
|
@@cv = 'str'
|
|
|
|
end
|
|
|
|
|
|
|
|
r = Ractor.new do
|
|
|
|
class C
|
|
|
|
p @@cv
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-24 06:26:21 +03:00
|
|
|
begin
|
|
|
|
r.take
|
|
|
|
rescue Ractor::RemoteError => e
|
|
|
|
e.cause.message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# also cached cvar in shareable-objects are not allowed to access from non-main Ractor
|
|
|
|
assert_equal 'can not access class variables from non-main Ractors', %q{
|
|
|
|
class C
|
|
|
|
@@cv = 'str'
|
|
|
|
def self.cv
|
|
|
|
@@cv
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
C.cv # cache
|
|
|
|
|
|
|
|
r = Ractor.new do
|
|
|
|
C.cv
|
|
|
|
end
|
|
|
|
|
2020-03-09 20:22:11 +03:00
|
|
|
begin
|
|
|
|
r.take
|
|
|
|
rescue Ractor::RemoteError => e
|
|
|
|
e.cause.message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-10-26 01:09:51 +03:00
|
|
|
# Getting non-shareable objects via constants by other Ractors is not allowed
|
|
|
|
assert_equal 'can not access non-shareable objects in constant C::CONST by non-main Ractor.', %q{
|
2020-03-09 20:22:11 +03:00
|
|
|
class C
|
|
|
|
CONST = 'str'
|
|
|
|
end
|
|
|
|
r = Ractor.new do
|
|
|
|
C::CONST
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
r.take
|
|
|
|
rescue Ractor::RemoteError => e
|
|
|
|
e.cause.message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2021-01-04 12:08:25 +03:00
|
|
|
# Constant cache should care about non-sharable constants
|
|
|
|
assert_equal "can not access non-shareable objects in constant Object::STR by non-main Ractor.", %q{
|
|
|
|
STR = "hello"
|
|
|
|
def str; STR; end
|
|
|
|
s = str() # fill const cache
|
|
|
|
begin
|
|
|
|
Ractor.new{ str() }.take
|
|
|
|
rescue Ractor::RemoteError => e
|
|
|
|
e.cause.message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-10-26 01:09:51 +03:00
|
|
|
# Setting non-shareable objects into constants by other Ractors is not allowed
|
2020-03-09 20:22:11 +03:00
|
|
|
assert_equal 'can not set constants with non-shareable objects by non-main Ractors', %q{
|
|
|
|
class C
|
|
|
|
end
|
|
|
|
r = Ractor.new do
|
|
|
|
C::CONST = 'str'
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
r.take
|
|
|
|
rescue Ractor::RemoteError => e
|
|
|
|
e.cause.message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-09-25 12:31:04 +03:00
|
|
|
# define_method is not allowed
|
2021-08-24 15:00:00 +03:00
|
|
|
assert_equal "defined with an un-shareable Proc in a different Ractor", %q{
|
2020-09-25 12:31:04 +03:00
|
|
|
str = "foo"
|
|
|
|
define_method(:buggy){|i| str << "#{i}"}
|
|
|
|
begin
|
|
|
|
Ractor.new{buggy(10)}.take
|
|
|
|
rescue => e
|
|
|
|
e.cause.message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:22:11 +03:00
|
|
|
# Immutable Array and Hash are shareable, so it can be shared with constants
|
|
|
|
assert_equal '[1000, 3]', %q{
|
|
|
|
A = Array.new(1000).freeze # [nil, ...]
|
|
|
|
H = {a: 1, b: 2, c: 3}.freeze
|
|
|
|
|
|
|
|
Ractor.new{ [A.size, H.size] }.take
|
|
|
|
}
|
|
|
|
|
2020-09-20 09:17:18 +03:00
|
|
|
# Ractor.count
|
|
|
|
assert_equal '[1, 4, 3, 2, 1]', %q{
|
|
|
|
counts = []
|
|
|
|
counts << Ractor.count
|
2020-10-03 15:05:15 +03:00
|
|
|
ractors = (1..3).map { Ractor.new { Ractor.receive } }
|
2020-09-20 09:17:18 +03:00
|
|
|
counts << Ractor.count
|
|
|
|
|
|
|
|
ractors[0].send('End 0').take
|
|
|
|
sleep 0.1 until ractors[0].inspect =~ /terminated/
|
|
|
|
counts << Ractor.count
|
|
|
|
|
|
|
|
ractors[1].send('End 1').take
|
|
|
|
sleep 0.1 until ractors[1].inspect =~ /terminated/
|
|
|
|
counts << Ractor.count
|
|
|
|
|
|
|
|
ractors[2].send('End 2').take
|
|
|
|
sleep 0.1 until ractors[2].inspect =~ /terminated/
|
|
|
|
counts << Ractor.count
|
|
|
|
|
|
|
|
counts.inspect
|
|
|
|
}
|
|
|
|
|
2020-10-20 05:24:37 +03:00
|
|
|
# ObjectSpace.each_object can not handle unshareable objects with Ractors
|
|
|
|
assert_equal '0', %q{
|
|
|
|
Ractor.new{
|
|
|
|
n = 0
|
|
|
|
ObjectSpace.each_object{|o| n += 1 unless Ractor.shareable?(o)}
|
|
|
|
n
|
|
|
|
}.take
|
|
|
|
}
|
|
|
|
|
2020-12-10 07:27:08 +03:00
|
|
|
# ObjectSpace._id2ref can not handle unshareable objects with Ractors
|
|
|
|
assert_equal 'ok', %q{
|
|
|
|
s = 'hello'
|
|
|
|
|
|
|
|
Ractor.new s.object_id do |id ;s|
|
|
|
|
begin
|
|
|
|
s = ObjectSpace._id2ref(id)
|
|
|
|
rescue => e
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end.take
|
|
|
|
}
|
|
|
|
|
Ractor.make_shareable(obj)
Introduce new method Ractor.make_shareable(obj) which tries to make
obj shareable object. Protocol is here.
(1) If obj is shareable, it is shareable.
(2) If obj is not a shareable object and if obj can be shareable
object if it is frozen, then freeze obj. If obj has reachable
objects (rs), do rs.each{|o| Ractor.make_shareable(o)}
recursively (recursion is not Ruby-level, but C-level).
(3) Otherwise, raise Ractor::Error. Now T_DATA is not a shareable
object even if the object is frozen.
If the method finished without error, given obj is marked as
a sharable object.
To allow makng a shareable frozen T_DATA object, then set
`RUBY_TYPED_FROZEN_SHAREABLE` as type->flags. On default,
this flag is not set. It means user defined T_DATA objects are
not allowed to become shareable objects when it is frozen.
You can make any object shareable by setting FL_SHAREABLE flag,
so if you know that the T_DATA object is shareable (== thread-safe),
set this flag, at creation time for example. `Ractor` object is one
example, which is not a frozen, but a shareable object.
2020-10-20 18:54:03 +03:00
|
|
|
# Ractor.make_shareable(obj)
|
|
|
|
assert_equal 'true', %q{
|
|
|
|
class C
|
|
|
|
def initialize
|
|
|
|
@a = 'foo'
|
|
|
|
@b = 'bar'
|
|
|
|
end
|
2020-10-24 00:42:48 +03:00
|
|
|
|
|
|
|
def freeze
|
|
|
|
@c = [:freeze_called]
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :a, :b, :c
|
Ractor.make_shareable(obj)
Introduce new method Ractor.make_shareable(obj) which tries to make
obj shareable object. Protocol is here.
(1) If obj is shareable, it is shareable.
(2) If obj is not a shareable object and if obj can be shareable
object if it is frozen, then freeze obj. If obj has reachable
objects (rs), do rs.each{|o| Ractor.make_shareable(o)}
recursively (recursion is not Ruby-level, but C-level).
(3) Otherwise, raise Ractor::Error. Now T_DATA is not a shareable
object even if the object is frozen.
If the method finished without error, given obj is marked as
a sharable object.
To allow makng a shareable frozen T_DATA object, then set
`RUBY_TYPED_FROZEN_SHAREABLE` as type->flags. On default,
this flag is not set. It means user defined T_DATA objects are
not allowed to become shareable objects when it is frozen.
You can make any object shareable by setting FL_SHAREABLE flag,
so if you know that the T_DATA object is shareable (== thread-safe),
set this flag, at creation time for example. `Ractor` object is one
example, which is not a frozen, but a shareable object.
2020-10-20 18:54:03 +03:00
|
|
|
end
|
|
|
|
S = Struct.new(:s1, :s2)
|
|
|
|
str = "hello"
|
|
|
|
str.instance_variable_set("@iv", "hello")
|
|
|
|
/a/ =~ 'a'
|
|
|
|
m = $~
|
|
|
|
class N < Numeric
|
|
|
|
def /(other)
|
|
|
|
1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ary = []; ary << ary
|
|
|
|
|
|
|
|
a = [[1, ['2', '3']],
|
|
|
|
{Object.new => "hello"},
|
|
|
|
C.new,
|
|
|
|
S.new("x", "y"),
|
|
|
|
("a".."b"),
|
|
|
|
str,
|
|
|
|
ary, # cycle
|
|
|
|
/regexp/,
|
|
|
|
/#{'r'.upcase}/,
|
|
|
|
m,
|
|
|
|
Complex(N.new,0),
|
|
|
|
Rational(N.new,0),
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
nil,
|
|
|
|
1, 1.2, 1+3r, 1+4i, # Numeric
|
|
|
|
]
|
|
|
|
Ractor.make_shareable(a)
|
|
|
|
|
|
|
|
# check all frozen
|
|
|
|
a.each{|o|
|
|
|
|
raise o.inspect unless o.frozen?
|
|
|
|
|
|
|
|
case o
|
|
|
|
when C
|
|
|
|
raise o.a.inspect unless o.a.frozen?
|
|
|
|
raise o.b.inspect unless o.b.frozen?
|
2020-10-24 00:42:48 +03:00
|
|
|
raise o.c.inspect unless o.c.frozen? && o.c == [:freeze_called]
|
Ractor.make_shareable(obj)
Introduce new method Ractor.make_shareable(obj) which tries to make
obj shareable object. Protocol is here.
(1) If obj is shareable, it is shareable.
(2) If obj is not a shareable object and if obj can be shareable
object if it is frozen, then freeze obj. If obj has reachable
objects (rs), do rs.each{|o| Ractor.make_shareable(o)}
recursively (recursion is not Ruby-level, but C-level).
(3) Otherwise, raise Ractor::Error. Now T_DATA is not a shareable
object even if the object is frozen.
If the method finished without error, given obj is marked as
a sharable object.
To allow makng a shareable frozen T_DATA object, then set
`RUBY_TYPED_FROZEN_SHAREABLE` as type->flags. On default,
this flag is not set. It means user defined T_DATA objects are
not allowed to become shareable objects when it is frozen.
You can make any object shareable by setting FL_SHAREABLE flag,
so if you know that the T_DATA object is shareable (== thread-safe),
set this flag, at creation time for example. `Ractor` object is one
example, which is not a frozen, but a shareable object.
2020-10-20 18:54:03 +03:00
|
|
|
when Rational
|
|
|
|
raise o.numerator.inspect unless o.numerator.frozen?
|
|
|
|
when Complex
|
|
|
|
raise o.real.inspect unless o.real.frozen?
|
|
|
|
when Array
|
|
|
|
if o[0] == 1
|
|
|
|
raise o[1][1].inspect unless o[1][1].frozen?
|
|
|
|
end
|
|
|
|
when Hash
|
|
|
|
o.each{|k, v|
|
|
|
|
raise k.inspect unless k.frozen?
|
|
|
|
raise v.inspect unless v.frozen?
|
|
|
|
}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
Ractor.shareable?(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
# Ractor.make_shareable(obj) doesn't freeze shareable objects
|
|
|
|
assert_equal 'true', %q{
|
|
|
|
r = Ractor.new{}
|
|
|
|
Ractor.make_shareable(a = [r])
|
|
|
|
[a.frozen?, a[0].frozen?] == [true, false]
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:32:53 +03:00
|
|
|
# Ractor.make_shareable(a_proc) makes a proc shareable.
|
|
|
|
assert_equal 'true', %q{
|
|
|
|
a = [1, [2, 3], {a: "4"}]
|
2021-12-08 21:58:44 +03:00
|
|
|
|
|
|
|
pr = Ractor.current.instance_eval do
|
|
|
|
Proc.new do
|
|
|
|
a
|
|
|
|
end
|
2020-10-29 18:32:53 +03:00
|
|
|
end
|
2021-12-08 21:58:44 +03:00
|
|
|
|
2020-10-29 18:32:53 +03:00
|
|
|
Ractor.make_shareable(a) # referred value should be shareable
|
|
|
|
Ractor.make_shareable(pr)
|
|
|
|
Ractor.shareable?(pr)
|
|
|
|
}
|
|
|
|
|
2020-11-30 10:07:36 +03:00
|
|
|
# Ractor.shareable?(recursive_objects)
|
|
|
|
assert_equal '[false, false]', %q{
|
|
|
|
y = []
|
|
|
|
x = [y, {}].freeze
|
|
|
|
y << x
|
|
|
|
y.freeze
|
|
|
|
[Ractor.shareable?(x), Ractor.shareable?(y)]
|
|
|
|
}
|
|
|
|
|
2020-11-30 23:51:30 +03:00
|
|
|
# Ractor.make_shareable(recursive_objects)
|
|
|
|
assert_equal '[:ok, false, false]', %q{
|
|
|
|
o = Object.new
|
|
|
|
def o.freeze; raise; end
|
|
|
|
y = []
|
|
|
|
x = [y, o].freeze
|
|
|
|
y << x
|
|
|
|
y.freeze
|
|
|
|
[(Ractor.make_shareable(x) rescue :ok), Ractor.shareable?(x), Ractor.shareable?(y)]
|
|
|
|
}
|
|
|
|
|
2020-12-20 19:13:39 +03:00
|
|
|
# Ractor.make_shareable with Class/Module
|
|
|
|
assert_equal '[C, M]', %q{
|
|
|
|
class C; end
|
|
|
|
module M; end
|
|
|
|
|
|
|
|
Ractor.make_shareable(ary = [C, M])
|
|
|
|
}
|
|
|
|
|
2020-10-30 10:48:42 +03:00
|
|
|
# define_method() can invoke different Ractor's proc if the proc is shareable.
|
|
|
|
assert_equal '1', %q{
|
|
|
|
class C
|
|
|
|
a = 1
|
|
|
|
define_method "foo", Ractor.make_shareable(Proc.new{ a })
|
|
|
|
a = 2
|
|
|
|
end
|
|
|
|
|
|
|
|
Ractor.new{ C.new.foo }.take
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:32:53 +03:00
|
|
|
# Ractor.make_shareable(a_proc) makes a proc shareable.
|
|
|
|
assert_equal 'can not make a Proc shareable because it accesses outer variables (a).', %q{
|
|
|
|
a = b = nil
|
2021-12-08 21:58:44 +03:00
|
|
|
pr = Ractor.current.instance_eval do
|
|
|
|
Proc.new do
|
|
|
|
c = b # assign to a is okay because c is block local variable
|
|
|
|
# reading b is okay
|
|
|
|
a = b # assign to a is not allowed #=> Ractor::Error
|
|
|
|
end
|
2020-10-29 18:32:53 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
Ractor.make_shareable(pr)
|
|
|
|
rescue => e
|
|
|
|
e.message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-12-18 23:52:18 +03:00
|
|
|
# Ractor.make_shareable(obj, copy: true) makes copied shareable object.
|
|
|
|
assert_equal '[false, false, true, true]', %q{
|
|
|
|
r = []
|
|
|
|
o1 = [1, 2, ["3"]]
|
|
|
|
|
|
|
|
o2 = Ractor.make_shareable(o1, copy: true)
|
|
|
|
r << Ractor.shareable?(o1) # false
|
|
|
|
r << (o1.object_id == o2.object_id) # false
|
|
|
|
|
|
|
|
o3 = Ractor.make_shareable(o1)
|
|
|
|
r << Ractor.shareable?(o1) # true
|
|
|
|
r << (o1.object_id == o3.object_id) # false
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2020-12-19 00:38:58 +03:00
|
|
|
# TracePoint with normal Proc should be Ractor local
|
|
|
|
assert_equal '[4, 8]', %q{
|
|
|
|
rs = []
|
|
|
|
TracePoint.new(:line){|tp| rs << tp.lineno if tp.path == __FILE__}.enable do
|
|
|
|
Ractor.new{ # line 4
|
|
|
|
a = 1
|
|
|
|
b = 2
|
|
|
|
}.take
|
|
|
|
c = 3 # line 8
|
|
|
|
end
|
|
|
|
rs
|
|
|
|
}
|
|
|
|
|
2020-12-01 10:34:59 +03:00
|
|
|
# Ractor deep copies frozen objects (ary)
|
2020-12-01 05:14:36 +03:00
|
|
|
assert_equal '[true, false]', %q{
|
|
|
|
Ractor.new([[]].freeze) { |ary|
|
|
|
|
[ary.frozen?, ary.first.frozen? ]
|
|
|
|
}.take
|
|
|
|
}
|
|
|
|
|
2020-12-01 10:34:59 +03:00
|
|
|
# Ractor deep copies frozen objects (str)
|
|
|
|
assert_equal '[true, false]', %q{
|
|
|
|
s = String.new.instance_eval { @x = []; freeze}
|
|
|
|
Ractor.new(s) { |s|
|
|
|
|
[s.frozen?, s.instance_variable_get(:@x).frozen?]
|
|
|
|
}.take
|
|
|
|
}
|
|
|
|
|
2020-12-12 00:54:46 +03:00
|
|
|
# Can not trap with not isolated Proc on non-main ractor
|
|
|
|
assert_equal '[:ok, :ok]', %q{
|
|
|
|
a = []
|
|
|
|
Ractor.new{
|
|
|
|
trap(:INT){p :ok}
|
|
|
|
}.take
|
|
|
|
a << :ok
|
|
|
|
|
|
|
|
begin
|
|
|
|
Ractor.new{
|
|
|
|
s = 'str'
|
|
|
|
trap(:INT){p s}
|
|
|
|
}.take
|
|
|
|
rescue => Ractor::RemoteError
|
|
|
|
a << :ok
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2020-12-21 19:55:15 +03:00
|
|
|
# Ractor-local storage
|
|
|
|
assert_equal '[nil, "b", "a"]', %q{
|
|
|
|
ans = []
|
|
|
|
Ractor.current[:key] = 'a'
|
|
|
|
r = Ractor.new{
|
|
|
|
Ractor.yield self[:key]
|
|
|
|
self[:key] = 'b'
|
|
|
|
self[:key]
|
|
|
|
}
|
|
|
|
ans << r.take
|
|
|
|
ans << r.take
|
|
|
|
ans << Ractor.current[:key]
|
|
|
|
}
|
|
|
|
|
2020-09-11 12:31:15 +03:00
|
|
|
###
|
|
|
|
### Synchronization tests
|
|
|
|
###
|
|
|
|
|
|
|
|
N = 100_000
|
|
|
|
|
|
|
|
# fstring pool
|
|
|
|
assert_equal "#{N}#{N}", %Q{
|
|
|
|
N = #{N}
|
|
|
|
2.times.map{
|
|
|
|
Ractor.new{
|
|
|
|
N.times{|i| -(i.to_s)}
|
|
|
|
}
|
|
|
|
}.map{|r| r.take}.join
|
|
|
|
}
|
|
|
|
|
2020-10-14 04:43:13 +03:00
|
|
|
# enc_table
|
2020-10-13 07:16:08 +03:00
|
|
|
assert_equal "#{N/10}", %Q{
|
|
|
|
Ractor.new do
|
|
|
|
loop do
|
|
|
|
Encoding.find("test-enc-#{rand(5_000)}").inspect
|
|
|
|
rescue ArgumentError => e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
src = Encoding.find("UTF-8")
|
|
|
|
#{N/10}.times{|i|
|
|
|
|
src.replicate("test-enc-\#{i}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 04:43:13 +03:00
|
|
|
# Generic ivtbl
|
|
|
|
n = N/2
|
|
|
|
assert_equal "#{n}#{n}", %Q{
|
|
|
|
2.times.map{
|
|
|
|
Ractor.new do
|
|
|
|
#{n}.times do
|
|
|
|
obj = ''
|
|
|
|
obj.instance_variable_set("@a", 1)
|
|
|
|
obj.instance_variable_set("@b", 1)
|
|
|
|
obj.instance_variable_set("@c", 1)
|
|
|
|
obj.instance_variable_defined?("@a")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}.map{|r| r.take}.join
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:01:10 +03:00
|
|
|
# NameError
|
|
|
|
assert_equal "ok", %q{
|
|
|
|
begin
|
|
|
|
bar
|
|
|
|
rescue => err
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
Ractor.new{} << err
|
|
|
|
rescue TypeError
|
|
|
|
'ok'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2021-06-22 20:43:18 +03:00
|
|
|
assert_equal "ok", %q{
|
|
|
|
GC.disable
|
|
|
|
Ractor.new {}
|
|
|
|
raise "not ok" unless GC.disable
|
|
|
|
|
|
|
|
foo = []
|
|
|
|
10.times { foo << 1 }
|
|
|
|
|
|
|
|
GC.start
|
|
|
|
|
|
|
|
'ok'
|
|
|
|
}
|
|
|
|
|
2021-08-17 16:38:40 +03:00
|
|
|
# Can yield back values while GC is sweeping [Bug #18117]
|
|
|
|
assert_equal "ok", %q{
|
|
|
|
workers = (0...8).map do
|
|
|
|
Ractor.new do
|
|
|
|
loop do
|
|
|
|
10_000.times.map { Object.new }
|
|
|
|
Ractor.yield Time.now
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
1_000.times { idle_worker, tmp_reporter = Ractor.select(*workers) }
|
|
|
|
"ok"
|
2022-12-02 21:26:54 +03:00
|
|
|
} unless yjit_enabled # flaky with YJIT https://github.com/ruby/ruby/actions/runs/3575374374/jobs/6011846425
|
2021-08-17 16:38:40 +03:00
|
|
|
|
2021-10-13 08:25:34 +03:00
|
|
|
assert_equal "ok", %q{
|
|
|
|
def foo(*); ->{ super }; end
|
|
|
|
begin
|
|
|
|
Ractor.make_shareable(foo)
|
|
|
|
rescue Ractor::IsolationError
|
|
|
|
"ok"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal "ok", %q{
|
|
|
|
def foo(**); ->{ super }; end
|
|
|
|
begin
|
|
|
|
Ractor.make_shareable(foo)
|
|
|
|
rescue Ractor::IsolationError
|
|
|
|
"ok"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal "ok", %q{
|
|
|
|
def foo(...); ->{ super }; end
|
|
|
|
begin
|
|
|
|
Ractor.make_shareable(foo)
|
|
|
|
rescue Ractor::IsolationError
|
|
|
|
"ok"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal "ok", %q{
|
|
|
|
def foo((x), (y)); ->{ super }; end
|
|
|
|
begin
|
|
|
|
Ractor.make_shareable(foo([], []))
|
|
|
|
rescue Ractor::IsolationError
|
|
|
|
"ok"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2022-10-04 22:27:13 +03:00
|
|
|
assert_equal "ok", %q{
|
|
|
|
module M
|
|
|
|
def foo
|
|
|
|
@foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class A
|
|
|
|
include M
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
100.times { |i| instance_variable_set(:"@var_#{i}", "bad: #{i}") }
|
|
|
|
@foo = 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class B
|
|
|
|
include M
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@foo = 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Ractor.new do
|
|
|
|
b = B.new
|
|
|
|
100_000.times do
|
|
|
|
raise unless b.foo == 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
a = A.new
|
|
|
|
100_000.times do
|
|
|
|
raise unless a.foo == 2
|
|
|
|
end
|
|
|
|
|
|
|
|
"ok"
|
|
|
|
}
|
|
|
|
|
2022-10-26 13:43:14 +03:00
|
|
|
assert_match /\Atest_ractor\.rb:1:\s+warning:\s+Ractor is experimental/, %q{
|
|
|
|
Warning[:experimental] = $VERBOSE = true
|
|
|
|
STDERR.reopen(STDOUT)
|
|
|
|
eval("Ractor.new{}.take", nil, "test_ractor.rb", 1)
|
|
|
|
}
|
|
|
|
|
2020-09-03 22:07:34 +03:00
|
|
|
end # if !ENV['GITHUB_WORKFLOW']
|