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