Skip to content

Commit

Permalink
Improve test_database_transaction_commit
Browse files Browse the repository at this point in the history
  • Loading branch information
noteflakes committed Dec 22, 2023
1 parent c9a7116 commit d891b26
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions test/test_database.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

require_relative 'helper'

require 'date'
require 'tempfile'

class DatabaseTest < MiniTest::Test
def setup
Expand Down Expand Up @@ -439,17 +441,34 @@ def test_string_encoding
end

def test_database_transaction_commit
db = Extralite::Database.new(':memory:')
db.execute('create table foo(x)')

assert_equal [], db.query('select * from foo')

exception = nil
db.transaction do
db.execute('insert into foo values (42)')
path = Tempfile.new('extralite_transaction_commit').path
db1 = Extralite::Database.new(path)
db2 = Extralite::Database.new(path)

db1.execute('create table foo(x)')
assert_equal ['foo'], db1.tables
assert_equal ['foo'], db2.tables

q1 = Queue.new
q2 = Queue.new
th = Thread.new do
db1.transaction do
assert_equal true, db1.transaction_active?
db1.execute('insert into foo values (42)')
q1 << true
q2.pop
end
assert_equal false, db1.transaction_active?
end

assert_equal [{ x: 42 }], db.query('select * from foo')
q1.pop
# transaction not yet committed
assert_equal false, db2.transaction_active?
assert_equal [], db2.query('select * from foo')

q2 << true
th.join
# transaction now committed
assert_equal [{ x: 42 }], db2.query('select * from foo')
end

def test_database_transaction_rollback
Expand Down

0 comments on commit d891b26

Please sign in to comment.