github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/chain/swarm/network/stream/intervals/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 intervals 18 19 import ( 20 "testing" 21 22 "github.com/ethereum/go-ethereum/swarm/state" 23 ) 24 25 // TestInmemoryStore tests basic functionality of InmemoryStore. 26 func TestInmemoryStore(t *testing.T) { 27 testStore(t, state.NewInmemoryStore()) 28 } 29 30 // testStore is a helper function to test various Store implementations. 31 func testStore(t *testing.T, s state.Store) { 32 key1 := "key1" 33 i1 := NewIntervals(0) 34 i1.Add(10, 20) 35 if err := s.Put(key1, i1); err != nil { 36 t.Fatal(err) 37 } 38 i := &Intervals{} 39 err := s.Get(key1, i) 40 if err != nil { 41 t.Fatal(err) 42 } 43 if i.String() != i1.String() { 44 t.Errorf("expected interval %s, got %s", i1, i) 45 } 46 47 key2 := "key2" 48 i2 := NewIntervals(0) 49 i2.Add(10, 20) 50 if err := s.Put(key2, i2); err != nil { 51 t.Fatal(err) 52 } 53 err = s.Get(key2, i) 54 if err != nil { 55 t.Fatal(err) 56 } 57 if i.String() != i2.String() { 58 t.Errorf("expected interval %s, got %s", i2, i) 59 } 60 61 if err := s.Delete(key1); err != nil { 62 t.Fatal(err) 63 } 64 if err := s.Get(key1, i); err != state.ErrNotFound { 65 t.Errorf("expected error %v, got %s", state.ErrNotFound, err) 66 } 67 if err := s.Get(key2, i); err != nil { 68 t.Errorf("expected error %v, got %s", nil, err) 69 } 70 71 if err := s.Delete(key2); err != nil { 72 t.Fatal(err) 73 } 74 if err := s.Get(key2, i); err != state.ErrNotFound { 75 t.Errorf("expected error %v, got %s", state.ErrNotFound, err) 76 } 77 }