MCPcopy
hub / github.com/etcd-io/bbolt / TestDB_Batch

Function TestDB_Batch

db_test.go:1319–1361  ·  view source on GitHub ↗

Ensure two functions can perform updates in a single batch.

(t *testing.T)

Source from the content-addressed store, hash-verified

1317
1318// Ensure two functions can perform updates in a single batch.
1319func TestDB_Batch(t *testing.T) {
1320 db := btesting.MustCreateDB(t)
1321
1322 if err := db.Update(func(tx *bolt.Tx) error {
1323 if _, err := tx.CreateBucket([]byte("widgets")); err != nil {
1324 t.Fatal(err)
1325 }
1326 return nil
1327 }); err != nil {
1328 t.Fatal(err)
1329 }
1330
1331 // Iterate over multiple updates in separate goroutines.
1332 n := 2
1333 ch := make(chan error, n)
1334 for i := 0; i < n; i++ {
1335 go func(i int) {
1336 ch <- db.Batch(func(tx *bolt.Tx) error {
1337 return tx.Bucket([]byte("widgets")).Put(u64tob(uint64(i)), []byte{})
1338 })
1339 }(i)
1340 }
1341
1342 // Check all responses to make sure there's no error.
1343 for i := 0; i < n; i++ {
1344 if err := <-ch; err != nil {
1345 t.Fatal(err)
1346 }
1347 }
1348
1349 // Ensure data is correct.
1350 if err := db.View(func(tx *bolt.Tx) error {
1351 b := tx.Bucket([]byte("widgets"))
1352 for i := 0; i < n; i++ {
1353 if v := b.Get(u64tob(uint64(i))); v == nil {
1354 t.Errorf("key not found: %d", i)
1355 }
1356 }
1357 return nil
1358 }); err != nil {
1359 t.Fatal(err)
1360 }
1361}
1362
1363func TestDB_Batch_Panic(t *testing.T) {
1364 db := btesting.MustCreateDB(t)

Callers

nothing calls this directly

Calls 11

MustCreateDBFunction · 0.92
u64tobFunction · 0.85
UpdateMethod · 0.80
BatchMethod · 0.80
ViewMethod · 0.80
FatalMethod · 0.65
ErrorfMethod · 0.65
CreateBucketMethod · 0.45
PutMethod · 0.45
BucketMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected