test_extensions.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. package badger
  6. // Important: Do NOT import the "testing" package, as otherwise, that
  7. // will pull in imports into the production class that we do not want.
  8. // TODO: Consider using this with specific compilation tags so that it only
  9. // shows up when performing testing (e.g., specify build tag=unit).
  10. // We are not yet ready to do that, as it may impact customer usage as
  11. // well as requiring us to update the CI build flags. Moreover, the
  12. // current model does not actually incur any significant cost.
  13. // If we do this, we will also want to introduce a parallel file that
  14. // overrides some of these structs and functions with empty contents.
  15. // String constants for messages to be pushed to syncChan.
  16. const (
  17. updateDiscardStatsMsg = "updateDiscardStats iteration done"
  18. endVLogInitMsg = "End: vlog.init(db)"
  19. )
  20. // testOnlyOptions specifies an extension to the type Options that we want to
  21. // use only in the context of testing.
  22. type testOnlyOptions struct {
  23. // syncChan is used to listen for specific messages related to activities
  24. // that can occur in a DB instance. Currently, this is only used in
  25. // testing activities.
  26. syncChan chan string
  27. }
  28. // testOnlyDBExtensions specifies an extension to the type DB that we want to
  29. // use only in the context of testing.
  30. type testOnlyDBExtensions struct {
  31. syncChan chan string
  32. // onCloseDiscardCapture will be populated by a DB instance during the
  33. // process of performing the Close operation. Currently, we only consider
  34. // using this during testing.
  35. onCloseDiscardCapture map[uint64]uint64
  36. }
  37. // logToSyncChan sends a message to the DB's syncChan. Note that we expect
  38. // that the DB never closes this channel; the responsibility for
  39. // allocating and closing the channel belongs to the test module.
  40. // if db.syncChan is nil or has never been initialized, ths will be
  41. // silently ignored.
  42. func (db *DB) logToSyncChan(msg string) {
  43. if db.syncChan != nil {
  44. db.syncChan <- msg
  45. }
  46. }
  47. // captureDiscardStats will copy the contents of the discardStats file
  48. // maintained by vlog to the onCloseDiscardCapture map specified by
  49. // db.opt. Of couse, if db.opt.onCloseDiscardCapture is nil (as expected
  50. // for a production system as opposed to a test system), this is a no-op.
  51. func (db *DB) captureDiscardStats() {
  52. if db.onCloseDiscardCapture != nil {
  53. db.vlog.discardStats.Lock()
  54. db.vlog.discardStats.Iterate(func(id, val uint64) {
  55. db.onCloseDiscardCapture[id] = val
  56. })
  57. db.vlog.discardStats.Unlock()
  58. }
  59. }