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.

195 lines
4.2 KiB

  1. package db
  2. import (
  3. "fmt"
  4. "os"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestDBIteratorSingleKey(t *testing.T) {
  9. for backend := range backends {
  10. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  11. db, dir := newTempDB(t, backend)
  12. defer os.RemoveAll(dir)
  13. db.SetSync(bz("1"), bz("value_1"))
  14. itr := db.Iterator(nil, nil)
  15. checkValid(t, itr, true)
  16. checkNext(t, itr, false)
  17. checkValid(t, itr, false)
  18. checkNextPanics(t, itr)
  19. // Once invalid...
  20. checkInvalid(t, itr)
  21. })
  22. }
  23. }
  24. func TestDBIteratorTwoKeys(t *testing.T) {
  25. for backend := range backends {
  26. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  27. db, dir := newTempDB(t, backend)
  28. defer os.RemoveAll(dir)
  29. db.SetSync(bz("1"), bz("value_1"))
  30. db.SetSync(bz("2"), bz("value_1"))
  31. { // Fail by calling Next too much
  32. itr := db.Iterator(nil, nil)
  33. checkValid(t, itr, true)
  34. checkNext(t, itr, true)
  35. checkValid(t, itr, true)
  36. checkNext(t, itr, false)
  37. checkValid(t, itr, false)
  38. checkNextPanics(t, itr)
  39. // Once invalid...
  40. checkInvalid(t, itr)
  41. }
  42. })
  43. }
  44. }
  45. func TestDBIteratorMany(t *testing.T) {
  46. for backend := range backends {
  47. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  48. db, dir := newTempDB(t, backend)
  49. defer os.RemoveAll(dir)
  50. keys := make([][]byte, 100)
  51. for i := 0; i < 100; i++ {
  52. keys[i] = []byte{byte(i)}
  53. }
  54. value := []byte{5}
  55. for _, k := range keys {
  56. db.Set(k, value)
  57. }
  58. itr := db.Iterator(nil, nil)
  59. defer itr.Close()
  60. for ; itr.Valid(); itr.Next() {
  61. assert.Equal(t, db.Get(itr.Key()), itr.Value())
  62. }
  63. })
  64. }
  65. }
  66. func TestDBIteratorEmpty(t *testing.T) {
  67. for backend := range backends {
  68. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  69. db, dir := newTempDB(t, backend)
  70. defer os.RemoveAll(dir)
  71. itr := db.Iterator(nil, nil)
  72. checkInvalid(t, itr)
  73. })
  74. }
  75. }
  76. func TestDBIteratorEmptyBeginAfter(t *testing.T) {
  77. for backend := range backends {
  78. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  79. db, dir := newTempDB(t, backend)
  80. defer os.RemoveAll(dir)
  81. itr := db.Iterator(bz("1"), nil)
  82. checkInvalid(t, itr)
  83. })
  84. }
  85. }
  86. func TestDBIteratorNonemptyBeginAfter(t *testing.T) {
  87. for backend := range backends {
  88. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  89. db, dir := newTempDB(t, backend)
  90. defer os.RemoveAll(dir)
  91. db.SetSync(bz("1"), bz("value_1"))
  92. itr := db.Iterator(bz("2"), nil)
  93. checkInvalid(t, itr)
  94. })
  95. }
  96. }
  97. func TestDBBatchWrite(t *testing.T) {
  98. testCases := []struct {
  99. modify func(batch Batch)
  100. calls map[string]int
  101. }{
  102. 0: {
  103. func(batch Batch) {
  104. batch.Set(bz("1"), bz("1"))
  105. batch.Set(bz("2"), bz("2"))
  106. batch.Delete(bz("3"))
  107. batch.Set(bz("4"), bz("4"))
  108. batch.Write()
  109. },
  110. map[string]int{
  111. "Set": 0, "SetSync": 0, "SetNoLock": 3, "SetNoLockSync": 0,
  112. "Delete": 0, "DeleteSync": 0, "DeleteNoLock": 1, "DeleteNoLockSync": 0,
  113. },
  114. },
  115. 1: {
  116. func(batch Batch) {
  117. batch.Set(bz("1"), bz("1"))
  118. batch.Set(bz("2"), bz("2"))
  119. batch.Set(bz("4"), bz("4"))
  120. batch.Delete(bz("3"))
  121. batch.Write()
  122. },
  123. map[string]int{
  124. "Set": 0, "SetSync": 0, "SetNoLock": 3, "SetNoLockSync": 0,
  125. "Delete": 0, "DeleteSync": 0, "DeleteNoLock": 1, "DeleteNoLockSync": 0,
  126. },
  127. },
  128. 2: {
  129. func(batch Batch) {
  130. batch.Set(bz("1"), bz("1"))
  131. batch.Set(bz("2"), bz("2"))
  132. batch.Delete(bz("3"))
  133. batch.Set(bz("4"), bz("4"))
  134. batch.WriteSync()
  135. },
  136. map[string]int{
  137. "Set": 0, "SetSync": 0, "SetNoLock": 2, "SetNoLockSync": 1,
  138. "Delete": 0, "DeleteSync": 0, "DeleteNoLock": 1, "DeleteNoLockSync": 0,
  139. },
  140. },
  141. 3: {
  142. func(batch Batch) {
  143. batch.Set(bz("1"), bz("1"))
  144. batch.Set(bz("2"), bz("2"))
  145. batch.Set(bz("4"), bz("4"))
  146. batch.Delete(bz("3"))
  147. batch.WriteSync()
  148. },
  149. map[string]int{
  150. "Set": 0, "SetSync": 0, "SetNoLock": 3, "SetNoLockSync": 0,
  151. "Delete": 0, "DeleteSync": 0, "DeleteNoLock": 0, "DeleteNoLockSync": 1,
  152. },
  153. },
  154. }
  155. for i, tc := range testCases {
  156. mdb := newMockDB()
  157. ddb := NewDebugDB(t.Name(), mdb)
  158. batch := ddb.NewBatch()
  159. tc.modify(batch)
  160. for call, exp := range tc.calls {
  161. got := mdb.calls[call]
  162. assert.Equal(t, exp, got, "#%v - key: %s", i, call)
  163. }
  164. }
  165. }