batch_test
batch_test.go - Overview
This file contains tests for the WriteBatch functionality of the BadgerDB. It tests various scenarios, including writing a large number of entries, deleting entries, flushing empty write batches, and handling errors during flush operations.
Detailed Documentation
TestWriteBatch
Purpose: Tests the basic functionality of WriteBatch, including setting and deleting keys, and verifying the data using an iterator.
Parameters:
t
(*testing.T): Testing context.
Returns:
- None
TestEmptyWriteBatch
Purpose: Tests the behavior of flushing empty WriteBatch in both normal and managed modes.
Parameters:
t
(*testing.T): Testing context.
Returns:
- None
TestFlushPanic
Purpose: Tests that the code does not panic when attempting to flush a WriteBatch after it has already been flushed or canceled.
Parameters:
t
(*testing.T): Testing context.
Returns:
- None
TestBatchErrDeadlock
Purpose: Tests for a potential deadlock situation when using managed transactions and encountering an error during WriteBatch operations.
Parameters:
t
(*testing.T): Testing context.
Returns:
- None
Code Examples
The TestWriteBatch
function provides a comprehensive example of how to use the WriteBatch
API.
wb := db.NewWriteBatch()
defer wb.Cancel()
N, M := 50000, 1000
for i := 0; i < N; i++ {
require.NoError(t, wb.Set(key(i), val(i)))
}
for i := 0; i < M; i++ {
require.NoError(t, wb.Delete(key(i)))
}
require.NoError(t, wb.Flush())
This example demonstrates creating a new WriteBatch
, setting a large number of keys, deleting some keys, and then flushing the batch to the database. The defer wb.Cancel()
ensures that the WriteBatch is cancelled when the function exits.