ruby/test/did_you_mean/test_ractor_compatibility.rb
Koichi Sasada ef2bb61018 Ractor::Port
* Added `Ractor::Port`
  * `Ractor::Port#receive` (support multi-threads)
  * `Rcator::Port#close`
  * `Ractor::Port#closed?`
* Added some methods
  * `Ractor#join`
  * `Ractor#value`
  * `Ractor#monitor`
  * `Ractor#unmonitor`
* Removed some methods
  * `Ractor#take`
  * `Ractor.yield`
* Change the spec
  * `Racotr.select`

You can wait for multiple sequences of messages with `Ractor::Port`.

```ruby
ports = 3.times.map{ Ractor::Port.new }
ports.map.with_index do |port, ri|
  Ractor.new port,ri do |port, ri|
    3.times{|i| port << "r#{ri}-#{i}"}
  end
end

p ports.each{|port| pp 3.times.map{port.receive}}

```

In this example, we use 3 ports, and 3 Ractors send messages to them respectively.
We can receive a series of messages from each port.

You can use `Ractor#value` to get the last value of a Ractor's block:

```ruby
result = Ractor.new do
  heavy_task()
end.value
```

You can wait for the termination of a Ractor with `Ractor#join` like this:

```ruby
Ractor.new do
  some_task()
end.join
```

`#value` and `#join` are similar to `Thread#value` and `Thread#join`.

To implement `#join`, `Ractor#monitor` (and `Ractor#unmonitor`) is introduced.

This commit changes `Ractor.select()` method.
It now only accepts ports or Ractors, and returns when a port receives a message or a Ractor terminates.

We removes `Ractor.yield` and `Ractor#take` because:
* `Ractor::Port` supports most of similar use cases in a simpler manner.
* Removing them significantly simplifies the code.

We also change the internal thread scheduler code (thread_pthread.c):
* During barrier synchronization, we keep the `ractor_sched` lock to avoid deadlocks.
  This lock is released by `rb_ractor_sched_barrier_end()`
  which is called at the end of operations that require the barrier.
* fix potential deadlock issues by checking interrupts just before setting UBF.

https://bugs.ruby-lang.org/issues/21262
2025-05-31 04:01:33 +09:00

117 lines
3.6 KiB
Ruby

require_relative './helper'
return if not DidYouMean::TestHelper.ractor_compatible?
class RactorCompatibilityTest < Test::Unit::TestCase
def test_class_name_suggestion_works_in_ractor
assert_ractor(<<~CODE, require_relative: "helper")
class ::Book; end
include DidYouMean::TestHelper
error = Ractor.new {
begin
Boook
rescue NameError => e
e.corrections # It is important to call the #corrections method within Ractor.
e
end
}.value
assert_correction "Book", error.corrections
CODE
end
def test_key_name_suggestion_works_in_ractor
assert_ractor(<<~CODE, require_relative: "helper")
include DidYouMean::TestHelper
error = Ractor.new {
begin
hash = { "foo" => 1, bar: 2 }
hash.fetch(:bax)
rescue KeyError => e
e.corrections # It is important to call the #corrections method within Ractor.
e
end
}.value
assert_correction ":bar", error.corrections
assert_match "Did you mean? :bar", get_message(error)
CODE
end
def test_method_name_suggestion_works_in_ractor
assert_ractor(<<~CODE, require_relative: "helper")
include DidYouMean::TestHelper
error = Ractor.new {
begin
self.to__s
rescue NoMethodError => e
e.corrections # It is important to call the #corrections method within Ractor.
e
end
}.value
assert_correction :to_s, error.corrections
assert_match "Did you mean? to_s", get_message(error)
CODE
end
if defined?(::NoMatchingPatternKeyError)
def test_pattern_key_name_suggestion_works_in_ractor
assert_ractor(<<~CODE, require_relative: "helper")
include DidYouMean::TestHelper
error = Ractor.new {
begin
eval(<<~RUBY, binding, __FILE__, __LINE__)
hash = {foo: 1, bar: 2, baz: 3}
hash => {fooo:}
fooo = 1 # suppress "unused variable: fooo" warning
RUBY
rescue NoMatchingPatternKeyError => e
e.corrections # It is important to call the #corrections method within Ractor.
e
end
}.value
assert_correction ":foo", error.corrections
assert_match "Did you mean? :foo", get_message(error)
CODE
end
end
def test_can_raise_other_name_error_in_ractor
assert_ractor(<<~CODE, require_relative: "helper")
class FirstNameError < NameError; end
include DidYouMean::TestHelper
error = Ractor.new {
begin
raise FirstNameError, "Other name error"
rescue FirstNameError => e
e.corrections # It is important to call the #corrections method within Ractor.
e
end
}.value
assert_not_match(/Did you mean\?/, error.message)
CODE
end
def test_variable_name_suggestion_works_in_ractor
assert_ractor(<<~CODE, require_relative: "helper")
include DidYouMean::TestHelper
error = Ractor.new {
in_ractor = in_ractor = 1
begin
in_reactor
rescue NameError => e
e.corrections # It is important to call the #corrections method within Ractor.
e
end
}.value
assert_correction :in_ractor, error.corrections
assert_match "Did you mean? in_ractor", get_message(error)
CODE
end
end