github.com/xxRanger/go-ethereum@v1.8.23/swarm/storage/memstore_test.go (about)

     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  
    17  package storage
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/swarm/log"
    24  )
    25  
    26  func newTestMemStore() *MemStore {
    27  	storeparams := NewDefaultStoreParams()
    28  	return NewMemStore(storeparams, nil)
    29  }
    30  
    31  func testMemStoreRandom(n int, t *testing.T) {
    32  	m := newTestMemStore()
    33  	defer m.Close()
    34  	testStoreRandom(m, n, t)
    35  }
    36  
    37  func testMemStoreCorrect(n int, t *testing.T) {
    38  	m := newTestMemStore()
    39  	defer m.Close()
    40  	testStoreCorrect(m, n, t)
    41  }
    42  
    43  func TestMemStoreRandom_1(t *testing.T) {
    44  	testMemStoreRandom(1, t)
    45  }
    46  
    47  func TestMemStoreCorrect_1(t *testing.T) {
    48  	testMemStoreCorrect(1, t)
    49  }
    50  
    51  func TestMemStoreRandom_1k(t *testing.T) {
    52  	testMemStoreRandom(1000, t)
    53  }
    54  
    55  func TestMemStoreCorrect_1k(t *testing.T) {
    56  	testMemStoreCorrect(100, t)
    57  }
    58  
    59  func TestMemStoreNotFound(t *testing.T) {
    60  	m := newTestMemStore()
    61  	defer m.Close()
    62  
    63  	_, err := m.Get(context.TODO(), ZeroAddr)
    64  	if err != ErrChunkNotFound {
    65  		t.Errorf("Expected ErrChunkNotFound, got %v", err)
    66  	}
    67  }
    68  
    69  func benchmarkMemStorePut(n int, b *testing.B) {
    70  	m := newTestMemStore()
    71  	defer m.Close()
    72  	benchmarkStorePut(m, n, b)
    73  }
    74  
    75  func benchmarkMemStoreGet(n int, b *testing.B) {
    76  	m := newTestMemStore()
    77  	defer m.Close()
    78  	benchmarkStoreGet(m, n, b)
    79  }
    80  
    81  func BenchmarkMemStorePut_500(b *testing.B) {
    82  	benchmarkMemStorePut(500, b)
    83  }
    84  
    85  func BenchmarkMemStoreGet_500(b *testing.B) {
    86  	benchmarkMemStoreGet(500, b)
    87  }
    88  
    89  func TestMemStoreAndLDBStore(t *testing.T) {
    90  	ldb, cleanup := newLDBStore(t)
    91  	ldb.setCapacity(4000)
    92  	defer cleanup()
    93  
    94  	cacheCap := 200
    95  	memStore := NewMemStore(NewStoreParams(4000, 200, nil, nil), nil)
    96  
    97  	tests := []struct {
    98  		n         int   // number of chunks to push to memStore
    99  		chunkSize int64 // size of chunk (by default in Swarm - 4096)
   100  	}{
   101  		{
   102  			n:         1,
   103  			chunkSize: 4096,
   104  		},
   105  		{
   106  			n:         101,
   107  			chunkSize: 4096,
   108  		},
   109  		{
   110  			n:         501,
   111  			chunkSize: 4096,
   112  		},
   113  		{
   114  			n:         1100,
   115  			chunkSize: 4096,
   116  		},
   117  	}
   118  
   119  	for i, tt := range tests {
   120  		log.Info("running test", "idx", i, "tt", tt)
   121  		var chunks []Chunk
   122  
   123  		for i := 0; i < tt.n; i++ {
   124  			c := GenerateRandomChunk(tt.chunkSize)
   125  			chunks = append(chunks, c)
   126  		}
   127  
   128  		for i := 0; i < tt.n; i++ {
   129  			err := ldb.Put(context.TODO(), chunks[i])
   130  			if err != nil {
   131  				t.Fatal(err)
   132  			}
   133  			err = memStore.Put(context.TODO(), chunks[i])
   134  			if err != nil {
   135  				t.Fatal(err)
   136  			}
   137  
   138  			if got := memStore.cache.Len(); got > cacheCap {
   139  				t.Fatalf("expected to get cache capacity less than %v, but got %v", cacheCap, got)
   140  			}
   141  
   142  		}
   143  
   144  		for i := 0; i < tt.n; i++ {
   145  			_, err := memStore.Get(context.TODO(), chunks[i].Address())
   146  			if err != nil {
   147  				if err == ErrChunkNotFound {
   148  					_, err := ldb.Get(context.TODO(), chunks[i].Address())
   149  					if err != nil {
   150  						t.Fatalf("couldn't get chunk %v from ldb, got error: %v", i, err)
   151  					}
   152  				} else {
   153  					t.Fatalf("got error from memstore: %v", err)
   154  				}
   155  			}
   156  		}
   157  	}
   158  }