github.com/ethersphere/bee/v2@v2.2.0/pkg/puller/intervalstore/store_test.go (about) 1 // Copyright 2018 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 17 package intervalstore 18 19 import ( 20 "errors" 21 "testing" 22 23 "github.com/ethersphere/bee/v2/pkg/log" 24 "github.com/ethersphere/bee/v2/pkg/statestore/leveldb" 25 "github.com/ethersphere/bee/v2/pkg/statestore/mock" 26 "github.com/ethersphere/bee/v2/pkg/storage" 27 "github.com/ethersphere/bee/v2/pkg/util/testutil" 28 ) 29 30 // TestInmemoryStore tests basic functionality of InmemoryStore. 31 func TestInmemoryStore(t *testing.T) { 32 t.Parallel() 33 34 testStore(t, mock.NewStateStore()) 35 } 36 37 // TestDBStore tests basic functionality of DBStore. 38 func TestDBStore(t *testing.T) { 39 t.Parallel() 40 41 dir := t.TempDir() 42 43 store, err := leveldb.NewStateStore(dir, log.Noop) 44 if err != nil { 45 t.Fatal(err) 46 } 47 testutil.CleanupCloser(t, store) 48 49 testStore(t, store) 50 } 51 52 // testStore is a helper function to test various Store implementations. 53 func testStore(t *testing.T, s storage.StateStorer) { 54 t.Helper() 55 56 key1 := "key1" 57 i1 := NewIntervals(0) 58 i1.Add(10, 20) 59 if err := s.Put(key1, i1); err != nil { 60 t.Fatal(err) 61 } 62 i := &Intervals{} 63 err := s.Get(key1, i) 64 if err != nil { 65 t.Fatal(err) 66 } 67 if i.String() != i1.String() { 68 t.Errorf("expected interval %s, got %s", i1, i) 69 } 70 71 key2 := "key2" 72 i2 := NewIntervals(0) 73 i2.Add(10, 20) 74 if err := s.Put(key2, i2); err != nil { 75 t.Fatal(err) 76 } 77 err = s.Get(key2, i) 78 if err != nil { 79 t.Fatal(err) 80 } 81 if i.String() != i2.String() { 82 t.Errorf("expected interval %s, got %s", i2, i) 83 } 84 85 if err := s.Delete(key1); err != nil { 86 t.Fatal(err) 87 } 88 if err := s.Get(key1, i); !errors.Is(err, storage.ErrNotFound) { 89 t.Errorf("expected error %v, got %s", storage.ErrNotFound, err) 90 } 91 if err := s.Get(key2, i); err != nil { 92 t.Errorf("expected error %v, got %s", nil, err) 93 } 94 95 if err := s.Delete(key2); err != nil { 96 t.Fatal(err) 97 } 98 if err := s.Get(key2, i); !errors.Is(err, storage.ErrNotFound) { 99 t.Errorf("expected error %v, got %s", storage.ErrNotFound, err) 100 } 101 }