Ensure two functions can perform updates in a single batch.
(t *testing.T)
| 1317 | |
| 1318 | // Ensure two functions can perform updates in a single batch. |
| 1319 | func 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 | |
| 1363 | func TestDB_Batch_Panic(t *testing.T) { |
| 1364 | db := btesting.MustCreateDB(t) |