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.

194 lines
4.7 KiB

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