dbstore_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package storage
  17. import (
  18. "bytes"
  19. "io/ioutil"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. )
  23. func initDbStore(t *testing.T) *DbStore {
  24. dir, err := ioutil.TempDir("", "bzz-storage-test")
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. m, err := NewDbStore(dir, MakeHashFunc(SHA3Hash), defaultDbCapacity, defaultRadius)
  29. if err != nil {
  30. t.Fatal("can't create store:", err)
  31. }
  32. return m
  33. }
  34. func testDbStore(l int64, branches int64, t *testing.T) {
  35. m := initDbStore(t)
  36. defer m.Close()
  37. testStore(m, l, branches, t)
  38. }
  39. func TestDbStore128_0x1000000(t *testing.T) {
  40. testDbStore(0x1000000, 128, t)
  41. }
  42. func TestDbStore128_10000_(t *testing.T) {
  43. testDbStore(10000, 128, t)
  44. }
  45. func TestDbStore128_1000_(t *testing.T) {
  46. testDbStore(1000, 128, t)
  47. }
  48. func TestDbStore128_100_(t *testing.T) {
  49. testDbStore(100, 128, t)
  50. }
  51. func TestDbStore2_100_(t *testing.T) {
  52. testDbStore(100, 2, t)
  53. }
  54. func TestDbStoreNotFound(t *testing.T) {
  55. m := initDbStore(t)
  56. defer m.Close()
  57. _, err := m.Get(ZeroKey)
  58. if err != notFound {
  59. t.Errorf("Expected notFound, got %v", err)
  60. }
  61. }
  62. func TestDbStoreSyncIterator(t *testing.T) {
  63. m := initDbStore(t)
  64. defer m.Close()
  65. keys := []Key{
  66. Key(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")),
  67. Key(common.Hex2Bytes("4000000000000000000000000000000000000000000000000000000000000000")),
  68. Key(common.Hex2Bytes("5000000000000000000000000000000000000000000000000000000000000000")),
  69. Key(common.Hex2Bytes("3000000000000000000000000000000000000000000000000000000000000000")),
  70. Key(common.Hex2Bytes("2000000000000000000000000000000000000000000000000000000000000000")),
  71. Key(common.Hex2Bytes("1000000000000000000000000000000000000000000000000000000000000000")),
  72. }
  73. for _, key := range keys {
  74. m.Put(NewChunk(key, nil))
  75. }
  76. it, err := m.NewSyncIterator(DbSyncState{
  77. Start: Key(common.Hex2Bytes("1000000000000000000000000000000000000000000000000000000000000000")),
  78. Stop: Key(common.Hex2Bytes("4000000000000000000000000000000000000000000000000000000000000000")),
  79. First: 2,
  80. Last: 4,
  81. })
  82. if err != nil {
  83. t.Fatalf("unexpected error creating NewSyncIterator")
  84. }
  85. var chunk Key
  86. var res []Key
  87. for {
  88. chunk = it.Next()
  89. if chunk == nil {
  90. break
  91. }
  92. res = append(res, chunk)
  93. }
  94. if len(res) != 1 {
  95. t.Fatalf("Expected 1 chunk, got %v: %v", len(res), res)
  96. }
  97. if !bytes.Equal(res[0][:], keys[3]) {
  98. t.Fatalf("Expected %v chunk, got %v", keys[3], res[0])
  99. }
  100. if err != nil {
  101. t.Fatalf("unexpected error creating NewSyncIterator")
  102. }
  103. it, err = m.NewSyncIterator(DbSyncState{
  104. Start: Key(common.Hex2Bytes("1000000000000000000000000000000000000000000000000000000000000000")),
  105. Stop: Key(common.Hex2Bytes("5000000000000000000000000000000000000000000000000000000000000000")),
  106. First: 2,
  107. Last: 4,
  108. })
  109. res = nil
  110. for {
  111. chunk = it.Next()
  112. if chunk == nil {
  113. break
  114. }
  115. res = append(res, chunk)
  116. }
  117. if len(res) != 2 {
  118. t.Fatalf("Expected 2 chunk, got %v: %v", len(res), res)
  119. }
  120. if !bytes.Equal(res[0][:], keys[3]) {
  121. t.Fatalf("Expected %v chunk, got %v", keys[3], res[0])
  122. }
  123. if !bytes.Equal(res[1][:], keys[2]) {
  124. t.Fatalf("Expected %v chunk, got %v", keys[2], res[1])
  125. }
  126. if err != nil {
  127. t.Fatalf("unexpected error creating NewSyncIterator")
  128. }
  129. it, _ = m.NewSyncIterator(DbSyncState{
  130. Start: Key(common.Hex2Bytes("1000000000000000000000000000000000000000000000000000000000000000")),
  131. Stop: Key(common.Hex2Bytes("4000000000000000000000000000000000000000000000000000000000000000")),
  132. First: 2,
  133. Last: 5,
  134. })
  135. res = nil
  136. for {
  137. chunk = it.Next()
  138. if chunk == nil {
  139. break
  140. }
  141. res = append(res, chunk)
  142. }
  143. if len(res) != 2 {
  144. t.Fatalf("Expected 2 chunk, got %v", len(res))
  145. }
  146. if !bytes.Equal(res[0][:], keys[4]) {
  147. t.Fatalf("Expected %v chunk, got %v", keys[4], res[0])
  148. }
  149. if !bytes.Equal(res[1][:], keys[3]) {
  150. t.Fatalf("Expected %v chunk, got %v", keys[3], res[1])
  151. }
  152. it, _ = m.NewSyncIterator(DbSyncState{
  153. Start: Key(common.Hex2Bytes("2000000000000000000000000000000000000000000000000000000000000000")),
  154. Stop: Key(common.Hex2Bytes("4000000000000000000000000000000000000000000000000000000000000000")),
  155. First: 2,
  156. Last: 5,
  157. })
  158. res = nil
  159. for {
  160. chunk = it.Next()
  161. if chunk == nil {
  162. break
  163. }
  164. res = append(res, chunk)
  165. }
  166. if len(res) != 1 {
  167. t.Fatalf("Expected 1 chunk, got %v", len(res))
  168. }
  169. if !bytes.Equal(res[0][:], keys[3]) {
  170. t.Fatalf("Expected %v chunk, got %v", keys[3], res[0])
  171. }
  172. }