You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
3.6 KiB

  1. package db
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. cmn "github.com/tendermint/tmlibs/common"
  9. )
  10. //----------------------------------------
  11. // Helper functions.
  12. func checkValue(t *testing.T, db DB, key []byte, valueWanted []byte) {
  13. valueGot := db.Get(key)
  14. assert.Equal(t, valueWanted, valueGot)
  15. }
  16. func checkValid(t *testing.T, itr Iterator, expected bool) {
  17. valid := itr.Valid()
  18. require.Equal(t, expected, valid)
  19. }
  20. func checkNext(t *testing.T, itr Iterator, expected bool) {
  21. itr.Next()
  22. valid := itr.Valid()
  23. require.Equal(t, expected, valid)
  24. }
  25. func checkNextPanics(t *testing.T, itr Iterator) {
  26. assert.Panics(t, func() { itr.Next() }, "checkNextPanics expected panic but didn't")
  27. }
  28. func checkItem(t *testing.T, itr Iterator, key []byte, value []byte) {
  29. k, v := itr.Key(), itr.Value()
  30. assert.Exactly(t, key, k)
  31. assert.Exactly(t, value, v)
  32. }
  33. func checkInvalid(t *testing.T, itr Iterator) {
  34. checkValid(t, itr, false)
  35. checkKeyPanics(t, itr)
  36. checkValuePanics(t, itr)
  37. checkNextPanics(t, itr)
  38. }
  39. func checkKeyPanics(t *testing.T, itr Iterator) {
  40. assert.Panics(t, func() { itr.Key() }, "checkKeyPanics expected panic but didn't")
  41. }
  42. func checkValuePanics(t *testing.T, itr Iterator) {
  43. assert.Panics(t, func() { itr.Key() }, "checkValuePanics expected panic but didn't")
  44. }
  45. func newTempDB(t *testing.T, backend DBBackendType) (db DB) {
  46. dir, dirname := cmn.Tempdir("db_common_test")
  47. db = NewDB("testdb", backend, dirname)
  48. dir.Close()
  49. return db
  50. }
  51. //----------------------------------------
  52. // mockDB
  53. // NOTE: not actually goroutine safe.
  54. // If you want something goroutine safe, maybe you just want a MemDB.
  55. type mockDB struct {
  56. mtx sync.Mutex
  57. calls map[string]int
  58. }
  59. func newMockDB() *mockDB {
  60. return &mockDB{
  61. calls: make(map[string]int),
  62. }
  63. }
  64. func (mdb *mockDB) Mutex() *sync.Mutex {
  65. return &(mdb.mtx)
  66. }
  67. func (mdb *mockDB) Get([]byte) []byte {
  68. mdb.calls["Get"]++
  69. return nil
  70. }
  71. func (mdb *mockDB) Has([]byte) bool {
  72. mdb.calls["Has"]++
  73. return false
  74. }
  75. func (mdb *mockDB) Set([]byte, []byte) {
  76. mdb.calls["Set"]++
  77. }
  78. func (mdb *mockDB) SetSync([]byte, []byte) {
  79. mdb.calls["SetSync"]++
  80. }
  81. func (mdb *mockDB) SetNoLock([]byte, []byte) {
  82. mdb.calls["SetNoLock"]++
  83. }
  84. func (mdb *mockDB) SetNoLockSync([]byte, []byte) {
  85. mdb.calls["SetNoLockSync"]++
  86. }
  87. func (mdb *mockDB) Delete([]byte) {
  88. mdb.calls["Delete"]++
  89. }
  90. func (mdb *mockDB) DeleteSync([]byte) {
  91. mdb.calls["DeleteSync"]++
  92. }
  93. func (mdb *mockDB) DeleteNoLock([]byte) {
  94. mdb.calls["DeleteNoLock"]++
  95. }
  96. func (mdb *mockDB) DeleteNoLockSync([]byte) {
  97. mdb.calls["DeleteNoLockSync"]++
  98. }
  99. func (mdb *mockDB) Iterator(start, end []byte) Iterator {
  100. mdb.calls["Iterator"]++
  101. return &mockIterator{}
  102. }
  103. func (mdb *mockDB) ReverseIterator(start, end []byte) Iterator {
  104. mdb.calls["ReverseIterator"]++
  105. return &mockIterator{}
  106. }
  107. func (mdb *mockDB) Close() {
  108. mdb.calls["Close"]++
  109. }
  110. func (mdb *mockDB) NewBatch() Batch {
  111. mdb.calls["NewBatch"]++
  112. return &memBatch{db: mdb}
  113. }
  114. func (mdb *mockDB) Print() {
  115. mdb.calls["Print"]++
  116. fmt.Printf("mockDB{%v}", mdb.Stats())
  117. }
  118. func (mdb *mockDB) Stats() map[string]string {
  119. mdb.calls["Stats"]++
  120. res := make(map[string]string)
  121. for key, count := range mdb.calls {
  122. res[key] = fmt.Sprintf("%d", count)
  123. }
  124. return res
  125. }
  126. //----------------------------------------
  127. // mockIterator
  128. type mockIterator struct{}
  129. func (mockIterator) Domain() (start []byte, end []byte) {
  130. return nil, nil
  131. }
  132. func (mockIterator) Valid() bool {
  133. return false
  134. }
  135. func (mockIterator) Next() {
  136. }
  137. func (mockIterator) Key() []byte {
  138. return nil
  139. }
  140. func (mockIterator) Value() []byte {
  141. return nil
  142. }
  143. func (mockIterator) Close() {
  144. }