diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-04-04 07:51:01 -0600 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-04-04 07:59:24 -0600 |
commit | 394e42e3ebd70b10488aa791894db28e9410077c (patch) | |
tree | 0b6843bb694a9a940910965e81a5e32dad9e4b03 /tx_test.go | |
parent | README (diff) | |
download | dedo-394e42e3ebd70b10488aa791894db28e9410077c.tar.gz dedo-394e42e3ebd70b10488aa791894db28e9410077c.tar.xz |
Add Tx.OnCommit() handler.
This commit adds the ability to execute a function after a transaction has
successfully committed.
Diffstat (limited to 'tx_test.go')
-rw-r--r-- | tx_test.go | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -1,6 +1,7 @@ package bolt import ( + "errors" "fmt" "math/rand" "os" @@ -484,6 +485,33 @@ func TestTxCursorIterateReverse(t *testing.T) { fmt.Fprint(os.Stderr, "\n") } +// Ensure that Tx commit handlers are called after a transaction successfully commits. +func TestTx_OnCommit(t *testing.T) { + var x int + withOpenDB(func(db *DB, path string) { + db.Update(func(tx *Tx) error { + tx.OnCommit(func() { x += 1 }) + tx.OnCommit(func() { x += 2 }) + return tx.CreateBucket("widgets") + }) + }) + assert.Equal(t, 3, x) +} + +// Ensure that Tx commit handlers are NOT called after a transaction rolls back. +func TestTx_OnCommit_Rollback(t *testing.T) { + var x int + withOpenDB(func(db *DB, path string) { + db.Update(func(tx *Tx) error { + tx.OnCommit(func() { x += 1 }) + tx.OnCommit(func() { x += 2 }) + tx.CreateBucket("widgets") + return errors.New("rollback this commit") + }) + }) + assert.Equal(t, 0, x) +} + // Benchmark the performance iterating over a cursor. func BenchmarkTxCursor(b *testing.B) { indexes := rand.Perm(b.N) |