|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +# These specs verify the extension is usable from non-main Ractors. A C |
| 4 | +# extension that doesn't declare rb_ext_ractor_safe(true) raises |
| 5 | +# Ractor::UnsafeError the moment one of its methods is called off the main |
| 6 | +# Ractor, so simply round-tripping inside Ractor.new exercises the safety |
| 7 | +# declaration. The parallel spec additionally stresses the page-allocation |
| 8 | +# path that used to be a process-global, lock-free slab (the reason the |
| 9 | +# extension was not Ractor-safe). |
| 10 | +# |
| 11 | +# Note: the top-level MessagePack.pack / .unpack helpers reach a shared, |
| 12 | +# non-shareable MessagePack::DefaultFactory and so can't be used from a |
| 13 | +# non-main Ractor regardless of this extension's safety. The supported pattern |
| 14 | +# is to build your own Factory/Packer/Unpacker inside the Ractor, which is |
| 15 | +# what these specs do. |
| 16 | + |
| 17 | +ractor_supported = defined?(Ractor) && RUBY_ENGINE == 'ruby' |
| 18 | + |
| 19 | +describe 'Ractor safety', skip: (ractor_supported ? false : 'Ractor not supported on this Ruby') do |
| 20 | + def ractor_value(ractor) |
| 21 | + # Ractor#value replaced #take in newer rubies; support both. |
| 22 | + ractor.respond_to?(:value) ? ractor.value : ractor.take |
| 23 | + end |
| 24 | + |
| 25 | + # Ruby prints a one-time "Ractor API is experimental" warning to stderr. Quiet |
| 26 | + # it for this group, and restore afterwards so we don't suppress the warning |
| 27 | + # for unrelated specs sharing the process. |
| 28 | + before(:all) do |
| 29 | + @experimental_warning = Warning[:experimental] |
| 30 | + Warning[:experimental] = false |
| 31 | + end |
| 32 | + |
| 33 | + after(:all) do |
| 34 | + Warning[:experimental] = @experimental_warning |
| 35 | + end |
| 36 | + |
| 37 | + it 'round-trips via a Factory inside a non-main Ractor' do |
| 38 | + result = ractor_value(Ractor.new do |
| 39 | + factory = MessagePack::Factory.new |
| 40 | + factory.load(factory.dump([1, "two", 3.0, nil, true, {"k" => "v"}])) |
| 41 | + end) |
| 42 | + expect(result).to eq([1, "two", 3.0, nil, true, {"k" => "v"}]) |
| 43 | + end |
| 44 | + |
| 45 | + it 'round-trips via a Packer and Unpacker inside a non-main Ractor' do |
| 46 | + result = ractor_value(Ractor.new do |
| 47 | + packed = MessagePack::Packer.new.write({"x" => [1, 2, 3]}).to_s |
| 48 | + MessagePack::Unpacker.new.feed(packed).read |
| 49 | + end) |
| 50 | + expect(result).to eq({"x" => [1, 2, 3]}) |
| 51 | + end |
| 52 | + |
| 53 | + it 'packs and unpacks concurrently across many Ractors without corruption' do |
| 54 | + ractor_count = 8 |
| 55 | + |
| 56 | + ractors = ractor_count.times.map do |n| |
| 57 | + Ractor.new(n) do |seed| |
| 58 | + obj = { |
| 59 | + "seed" => seed, |
| 60 | + "nums" => (0..20).to_a, |
| 61 | + "str" => "x" * 100, |
| 62 | + "nested" => {"deep" => [seed] * 10}, |
| 63 | + } |
| 64 | + ok = true |
| 65 | + 2_000.times do |
| 66 | + packed = MessagePack::Packer.new.write(obj).to_s |
| 67 | + ok &&= MessagePack::Unpacker.new.feed(packed).read == obj |
| 68 | + end |
| 69 | + ok |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + expect(ractors.map { |r| ractor_value(r) }).to all(be true) |
| 74 | + end |
| 75 | +end |
0 commit comments