Validate name during initialization
This commit is contained in:
parent
d5fa66156a
commit
398da71175
Notes:
git
2020-09-20 23:11:14 +09:00
@ -8,6 +8,37 @@ assert_equal 'Ractor', %q{
|
|||||||
Ractor.new{}.class
|
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
|
# Ractor.new must call with a block
|
||||||
assert_equal "must be called with a block", %q{
|
assert_equal "must be called with a block", %q{
|
||||||
begin
|
begin
|
||||||
@ -528,20 +559,6 @@ assert_equal '[1000, 3]', %q{
|
|||||||
Ractor.new{ [A.size, H.size] }.take
|
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
|
### Synchronization tests
|
||||||
###
|
###
|
||||||
@ -559,4 +576,3 @@ assert_equal "#{N}#{N}", %Q{
|
|||||||
}
|
}
|
||||||
|
|
||||||
end # if !ENV['GITHUB_WORKFLOW']
|
end # if !ENV['GITHUB_WORKFLOW']
|
||||||
|
|
||||||
|
10
ractor.c
10
ractor.c
@ -1310,6 +1310,16 @@ ractor_init(rb_ractor_t *r, VALUE name, VALUE loc)
|
|||||||
rb_ractor_living_threads_init(r);
|
rb_ractor_living_threads_init(r);
|
||||||
|
|
||||||
// naming
|
// 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->name = name;
|
||||||
r->loc = loc;
|
r->loc = loc;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user