Fix Close() to also wait for view transactions and fix tests as well ()

* Fix testDB_Close_PendingTx to do something with the writable arg and stop it from closing twice

* Fix Close() to wait for view transactions by getting a full lock on mmaplock

* Fix the TestTx_Check_ReadOnly to close the view transaction

* Fix the TestTx_Commit_ErrTxNotWritable to close the view transaction
pull/119/head
Paul 2018-08-28 04:56:38 +02:00 committed by Xiang Li
parent 1b9752fe53
commit e06ec0a754
3 changed files with 14 additions and 6 deletions

4
db.go
View File

@ -454,8 +454,8 @@ func (db *DB) Close() error {
db.metalock.Lock()
defer db.metalock.Unlock()
db.mmaplock.RLock()
defer db.mmaplock.RUnlock()
db.mmaplock.Lock()
defer db.mmaplock.Unlock()
return db.close()
}

View File

@ -662,10 +662,9 @@ func TestDB_Close_PendingTx_RO(t *testing.T) { testDB_Close_PendingTx(t, false)
// Ensure that a database cannot close while transactions are open.
func testDB_Close_PendingTx(t *testing.T, writable bool) {
db := MustOpenDB()
defer db.MustClose()
// Start transaction.
tx, err := db.Begin(true)
tx, err := db.Begin(writable)
if err != nil {
t.Fatal(err)
}
@ -687,8 +686,13 @@ func testDB_Close_PendingTx(t *testing.T, writable bool) {
default:
}
// Commit transaction.
if err := tx.Commit(); err != nil {
// Commit/close transaction.
if writable {
err = tx.Commit()
} else {
err = tx.Rollback()
}
if err != nil {
t.Fatal(err)
}

View File

@ -57,6 +57,8 @@ func TestTx_Check_ReadOnly(t *testing.T) {
t.Fatal(err)
}
}
// Close the view transaction
tx.Rollback()
}
// Ensure that committing a closed transaction returns an error.
@ -110,6 +112,8 @@ func TestTx_Commit_ErrTxNotWritable(t *testing.T) {
if err := tx.Commit(); err != bolt.ErrTxNotWritable {
t.Fatal(err)
}
// Close the view transaction
tx.Rollback()
}
// Ensure that a transaction can retrieve a cursor on the root bucket.