watermark_edge_test.go
watermark_edge_test.go - Overview
This file contains a test case (TestWaterMarkEdgeCase
) that validates the behavior of the BadgerDB under concurrent transactions, specifically focusing on watermark handling and conflict detection. It simulates multiple concurrent transactions attempting to read and write the same keys, and verifies that conflicts are correctly identified.
Detailed Documentation
TestWaterMarkEdgeCase
Purpose: Tests the watermark edge case in concurrent transactions. It spawns multiple goroutines, each performing a series of read and write operations within transactions. It then verifies that any conflicts encountered are correctly identified as ErrConflict
.
Parameters:
t
(*testing.T): The testing object, used for reporting test failures.
Returns: None
doWork
Purpose: Simulates a unit of work performed by a transaction. It creates two transactions, performs a series of get and set operations on keys, and commits the transactions. It also introduces random delays to simulate real-world conditions.
Parameters:
db
(*DB): The BadgerDB instance to use.i
(int): An identifier for the goroutine, used in key generation.
Returns:
error
: Returns an error if any of the transaction operations fail, or if a conflict is not detected when expected. Returnsnil
on success.
generateRandomBytes
Purpose: Generates a slice of random bytes.
Parameters: None
Returns:
[]byte
: A slice of 20 random bytes.
getValue
Purpose: Retrieves a value from the transaction by key, panicking if an error other than ErrKeyNotFound
is encountered.
Parameters:
txn
(*Txn): The transaction to use.key
(string): The key to retrieve.
Returns: None
setValue
Purpose: Sets a key-value pair in the transaction, panicking if an error occurs.
Parameters:
txn
(*Txn): The transaction to use.key
(string): The key to set.value
(string): The value to set.
Returns: None
delay
Purpose: Introduces a small random delay to simulate real-world conditions and increase the likelihood of conflicts.
Parameters: None
Returns: None