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