diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-03-13 14:39:28 -0600 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-03-13 14:39:28 -0600 |
commit | 4132080333b1cb0e96b7b8732e763f96d69074ee (patch) | |
tree | ca9076ef7bf7d42ce9ae38a32df5c52cee6ce002 /tx_test.go | |
parent | README (diff) | |
download | dedo-4132080333b1cb0e96b7b8732e763f96d69074ee.tar.gz dedo-4132080333b1cb0e96b7b8732e763f96d69074ee.tar.xz |
Fix Cursor.Last() on empty buckets.
@tv42 reported that creating a cursor on an empty bucket and then calling
Cursor.Last() causes an index out of range error and panics. This commit
adds a check for the page's item count being greater than zero.
Fixes #63.
Diffstat (limited to 'tx_test.go')
-rw-r--r-- | tx_test.go | 27 |
1 files changed, 21 insertions, 6 deletions
@@ -190,12 +190,27 @@ func TestTxDeleteBucketNotFound(t *testing.T) { func TestTxCursorEmptyBucket(t *testing.T) { withOpenDB(func(db *DB, path string) { db.CreateBucket("widgets") - tx, _ := db.Tx() - c := tx.Bucket("widgets").Cursor() - k, v := c.First() - assert.Nil(t, k) - assert.Nil(t, v) - tx.Commit() + db.With(func(tx *Tx) error { + c := tx.Bucket("widgets").Cursor() + k, v := c.First() + assert.Nil(t, k) + assert.Nil(t, v) + return nil + }) + }) +} + +// Ensure that a Tx cursor can reverse iterate over an empty bucket without error. +func TestCursorEmptyBucketReverse(t *testing.T) { + withOpenDB(func(db *DB, path string) { + db.CreateBucket("widgets") + db.With(func(tx *Tx) error { + c := tx.Bucket("widgets").Cursor() + k, v := c.Last() + assert.Nil(t, k) + assert.Nil(t, v) + return nil + }) }) } |