Skip to content

Commit

Permalink
test: concurrency for the transaction (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
supercaracal authored Feb 20, 2024
1 parent 597ef95 commit 1607da0
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions test/test_concurrency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1607da0

Please sign in to comment.