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.

206 lines
4.9 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 TestDBBatchWrite1(t *testing.T) {
  98. mdb := newMockDB()
  99. ddb := NewDebugDB(t.Name(), mdb)
  100. batch := ddb.NewBatch()
  101. batch.Set(bz("1"), bz("1"))
  102. batch.Set(bz("2"), bz("2"))
  103. batch.Delete(bz("3"))
  104. batch.Set(bz("4"), bz("4"))
  105. batch.Write()
  106. assert.Equal(t, 0, mdb.calls["Set"])
  107. assert.Equal(t, 0, mdb.calls["SetSync"])
  108. assert.Equal(t, 3, mdb.calls["SetNoLock"])
  109. assert.Equal(t, 0, mdb.calls["SetNoLockSync"])
  110. assert.Equal(t, 0, mdb.calls["Delete"])
  111. assert.Equal(t, 0, mdb.calls["DeleteSync"])
  112. assert.Equal(t, 1, mdb.calls["DeleteNoLock"])
  113. assert.Equal(t, 0, mdb.calls["DeleteNoLockSync"])
  114. }
  115. func TestDBBatchWrite2(t *testing.T) {
  116. mdb := newMockDB()
  117. ddb := NewDebugDB(t.Name(), mdb)
  118. batch := ddb.NewBatch()
  119. batch.Set(bz("1"), bz("1"))
  120. batch.Set(bz("2"), bz("2"))
  121. batch.Set(bz("4"), bz("4"))
  122. batch.Delete(bz("3"))
  123. batch.Write()
  124. assert.Equal(t, 0, mdb.calls["Set"])
  125. assert.Equal(t, 0, mdb.calls["SetSync"])
  126. assert.Equal(t, 3, mdb.calls["SetNoLock"])
  127. assert.Equal(t, 0, mdb.calls["SetNoLockSync"])
  128. assert.Equal(t, 0, mdb.calls["Delete"])
  129. assert.Equal(t, 0, mdb.calls["DeleteSync"])
  130. assert.Equal(t, 1, mdb.calls["DeleteNoLock"])
  131. assert.Equal(t, 0, mdb.calls["DeleteNoLockSync"])
  132. }
  133. func TestDBBatchWriteSync1(t *testing.T) {
  134. mdb := newMockDB()
  135. ddb := NewDebugDB(t.Name(), mdb)
  136. batch := ddb.NewBatch()
  137. batch.Set(bz("1"), bz("1"))
  138. batch.Set(bz("2"), bz("2"))
  139. batch.Delete(bz("3"))
  140. batch.Set(bz("4"), bz("4"))
  141. batch.WriteSync()
  142. assert.Equal(t, 0, mdb.calls["Set"])
  143. assert.Equal(t, 0, mdb.calls["SetSync"])
  144. assert.Equal(t, 2, mdb.calls["SetNoLock"])
  145. assert.Equal(t, 1, mdb.calls["SetNoLockSync"])
  146. assert.Equal(t, 0, mdb.calls["Delete"])
  147. assert.Equal(t, 0, mdb.calls["DeleteSync"])
  148. assert.Equal(t, 1, mdb.calls["DeleteNoLock"])
  149. assert.Equal(t, 0, mdb.calls["DeleteNoLockSync"])
  150. }
  151. func TestDBBatchWriteSync2(t *testing.T) {
  152. mdb := newMockDB()
  153. ddb := NewDebugDB(t.Name(), mdb)
  154. batch := ddb.NewBatch()
  155. batch.Set(bz("1"), bz("1"))
  156. batch.Set(bz("2"), bz("2"))
  157. batch.Set(bz("4"), bz("4"))
  158. batch.Delete(bz("3"))
  159. batch.WriteSync()
  160. assert.Equal(t, 0, mdb.calls["Set"])
  161. assert.Equal(t, 0, mdb.calls["SetSync"])
  162. assert.Equal(t, 3, mdb.calls["SetNoLock"])
  163. assert.Equal(t, 0, mdb.calls["SetNoLockSync"])
  164. assert.Equal(t, 0, mdb.calls["Delete"])
  165. assert.Equal(t, 0, mdb.calls["DeleteSync"])
  166. assert.Equal(t, 0, mdb.calls["DeleteNoLock"])
  167. assert.Equal(t, 1, mdb.calls["DeleteNoLockSync"])
  168. }