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.

190 lines
3.8 KiB

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