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.

215 lines
7.0 KiB

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