db2_test.go
db2_test.go - Overview
This file contains various test functions for the BadgerDB, focusing on scenarios like value log truncation, handling large key-value pairs, data recovery after crashes, compaction file picking, and ensuring data integrity during garbage collection and database operations. The tests cover both disk-based and in-memory modes, as well as managed and unmanaged database operations.
Detailed Documentation
TestTruncateVlogWithClose
Purpose: Tests the scenario where the value log is truncated after a database is closed and then reopened. It verifies that data written after reopening the database can be read correctly.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
TestTruncateVlogNoClose
Purpose: This test is meant to be run manually. It sets up the database and inserts a large entry into the value log. This simulates a crash scenario for value log truncation testing when the database isn't closed properly.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
TestTruncateVlogNoClose2
Purpose: This test is meant to be run manually. It reopens the database created by TestTruncateVlogNoClose
and adds more data. It then verifies that the newly added data can be read successfully. This continues the simulation of a crash scenario for value log truncation testing.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
TestTruncateVlogNoClose3
Purpose: This test is meant to be run manually. It reopens the database created by TestTruncateVlogNoClose2
and verifies that all previously written data can still be read correctly after the simulated crash and value log truncation.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
TestBigKeyValuePairs
Purpose: Tests the handling of very large key-value pairs that exceed the default size limits. It checks if the database correctly rejects oversized keys and values and ensures that valid data can still be stored and retrieved.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
TestPushValueLogLimit
Purpose: Tests the behavior of the database when attempting to write a value that exceeds the value log file size limit. This ensures that the database handles such scenarios gracefully without crashing.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
BenchmarkDBOpen
Purpose: Benchmarks the db.Open
function, measuring the time it takes to open and close the database in read-only mode. This benchmark is designed to be run against an existing BadgerDB directory.
Parameters:
b *testing.B
: Benchmarking object for the benchmark.
Returns: None
TestBigValues
Purpose: Tests the handling of very large values (1MB) to ensure data integrity and performance. It also tests the functionality in both disk-based and in-memory modes.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
TestCompactionFilePicking
Purpose: Tests the file picking logic during compaction, specifically how tables are chosen for compaction based on their key ranges and versions.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
addToManifest
Purpose: Adds a table to the database manifest.
Parameters:
t *testing.T
: Testing object for the test.db *DB
: The database instance.tab *table.Table
: The table to add.level uint32
: The level of the table.
Returns: None
createTableWithRange
Purpose: Creates a table with keys within a specified range.
Parameters:
t *testing.T
: Testing object for the test.db *DB
: The database instance.start int
: The starting key value.end int
: The ending key value.
Returns:
*table.Table
: The created table.
TestReadSameVlog
Purpose: Tests the scenario where the same value log entry is read multiple times. This ensures that values can be retrieved from the value log repeatedly without errors.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
TestL0GCBug
Purpose: Tests for a specific bug related to data loss when BadgerDB is opened with KeepL0InMemory
and garbage collection is running.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
TestWindowsDataLoss
Purpose: Regression test for a data loss issue specific to Windows. This test simulates a crash scenario to ensure that data written to the value log can be recovered correctly after reopening the database.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
TestDropPrefixWithNoData
Purpose: Tests the DropPrefix
functionality when there is no data associated with the given prefixes. This ensures that the function handles such cases correctly without unnecessary operations.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
TestDropAllDropPrefix
Purpose: Tests the interaction between DropAll
and DropPrefix
functions when they are called concurrently. This verifies that the database can handle concurrent calls to these functions without errors or data corruption.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
TestIsClosed
Purpose: Tests the IsClosed
method of the DB
struct, verifying that it correctly indicates whether the database is closed or not.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
TestMaxVersion
Purpose: Tests the MaxVersion
method, verifying that it returns the correct maximum version number of the database.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
TestTxnReadTs
Purpose: Tests the transaction read timestamp (readTs
) to ensure it's correctly updated and persisted across database restarts.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
TestKeyCount
Purpose: Tests the key count during stream writing, especially with compression enabled, to ensure that the correct number of keys are written and read.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
TestAssertValueLogIsNotWrittenToOnStartup
Purpose: This test asserts that the value log is not written to when the database is opened in read-only mode after being written to in read-write mode.
Parameters:
t *testing.T
: Testing object for the test.
Returns: None
Code Examples
None.