diff options
author | Koichi Sasada <ko1@atdot.net> | 2020-12-08 14:04:18 +0900 |
---|---|---|
committer | Koichi Sasada <ko1@atdot.net> | 2020-12-16 19:12:48 +0900 |
commit | a9a7f4d8b8ec30abc7a47ce700edc7209ae12279 (patch) | |
tree | bc0092b4e1643cc0889c53a0941319ecf76ae190 /bootstraptest/test_ractor.rb | |
parent | ddb93c3d64b8997c85f23c96a73ad0a9a7f82ad1 (diff) |
Ractor#receive_if to receive only matched messages
Instead of Ractor.receive, Ractor.receive_if can provide a pattern
by a block and you can choose the receiving message.
[Feature #17378]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3862
Diffstat (limited to 'bootstraptest/test_ractor.rb')
-rw-r--r-- | bootstraptest/test_ractor.rb | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/bootstraptest/test_ractor.rb b/bootstraptest/test_ractor.rb index b13ecbe3e9..cde0f92962 100644 --- a/bootstraptest/test_ractor.rb +++ b/bootstraptest/test_ractor.rb @@ -100,6 +100,64 @@ assert_equal 'ok', %q{ r.take } +# 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 = [] + + 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 + + a # +} + ### ### # Ractor still has several memory corruption so skip huge number of tests |