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.

216 lines
7.1 KiB

7 years ago
7 years ago
7 years ago
  1. package db
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. cmn "github.com/tendermint/tendermint/libs/common"
  11. )
  12. func cleanupDBDir(dir, name string) {
  13. os.RemoveAll(filepath.Join(dir, name) + ".db")
  14. }
  15. func testBackendGetSetDelete(t *testing.T, backend DBBackendType) {
  16. // Default
  17. dirname, err := ioutil.TempDir("", fmt.Sprintf("test_backend_%s_", backend))
  18. require.Nil(t, err)
  19. db := NewDB("testdb", backend, dirname)
  20. // A nonexistent key should return nil, even if the key is empty
  21. require.Nil(t, db.Get([]byte("")))
  22. // A nonexistent key should return nil, even if the key is nil
  23. require.Nil(t, db.Get(nil))
  24. // A nonexistent key should return nil.
  25. key := []byte("abc")
  26. require.Nil(t, db.Get(key))
  27. // Set empty value.
  28. db.Set(key, []byte(""))
  29. require.NotNil(t, db.Get(key))
  30. require.Empty(t, db.Get(key))
  31. // Set nil value.
  32. db.Set(key, nil)
  33. require.NotNil(t, db.Get(key))
  34. require.Empty(t, db.Get(key))
  35. // Delete.
  36. db.Delete(key)
  37. require.Nil(t, db.Get(key))
  38. }
  39. func TestBackendsGetSetDelete(t *testing.T) {
  40. for dbType := range backends {
  41. testBackendGetSetDelete(t, dbType)
  42. }
  43. }
  44. func withDB(t *testing.T, creator dbCreator, fn func(DB)) {
  45. name := cmn.Fmt("test_%x", cmn.RandStr(12))
  46. db, err := creator(name, "")
  47. defer cleanupDBDir("", name)
  48. assert.Nil(t, err)
  49. fn(db)
  50. db.Close()
  51. }
  52. func TestBackendsNilKeys(t *testing.T) {
  53. // Test all backends.
  54. for dbType, creator := range backends {
  55. withDB(t, creator, func(db DB) {
  56. t.Run(fmt.Sprintf("Testing %s", dbType), func(t *testing.T) {
  57. // Nil keys are treated as the empty key for most operations.
  58. expect := func(key, value []byte) {
  59. if len(key) == 0 { // nil or empty
  60. assert.Equal(t, db.Get(nil), db.Get([]byte("")))
  61. assert.Equal(t, db.Has(nil), db.Has([]byte("")))
  62. }
  63. assert.Equal(t, db.Get(key), value)
  64. assert.Equal(t, db.Has(key), value != nil)
  65. }
  66. // Not set
  67. expect(nil, nil)
  68. // Set nil value
  69. db.Set(nil, nil)
  70. expect(nil, []byte(""))
  71. // Set empty value
  72. db.Set(nil, []byte(""))
  73. expect(nil, []byte(""))
  74. // Set nil, Delete nil
  75. db.Set(nil, []byte("abc"))
  76. expect(nil, []byte("abc"))
  77. db.Delete(nil)
  78. expect(nil, nil)
  79. // Set nil, Delete empty
  80. db.Set(nil, []byte("abc"))
  81. expect(nil, []byte("abc"))
  82. db.Delete([]byte(""))
  83. expect(nil, nil)
  84. // Set empty, Delete nil
  85. db.Set([]byte(""), []byte("abc"))
  86. expect(nil, []byte("abc"))
  87. db.Delete(nil)
  88. expect(nil, nil)
  89. // Set empty, Delete empty
  90. db.Set([]byte(""), []byte("abc"))
  91. expect(nil, []byte("abc"))
  92. db.Delete([]byte(""))
  93. expect(nil, nil)
  94. // SetSync nil, DeleteSync nil
  95. db.SetSync(nil, []byte("abc"))
  96. expect(nil, []byte("abc"))
  97. db.DeleteSync(nil)
  98. expect(nil, nil)
  99. // SetSync nil, DeleteSync empty
  100. db.SetSync(nil, []byte("abc"))
  101. expect(nil, []byte("abc"))
  102. db.DeleteSync([]byte(""))
  103. expect(nil, nil)
  104. // SetSync empty, DeleteSync nil
  105. db.SetSync([]byte(""), []byte("abc"))
  106. expect(nil, []byte("abc"))
  107. db.DeleteSync(nil)
  108. expect(nil, nil)
  109. // SetSync empty, DeleteSync empty
  110. db.SetSync([]byte(""), []byte("abc"))
  111. expect(nil, []byte("abc"))
  112. db.DeleteSync([]byte(""))
  113. expect(nil, nil)
  114. })
  115. })
  116. }
  117. }
  118. func TestGoLevelDBBackend(t *testing.T) {
  119. name := cmn.Fmt("test_%x", cmn.RandStr(12))
  120. db := NewDB(name, GoLevelDBBackend, "")
  121. defer cleanupDBDir("", name)
  122. _, ok := db.(*GoLevelDB)
  123. assert.True(t, ok)
  124. }
  125. func TestDBIterator(t *testing.T) {
  126. for dbType := range backends {
  127. t.Run(fmt.Sprintf("%v", dbType), func(t *testing.T) {
  128. testDBIterator(t, dbType)
  129. })
  130. }
  131. }
  132. func testDBIterator(t *testing.T, backend DBBackendType) {
  133. name := cmn.Fmt("test_%x", cmn.RandStr(12))
  134. db := NewDB(name, backend, "")
  135. defer cleanupDBDir("", name)
  136. for i := 0; i < 10; i++ {
  137. if i != 6 { // but skip 6.
  138. db.Set(int642Bytes(int64(i)), nil)
  139. }
  140. }
  141. verifyIterator(t, db.Iterator(nil, nil), []int64{0, 1, 2, 3, 4, 5, 7, 8, 9}, "forward iterator")
  142. verifyIterator(t, db.ReverseIterator(nil, nil), []int64{9, 8, 7, 5, 4, 3, 2, 1, 0}, "reverse iterator")
  143. verifyIterator(t, db.Iterator(nil, int642Bytes(0)), []int64(nil), "forward iterator to 0")
  144. verifyIterator(t, db.ReverseIterator(nil, int642Bytes(10)), []int64(nil), "reverse iterator 10")
  145. verifyIterator(t, db.Iterator(int642Bytes(0), nil), []int64{0, 1, 2, 3, 4, 5, 7, 8, 9}, "forward iterator from 0")
  146. verifyIterator(t, db.Iterator(int642Bytes(1), nil), []int64{1, 2, 3, 4, 5, 7, 8, 9}, "forward iterator from 1")
  147. verifyIterator(t, db.ReverseIterator(int642Bytes(10), nil), []int64{9, 8, 7, 5, 4, 3, 2, 1, 0}, "reverse iterator from 10")
  148. verifyIterator(t, db.ReverseIterator(int642Bytes(9), nil), []int64{9, 8, 7, 5, 4, 3, 2, 1, 0}, "reverse iterator from 9")
  149. verifyIterator(t, db.ReverseIterator(int642Bytes(8), nil), []int64{8, 7, 5, 4, 3, 2, 1, 0}, "reverse iterator from 8")
  150. verifyIterator(t, db.Iterator(int642Bytes(5), int642Bytes(6)), []int64{5}, "forward iterator from 5 to 6")
  151. verifyIterator(t, db.Iterator(int642Bytes(5), int642Bytes(7)), []int64{5}, "forward iterator from 5 to 7")
  152. verifyIterator(t, db.Iterator(int642Bytes(5), int642Bytes(8)), []int64{5, 7}, "forward iterator from 5 to 8")
  153. verifyIterator(t, db.Iterator(int642Bytes(6), int642Bytes(7)), []int64(nil), "forward iterator from 6 to 7")
  154. verifyIterator(t, db.Iterator(int642Bytes(6), int642Bytes(8)), []int64{7}, "forward iterator from 6 to 8")
  155. verifyIterator(t, db.Iterator(int642Bytes(7), int642Bytes(8)), []int64{7}, "forward iterator from 7 to 8")
  156. verifyIterator(t, db.ReverseIterator(int642Bytes(5), int642Bytes(4)), []int64{5}, "reverse iterator from 5 to 4")
  157. verifyIterator(t, db.ReverseIterator(int642Bytes(6), int642Bytes(4)), []int64{5}, "reverse iterator from 6 to 4")
  158. verifyIterator(t, db.ReverseIterator(int642Bytes(7), int642Bytes(4)), []int64{7, 5}, "reverse iterator from 7 to 4")
  159. verifyIterator(t, db.ReverseIterator(int642Bytes(6), int642Bytes(5)), []int64(nil), "reverse iterator from 6 to 5")
  160. verifyIterator(t, db.ReverseIterator(int642Bytes(7), int642Bytes(5)), []int64{7}, "reverse iterator from 7 to 5")
  161. verifyIterator(t, db.ReverseIterator(int642Bytes(7), int642Bytes(6)), []int64{7}, "reverse iterator from 7 to 6")
  162. verifyIterator(t, db.Iterator(int642Bytes(0), int642Bytes(1)), []int64{0}, "forward iterator from 0 to 1")
  163. verifyIterator(t, db.ReverseIterator(int642Bytes(9), int642Bytes(8)), []int64{9}, "reverse iterator from 9 to 8")
  164. verifyIterator(t, db.Iterator(int642Bytes(2), int642Bytes(4)), []int64{2, 3}, "forward iterator from 2 to 4")
  165. verifyIterator(t, db.Iterator(int642Bytes(4), int642Bytes(2)), []int64(nil), "forward iterator from 4 to 2")
  166. verifyIterator(t, db.ReverseIterator(int642Bytes(4), int642Bytes(2)), []int64{4, 3}, "reverse iterator from 4 to 2")
  167. verifyIterator(t, db.ReverseIterator(int642Bytes(2), int642Bytes(4)), []int64(nil), "reverse iterator from 2 to 4")
  168. }
  169. func verifyIterator(t *testing.T, itr Iterator, expected []int64, msg string) {
  170. var list []int64
  171. for itr.Valid() {
  172. list = append(list, bytes2Int64(itr.Key()))
  173. itr.Next()
  174. }
  175. assert.Equal(t, expected, list, msg)
  176. }