diff --git a/test/test_helper.rb b/test/test_helper.rb index 29d046b..64a2f41 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -20,7 +20,7 @@ module En57 ActiveRecord::Base.establish_connection(SERVER.url) AR_POOL = ActiveRecord::Base.connection_pool ADAPTERS = { - pg: -> { PgAdapter.new(SERVER.url) }, + pg: -> { PgAdapter.new(SERVER.url, max_connections: 8) }, sequel: -> { SequelAdapter.new(SEQUEL_DB) }, active_record: -> { ActiveRecordAdapter.new(AR_POOL) }, } diff --git a/test/test_stress.rb b/test/test_stress.rb index 820b236..8b4871b 100644 --- a/test/test_stress.rb +++ b/test/test_stress.rb @@ -21,16 +21,17 @@ module En57 ], ) - barrier = Concurrent::CyclicBarrier.new(concurrency) - threads = - Array.new(concurrency) do - Thread.new do - with_event_store(factory) do |event_store_| + barrier = Concurrent::CyclicBarrier.new(concurrency) + threads = + Array.new(concurrency) do + Thread.new do + Thread.current.report_on_exception = false + + account_scope = event_store.read.with_tag(account_tag) barrier.wait - account_scope = event_store_.read.with_tag(account_tag) if account_balance(account_scope) >= 100 - event_store_.append( + event_store.append( [ Event.new( type: "CreditsUsed", @@ -43,19 +44,24 @@ module En57 fail_if: account_scope.of_type("CreditsUsed"), ) end + rescue AppendConditionViolated => e + e end - rescue AppendConditionViolated end - end - threads.each(&:join) - - account_scope = event_store.read.with_tag(account_tag) - assert_equal 1, account_scope.of_type("CreditsUsed").each.count - assert_equal 0, account_balance(account_scope) + assert_equal( + (concurrency - 1), + threads + .map(&:value) + .select { AppendConditionViolated === it } + .size, + ) + account_scope = event_store.read.with_tag(account_tag) + assert_equal(1, account_scope.of_type("CreditsUsed").each.count) + assert_equal(0, account_balance(account_scope)) + end end end - end private