Skip to content

Commit

Permalink
Fix Close() to also wait for view transactions and fix tests as well (#…
Browse files Browse the repository at this point in the history
…91)

* 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
  • Loading branch information
vansante authored and xiang90 committed Aug 28, 2018
1 parent 1b9752f commit e06ec0a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
12 changes: 8 additions & 4 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}

Expand Down
4 changes: 4 additions & 0 deletions tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit e06ec0a

Please sign in to comment.