From 3a49d9f9f5618c4e97602cf7eb56eb973f428e40 Mon Sep 17 00:00:00 2001 From: Watson Date: Sat, 3 Sep 2022 16:21:51 +0900 Subject: [PATCH] Add Ractor support --- ext/zstdruby/main.c | 4 ++++ spec/zstd-ruby-streaming-compress_spec.rb | 13 +++++++++++++ spec/zstd-ruby-streaming-decompress_spec.rb | 16 ++++++++++++++++ spec/zstd-ruby_spec.rb | 9 +++++++++ 4 files changed, 42 insertions(+) diff --git a/ext/zstdruby/main.c b/ext/zstdruby/main.c index eece212..ecd906a 100644 --- a/ext/zstdruby/main.c +++ b/ext/zstdruby/main.c @@ -7,6 +7,10 @@ void zstd_ruby_streaming_decompress_init(void); void Init_zstdruby(void) { +#ifdef HAVE_RB_EXT_RACTOR_SAFE + rb_ext_ractor_safe(true); +#endif + rb_mZstd = rb_define_module("Zstd"); zstd_ruby_init(); zstd_ruby_streaming_compress_init(); diff --git a/spec/zstd-ruby-streaming-compress_spec.rb b/spec/zstd-ruby-streaming-compress_spec.rb index 319bb6b..5c3f3b4 100644 --- a/spec/zstd-ruby-streaming-compress_spec.rb +++ b/spec/zstd-ruby-streaming-compress_spec.rb @@ -52,5 +52,18 @@ expect(Zstd.decompress(res)).to eq('abcdef') end end + + if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0') + describe 'Ractor' do + it 'should be supported' do + r = Ractor.new { + stream = Zstd::StreamingCompress.new(5) + stream << "abc" << "def" + res = stream.finish + } + expect(Zstd.decompress(r.take)).to eq('abcdef') + end + end + end end diff --git a/spec/zstd-ruby-streaming-decompress_spec.rb b/spec/zstd-ruby-streaming-decompress_spec.rb index 5889481..cddc119 100644 --- a/spec/zstd-ruby-streaming-decompress_spec.rb +++ b/spec/zstd-ruby-streaming-decompress_spec.rb @@ -32,5 +32,21 @@ end end + if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0') + describe 'Ractor' do + it 'should be supported' do + r = Ractor.new { + cstr = Zstd.compress('foo bar buzz') + stream = Zstd::StreamingDecompress.new + result = '' + result << stream.decompress(cstr[0, 5]) + result << stream.decompress(cstr[5, 5]) + result << stream.decompress(cstr[10..-1]) + result + } + expect(r.take).to eq('foo bar buzz') + end + end + end end diff --git a/spec/zstd-ruby_spec.rb b/spec/zstd-ruby_spec.rb index e7cad90..e8302c2 100644 --- a/spec/zstd-ruby_spec.rb +++ b/spec/zstd-ruby_spec.rb @@ -74,5 +74,14 @@ def to_str expect(Zstd.decompress(DummyForDecompress.new)).to eq('abc') end end + + if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0') + describe 'Ractor' do + it 'should be supported' do + r = Ractor.new { Zstd.compress('abc') } + expect(Zstd.decompress(r.take)).to eq('abc') + end + end + end end