Skip to main content

test_extensions.go

test_extensions.go - Overview

This file defines extensions to the badger package's Options and DB types, specifically for testing purposes. These extensions allow for synchronization and capturing discard statistics during database operations within the testing environment.

Detailed Documentation

Constants

updateDiscardStatsMsg

const updateDiscardStatsMsg = "updateDiscardStats iteration done"
  • Purpose: A string constant used as a message to be sent to the syncChan to indicate the completion of a discard stats update iteration.

endVLogInitMsg

const endVLogInitMsg = "End: vlog.init(db)"
  • Purpose: A string constant used as a message to be sent to the syncChan to indicate the end of the value log initialization process.

Type: testOnlyOptions

type testOnlyOptions struct {
syncChan chan string
}
  • Purpose: Extends the Options type with testing-specific configurations.
  • Fields:
    • syncChan: A channel used for receiving messages related to database activities during testing.
      • Type: chan string
      • Description: Allows test modules to synchronize with specific events within the DB instance.

Type: testOnlyDBExtensions

type testOnlyDBExtensions struct {
syncChan chan string
onCloseDiscardCapture map[uint64]uint64
}
  • Purpose: Extends the DB type with testing-specific fields.
  • Fields:
    • syncChan: A channel for receiving messages related to DB activities.
      • Type: chan string
      • Description: Used for synchronization in tests.
    • onCloseDiscardCapture: A map to capture discard statistics when the DB is closed.
      • Type: map[uint64]uint64
      • Description: Stores the discard stats captured during the Close operation, used for testing.

Function: logToSyncChan

func (db *DB) logToSyncChan(msg string) {
if db.syncChan != nil {
db.syncChan <- msg
}
}
  • Purpose: Sends a message to the DB's syncChan if it is initialized.
  • Parameters:
    • msg: The message to send.
      • Type: string
      • Description: The message that will be sent to the sync channel.
  • Returns: None

Function: captureDiscardStats

func (db *DB) captureDiscardStats() {
if db.onCloseDiscardCapture != nil {
db.vlog.discardStats.Lock()
db.vlog.discardStats.Iterate(func(id, val uint64) {
db.onCloseDiscardCapture[id] = val
})
db.vlog.discardStats.Unlock()
}
}
  • Purpose: Captures the discard statistics from the value log and copies them to the onCloseDiscardCapture map.
  • Parameters: None
  • Returns: None

Getting Started Relevance