test_extensions.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2023 Dgraph Labs, Inc. and Contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package badger
  17. // Important: Do NOT import the "testing" package, as otherwise, that
  18. // will pull in imports into the production class that we do not want.
  19. // TODO: Consider using this with specific compilation tags so that it only
  20. // shows up when performing testing (e.g., specify build tag=unit).
  21. // We are not yet ready to do that, as it may impact customer usage as
  22. // well as requiring us to update the CI build flags. Moreover, the
  23. // current model does not actually incur any significant cost.
  24. // If we do this, we will also want to introduce a parallel file that
  25. // overrides some of these structs and functions with empty contents.
  26. // String constants for messages to be pushed to syncChan.
  27. const (
  28. updateDiscardStatsMsg = "updateDiscardStats iteration done"
  29. endVLogInitMsg = "End: vlog.init(db)"
  30. )
  31. // testOnlyOptions specifies an extension to the type Options that we want to
  32. // use only in the context of testing.
  33. type testOnlyOptions struct {
  34. // syncChan is used to listen for specific messages related to activities
  35. // that can occur in a DB instance. Currently, this is only used in
  36. // testing activities.
  37. syncChan chan string
  38. }
  39. // testOnlyDBExtensions specifies an extension to the type DB that we want to
  40. // use only in the context of testing.
  41. type testOnlyDBExtensions struct {
  42. syncChan chan string
  43. // onCloseDiscardCapture will be populated by a DB instance during the
  44. // process of performing the Close operation. Currently, we only consider
  45. // using this during testing.
  46. onCloseDiscardCapture map[uint64]uint64
  47. }
  48. // logToSyncChan sends a message to the DB's syncChan. Note that we expect
  49. // that the DB never closes this channel; the responsibility for
  50. // allocating and closing the channel belongs to the test module.
  51. // if db.syncChan is nil or has never been initialized, ths will be
  52. // silently ignored.
  53. func (db *DB) logToSyncChan(msg string) {
  54. if db.syncChan != nil {
  55. db.syncChan <- msg
  56. }
  57. }
  58. // captureDiscardStats will copy the contents of the discardStats file
  59. // maintained by vlog to the onCloseDiscardCapture map specified by
  60. // db.opt. Of couse, if db.opt.onCloseDiscardCapture is nil (as expected
  61. // for a production system as opposed to a test system), this is a no-op.
  62. func (db *DB) captureDiscardStats() {
  63. if db.onCloseDiscardCapture != nil {
  64. db.vlog.discardStats.Lock()
  65. db.vlog.discardStats.Iterate(func(id, val uint64) {
  66. db.onCloseDiscardCapture[id] = val
  67. })
  68. db.vlog.discardStats.Unlock()
  69. }
  70. }