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
151 lines
4.1 KiB
Ruby
151 lines
4.1 KiB
Ruby
# frozen_string_literal: true
|
|
require 'test/unit'
|
|
require 'tmpdir'
|
|
|
|
class TestTmpdir < Test::Unit::TestCase
|
|
def test_tmpdir_modifiable
|
|
tmpdir = Dir.tmpdir
|
|
assert_not_predicate(tmpdir, :frozen?)
|
|
tmpdir_org = tmpdir.dup
|
|
tmpdir << "foo"
|
|
assert_equal(tmpdir_org, Dir.tmpdir)
|
|
end
|
|
|
|
def test_world_writable
|
|
omit "no meaning on this platform" if /mswin|mingw/ =~ RUBY_PLATFORM
|
|
Dir.mktmpdir do |tmpdir|
|
|
# ToDo: fix for parallel test
|
|
envs = %w[TMPDIR TMP TEMP]
|
|
oldenv = envs.each_with_object({}) {|v, h| h[v] = ENV.delete(v)}
|
|
begin
|
|
envs.each do |e|
|
|
tmpdirx = File.join(tmpdir, e)
|
|
ENV[e] = tmpdirx
|
|
assert_not_equal(tmpdirx, assert_warn('') {Dir.tmpdir})
|
|
File.write(tmpdirx, "")
|
|
assert_not_equal(tmpdirx, assert_warn(/\A#{e} is not a directory/) {Dir.tmpdir})
|
|
File.unlink(tmpdirx)
|
|
ENV[e] = tmpdir
|
|
assert_equal(tmpdir, Dir.tmpdir)
|
|
File.chmod(0555, tmpdir)
|
|
assert_not_equal(tmpdir, assert_warn(/\A#{e} is not writable/) {Dir.tmpdir})
|
|
File.chmod(0777, tmpdir)
|
|
assert_not_equal(tmpdir, assert_warn(/\A#{e} is world-writable/) {Dir.tmpdir})
|
|
newdir = Dir.mktmpdir("d", tmpdir) do |dir|
|
|
assert_file.directory? dir
|
|
assert_equal(tmpdir, File.dirname(dir))
|
|
dir
|
|
end
|
|
assert_file.not_exist?(newdir)
|
|
File.chmod(01777, tmpdir)
|
|
assert_equal(tmpdir, Dir.tmpdir)
|
|
ENV[e] = nil
|
|
end
|
|
ensure
|
|
ENV.update(oldenv)
|
|
end
|
|
end
|
|
end
|
|
|
|
def test_tmpdir_not_empty_parent
|
|
Dir.mktmpdir do |tmpdir|
|
|
envs = %w[TMPDIR TMP TEMP]
|
|
oldenv = envs.each_with_object({}) {|v, h| h[v] = ENV.delete(v)}
|
|
ENV[envs[0]] = ""
|
|
ENV[envs[1]] = tmpdir
|
|
assert_equal(tmpdir, Dir.tmpdir)
|
|
ensure
|
|
ENV.update(oldenv)
|
|
end
|
|
end
|
|
|
|
def test_no_homedir
|
|
bug7547 = '[ruby-core:50793]'
|
|
home, ENV["HOME"] = ENV["HOME"], nil
|
|
dir = assert_nothing_raised(bug7547) do
|
|
break Dir.mktmpdir("~")
|
|
end
|
|
assert_match(/\A~/, File.basename(dir), bug7547)
|
|
ensure
|
|
ENV["HOME"] = home
|
|
Dir.rmdir(dir) if dir
|
|
end
|
|
|
|
def test_mktmpdir_nil
|
|
Dir.mktmpdir(nil) {|d|
|
|
assert_kind_of(String, d)
|
|
}
|
|
end
|
|
|
|
def test_mktmpdir_mutate
|
|
bug16918 = '[ruby-core:98563]'
|
|
assert_nothing_raised(bug16918) do
|
|
assert_mktmpdir_traversal do |traversal_path|
|
|
Dir.mktmpdir(traversal_path + 'foo') do |actual|
|
|
actual << "foo"
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def test_mktmpdir_traversal
|
|
assert_mktmpdir_traversal do |traversal_path|
|
|
Dir.mktmpdir(traversal_path + 'foo') do |actual|
|
|
actual
|
|
end
|
|
end
|
|
end
|
|
|
|
def test_mktmpdir_traversal_array
|
|
assert_mktmpdir_traversal do |traversal_path|
|
|
Dir.mktmpdir([traversal_path, 'foo']) do |actual|
|
|
actual
|
|
end
|
|
end
|
|
end
|
|
|
|
def test_mktmpdir_not_empty_parent
|
|
assert_raise(ArgumentError) do
|
|
Dir.mktmpdir("foo", "")
|
|
end
|
|
|
|
path = Struct.new(:to_path).new("")
|
|
assert_raise(ArgumentError) do
|
|
Dir.mktmpdir("foo", path)
|
|
end
|
|
|
|
Dir.mktmpdir do |d|
|
|
path = Struct.new(:to_path).new(d)
|
|
assert_operator(Dir.mktmpdir("prefix-", path), :start_with?, d + "/prefix-")
|
|
end
|
|
end
|
|
|
|
def assert_mktmpdir_traversal
|
|
Dir.mktmpdir do |target|
|
|
target = target.chomp('/') + '/'
|
|
traversal_path = target.sub(/\A\w:/, '') # for DOSISH
|
|
traversal_path = Array.new(target.count('/')-2, '..').join('/') + traversal_path
|
|
[File::SEPARATOR, File::ALT_SEPARATOR].compact.each do |separator|
|
|
actual = yield traversal_path.tr('/', separator)
|
|
assert_not_send([File.absolute_path(actual), :start_with?, target])
|
|
end
|
|
end
|
|
end
|
|
|
|
def test_ractor
|
|
assert_ractor(<<~'end;', require: "tmpdir")
|
|
port = Ractor::Port.new
|
|
r = Ractor.new port do |port|
|
|
Dir.mktmpdir() do |d|
|
|
port << d
|
|
Ractor.receive
|
|
end
|
|
end
|
|
dir = port.receive
|
|
assert_file.directory? dir
|
|
r.send true
|
|
r.join
|
|
assert_file.not_exist? dir
|
|
end;
|
|
end
|
|
end
|