diff --git a/test/test_concurrency.rb b/test/test_concurrency.rb index 4efbe6a..0f816cf 100644 --- a/test/test_concurrency.rb +++ b/test/test_concurrency.rb @@ -54,6 +54,32 @@ def test_forking_with_pipelining MAX_THREADS.times { |i| assert_equal(WANT, @client.call('GET', "key#{i}")) } end + def test_forking_with_transaction + skip("fork is not available on #{RUBY_ENGINE}") if %w[jruby truffleruby].include?(RUBY_ENGINE) + + @client.call('SET', '{key}1', WANT) + + pids = Array.new(MAX_THREADS) do + Process.fork do + @client.multi(watch: %w[{key}1]) do |tx| + ATTEMPTS.times do + MAX_THREADS.times do + tx.call('INCR', '{key}1') + tx.call('DECR', '{key}1') + end + end + end + end + end + + pids.each do |pid| + _, status = Process.waitpid2(pid) + assert_predicate(status, :success?) + end + + assert_equal(WANT, @client.call('GET', '{key}1')) + end + def test_threading threads = Array.new(MAX_THREADS) do Thread.new do @@ -84,6 +110,28 @@ def test_threading_with_pipelining MAX_THREADS.times { |i| assert_equal(WANT, @client.call('GET', "key#{i}")) } end + def test_threading_with_transaction + @client.call('SET', '{key}1', WANT) + + threads = Array.new(MAX_THREADS) do + Thread.new do + @client.multi(watch: %w[{key}1]) do |tx| + ATTEMPTS.times do + MAX_THREADS.times do + tx.call('INCR', '{key}1') + tx.call('DECR', '{key}1') + end + end + end + rescue StandardError => e + e + end + end + + threads.each { |t| refute_instance_of(StandardError, t.value) } + assert_equal(WANT, @client.call('GET', '{key}1')) + end + private def new_test_client