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

Validate name during initialization

This commit is contained in:
Quang-Minh Nguyen 2020-09-18 13:02:14 +07:00 committed by Koichi Sasada
parent d5fa66156a
commit 398da71175
Notes: git 2020-09-20 23:11:14 +09:00
2 changed files with 43 additions and 17 deletions

View file

@ -8,6 +8,37 @@ assert_equal 'Ractor', %q{
Ractor.new{}.class
}
# A Ractor can have a name
assert_equal 'test-name', %q{
r = Ractor.new name: 'test-name' do
end
r.name
}
# If Ractor doesn't have a name, Ractor#name returns nil.
assert_equal 'nil', %q{
r = Ractor.new do
end
r.name.inspect
}
# Raises exceptions if initialize with invalid name
assert_equal 'no implicit conversion of Array into String', %q{
begin
r = Ractor.new(name: [{}]) {}
rescue TypeError => e
e.message
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
}
# Ractor.new must call with a block
assert_equal "must be called with a block", %q{
begin
@ -263,7 +294,7 @@ assert_equal 'false', %q{
r = Ractor.new obj do |msg|
msg.object_id
end
obj.object_id == r.take
}
@ -360,7 +391,7 @@ assert_equal 'hello', %q{
str = r.take
begin
r.take
r.take
rescue Ractor::RemoteError
str #=> "hello"
end
@ -528,20 +559,6 @@ assert_equal '[1000, 3]', %q{
Ractor.new{ [A.size, H.size] }.take
}
# A Ractor can have a name
assert_equal 'test-name', %q{
r = Ractor.new name: 'test-name' do
end
r.name
}
# If Ractor doesn't have a name, Ractor#name returns nil.
assert_equal 'nil', %q{
r = Ractor.new do
end
r.name.inspect
}
###
### Synchronization tests
###
@ -559,4 +576,3 @@ assert_equal "#{N}#{N}", %Q{
}
end # if !ENV['GITHUB_WORKFLOW']

View file

@ -1310,6 +1310,16 @@ ractor_init(rb_ractor_t *r, VALUE name, VALUE loc)
rb_ractor_living_threads_init(r);
// naming
if (!NIL_P(name)) {
rb_encoding *enc;
StringValueCStr(name);
enc = rb_enc_get(name);
if (!rb_enc_asciicompat(enc)) {
rb_raise(rb_eArgError, "ASCII incompatible encoding (%s)",
rb_enc_name(enc));
}
name = rb_str_new_frozen(name);
}
r->name = name;
r->loc = loc;
}