1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Fulfill missing tests and stabilize tests

This commit is contained in:
Quang-Minh Nguyen 2020-09-20 13:17:18 +07:00 committed by Koichi Sasada
parent 398da71175
commit be2efb118f
Notes: git 2020-09-20 23:11:13 +09:00

View file

@ -22,20 +22,12 @@ assert_equal 'nil', %q{
r.name.inspect r.name.inspect
} }
# Raises exceptions if initialize with invalid name # Raises exceptions if initialize with an invalid name
assert_equal 'no implicit conversion of Array into String', %q{ assert_equal 'ok', %q{
begin begin
r = Ractor.new(name: [{}]) {} r = Ractor.new(name: [{}]) {}
rescue TypeError => e rescue TypeError => e
e.message 'ok'
end
}
assert_equal 'ASCII incompatible encoding (UTF-16BE)', %q{
begin
r = Ractor.new(name: String.new('Invalid encoding', encoding: 'UTF-16BE')) {}
rescue ArgumentError => e
e.message
end end
} }
@ -49,23 +41,24 @@ assert_equal "must be called with a block", %q{
} }
# Ractor#inspect # Ractor#inspect
# Return only id and status for main ractor
assert_equal "#<Ractor:#1 running>", %q{ assert_equal "#<Ractor:#1 running>", %q{
Ractor.current.inspect Ractor.current.inspect
} }
assert_match /^#<Ractor:#([^ ]*?) bootstraptest.tmp.rb:[0-9]+ blocking>$/, %q{ # Return id, loc, and status for no-name ractor
r = Ractor.new { Ractor.recv } assert_match /^#<Ractor:#([^ ]*?) .+:[0-9]+ terminated>$/, %q{
r.inspect
}
assert_match /^#<Ractor:#([^ ]*?) bootstraptest.tmp.rb:[0-9]+ terminated>$/, %q{
r = Ractor.new { '' } r = Ractor.new { '' }
r.take r.take
sleep 0.1 until r.inspect =~ /terminated/
r.inspect r.inspect
} }
assert_match /^#<Ractor:#([^ ]*?) Test Ractor bootstraptest.tmp.rb:[0-9]+ blocking>$/, %q{ # Return id, name, loc, and status for named ractor
r = Ractor.new(name: 'Test Ractor') { Ractor.recv } assert_match /^#<Ractor:#([^ ]*?) Test Ractor .+:[0-9]+ terminated>$/, %q{
r = Ractor.new(name: 'Test Ractor') { '' }
r.take
sleep 0.1 until r.inspect =~ /terminated/
r.inspect r.inspect
} }
@ -154,7 +147,7 @@ assert_equal 'true', %q{
rs.delete(r) rs.delete(r)
} }
if as.map{|r, o| r.inspect}.sort == all_rs.map{|r| r.inspect}.sort && if as.map{|r, o| r.object_id}.sort == all_rs.map{|r| r.object_id}.sort &&
as.map{|r, o| o}.sort == (1..n).map{|i| "r#{i}"}.sort as.map{|r, o| o}.sort == (1..n).map{|i| "r#{i}"}.sort
'ok' 'ok'
else else
@ -174,7 +167,7 @@ assert_equal 'ok', %q{
end end
r.take r.take
sleep 0.1 # wait for terminate sleep 0.1 until r.inspect =~ /terminated/
begin begin
o = r.take o = r.take
@ -190,7 +183,7 @@ assert_equal 'ok', %q{
end end
r.take # closed r.take # closed
sleep 0.1 # wait for terminate sleep 0.1 until r.inspect =~ /terminated/
begin begin
r.send(1) r.send(1)
@ -201,6 +194,95 @@ assert_equal 'ok', %q{
end end
} }
# Raise Ractor::ClosedError when try to send into a closed actor
assert_equal 'ok', %q{
r = Ractor.new { Ractor.recv }
r.close
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
Ractor.recv
end
r.close
begin
r.take
rescue Ractor::ClosedError
'ok'
else
'ng'
end
}
# Raise Ractor::ClosedError when try to send into a ractor with closed incoming port
assert_equal 'ok', %q{
r = Ractor.new { Ractor.recv }
r.close_incoming
begin
r.send(1)
rescue Ractor::ClosedError
'ok'
else
'ng'
end
}
# 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
Ractor.recv
end
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
Ractor.recv
end
r.close_outgoing
begin
r.send(1)
rescue Ractor::ClosedError
'ng'
else
'ok'
end
}
# multiple Ractors can recv (wait) from one Ractor # multiple Ractors can recv (wait) from one Ractor
assert_equal '[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]', %q{ assert_equal '[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]', %q{
pipe = Ractor.new do pipe = Ractor.new do
@ -559,6 +641,28 @@ assert_equal '[1000, 3]', %q{
Ractor.new{ [A.size, H.size] }.take Ractor.new{ [A.size, H.size] }.take
} }
# Ractor.count
assert_equal '[1, 4, 3, 2, 1]', %q{
counts = []
counts << Ractor.count
ractors = (1..3).map { Ractor.new { Ractor.recv } }
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
}
### ###
### Synchronization tests ### Synchronization tests
### ###