mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 05:29:10 +02:00

* 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
70 lines
2 KiB
Ruby
70 lines
2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
return unless defined?(Ractor) && Process.respond_to?(:fork)
|
|
|
|
require_relative "test_helper"
|
|
|
|
module Prism
|
|
class RactorTest < TestCase
|
|
def test_version
|
|
assert_match(/\A\d+\.\d+\.\d+\z/, with_ractor { Prism::VERSION })
|
|
end
|
|
|
|
def test_parse_file
|
|
assert_equal("Prism::ParseResult", with_ractor(__FILE__) { |filepath| Prism.parse_file(filepath).class })
|
|
end
|
|
|
|
def test_lex_file
|
|
assert_equal("Prism::LexResult", with_ractor(__FILE__) { |filepath| Prism.lex_file(filepath).class })
|
|
end
|
|
|
|
def test_parse_file_comments
|
|
assert_equal("Array", with_ractor(__FILE__) { |filepath| Prism.parse_file_comments(filepath).class })
|
|
end
|
|
|
|
def test_parse_lex_file
|
|
assert_equal("Prism::ParseLexResult", with_ractor(__FILE__) { |filepath| Prism.parse_lex_file(filepath).class })
|
|
end
|
|
|
|
def test_parse_success
|
|
assert_equal("true", with_ractor("1 + 1") { |source| Prism.parse_success?(source) })
|
|
end
|
|
|
|
def test_parse_failure
|
|
assert_equal("true", with_ractor("1 +") { |source| Prism.parse_failure?(source) })
|
|
end
|
|
|
|
def test_string_query_local
|
|
assert_equal("true", with_ractor("foo") { |source| StringQuery.local?(source) })
|
|
end
|
|
|
|
def test_string_query_constant
|
|
assert_equal("true", with_ractor("FOO") { |source| StringQuery.constant?(source) })
|
|
end
|
|
|
|
def test_string_query_method_name
|
|
assert_equal("true", with_ractor("foo?") { |source| StringQuery.method_name?(source) })
|
|
end
|
|
|
|
if !ENV["PRISM_BUILD_MINIMAL"]
|
|
def test_dump_file
|
|
result = with_ractor(__FILE__) { |filepath| Prism.dump_file(filepath) }
|
|
assert_operator(result, :start_with?, "PRISM")
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
# Note that this must be done in a subprocess, otherwise it can mess up
|
|
# CRuby's test suite.
|
|
def with_ractor(*arguments, &block)
|
|
IO.popen("-") do |reader|
|
|
if reader
|
|
reader.gets.chomp
|
|
else
|
|
puts(ignore_warnings { Ractor.new(*arguments, &block) }.value)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|