diff options
Diffstat (limited to 'rwtransaction_test.go')
-rw-r--r-- | rwtransaction_test.go | 76 |
1 files changed, 75 insertions, 1 deletions
diff --git a/rwtransaction_test.go b/rwtransaction_test.go index 4d256cc..75a606b 100644 --- a/rwtransaction_test.go +++ b/rwtransaction_test.go @@ -1,12 +1,15 @@ package bolt import ( + "bytes" + "fmt" + "os" "strings" "testing" + "testing/quick" "github.com/stretchr/testify/assert" ) - // Ensure that a RWTransaction can be retrieved. func TestRWTransaction(t *testing.T) { withOpenDB(func(db *DB, path string) { @@ -61,3 +64,74 @@ func TestTransactionCreateBucketWithLongName(t *testing.T) { assert.Equal(t, err, &Error{"bucket name too long", nil}) }) } + +// Ensure that a bucket can write random keys and values across multiple txns. +func TestRWTransactionPutSingle(t *testing.T) { + index := 0 + f := func(items testdata) bool { + withOpenDB(func(db *DB, path string) { + m := make(map[string][]byte) + + db.CreateBucket("widgets") + for _, item := range items { + if err := db.Put("widgets", item.Key, item.Value); err != nil { + panic("put error: " + err.Error()) + } + m[string(item.Key)] = item.Value + + // Verify all key/values so far. + i := 0 + for k, v := range m { + value, err := db.Get("widgets", []byte(k)) + if err != nil { + panic("get error: " + err.Error()) + } + if !bytes.Equal(value, v) { + db.CopyFile("/tmp/bolt.put.single.db") + t.Fatalf("value mismatch [run %d] (%d of %d):\nkey: %x\ngot: %x\nexp: %x", index, i, len(m), []byte(k), value, v) + } + i++ + } + } + + fmt.Fprint(os.Stderr, ".") + }) + index++ + return true + } + if err := quick.Check(f, qconfig()); err != nil { + t.Error(err) + } + fmt.Fprint(os.Stderr, "\n") +} + +// Ensure that a transaction can insert multiple key/value pairs at once. +func TestRWTransactionPutMultiple(t *testing.T) { + f := func(items testdata) bool { + withOpenDB(func(db *DB, path string) { + // Bulk insert all values. + db.CreateBucket("widgets") + rwtxn, _ := db.RWTransaction() + for _, item := range items { + assert.NoError(t, rwtxn.Put("widgets", item.Key, item.Value)) + } + assert.NoError(t, rwtxn.Commit()) + + // Verify all items exist. + txn, _ := db.Transaction() + for _, item := range items { + if !assert.Equal(t, item.Value, txn.Get("widgets", item.Key)) { + db.CopyFile("/tmp/bolt.put.multiple.db") + t.FailNow() + } + } + txn.Close() + }) + fmt.Fprint(os.Stderr, ".") + return true + } + if err := quick.Check(f, qconfig()); err != nil { + t.Error(err) + } + fmt.Fprint(os.Stderr, "\n") +} |