github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/swarm/storage/dpa_test.go (about)

     1  // Copyright 2016 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package storage
    18  
    19  import (
    20  	"bytes"
    21  	"io"
    22  	"io/ioutil"
    23  	"os"
    24  	"sync"
    25  	"testing"
    26  )
    27  
    28  const testDataSize = 0x1000000
    29  
    30  func TestDPArandom(t *testing.T) {
    31  	dbStore := initDbStore(t)
    32  	dbStore.setCapacity(50000)
    33  	memStore := NewMemStore(dbStore, defaultCacheCapacity)
    34  	localStore := &LocalStore{
    35  		memStore,
    36  		dbStore,
    37  	}
    38  	chunker := NewTreeChunker(NewChunkerParams())
    39  	dpa := &DPA{
    40  		Chunker:    chunker,
    41  		ChunkStore: localStore,
    42  	}
    43  	dpa.Start()
    44  	defer dpa.Stop()
    45  	defer os.RemoveAll("/tmp/bzz")
    46  
    47  	reader, slice := testDataReaderAndSlice(testDataSize)
    48  	wg := &sync.WaitGroup{}
    49  	key, err := dpa.Store(reader, testDataSize, wg, nil)
    50  	if err != nil {
    51  		t.Errorf("Store error: %v", err)
    52  	}
    53  	wg.Wait()
    54  	resultReader := dpa.Retrieve(key)
    55  	resultSlice := make([]byte, len(slice))
    56  	n, err := resultReader.ReadAt(resultSlice, 0)
    57  	if err != io.EOF {
    58  		t.Errorf("Retrieve error: %v", err)
    59  	}
    60  	if n != len(slice) {
    61  		t.Errorf("Slice size error got %d, expected %d.", n, len(slice))
    62  	}
    63  	if !bytes.Equal(slice, resultSlice) {
    64  		t.Errorf("Comparison error.")
    65  	}
    66  	ioutil.WriteFile("/tmp/slice.bzz.16M", slice, 0666)
    67  	ioutil.WriteFile("/tmp/result.bzz.16M", resultSlice, 0666)
    68  	localStore.memStore = NewMemStore(dbStore, defaultCacheCapacity)
    69  	resultReader = dpa.Retrieve(key)
    70  	for i := range resultSlice {
    71  		resultSlice[i] = 0
    72  	}
    73  	n, err = resultReader.ReadAt(resultSlice, 0)
    74  	if err != io.EOF {
    75  		t.Errorf("Retrieve error after removing memStore: %v", err)
    76  	}
    77  	if n != len(slice) {
    78  		t.Errorf("Slice size error after removing memStore got %d, expected %d.", n, len(slice))
    79  	}
    80  	if !bytes.Equal(slice, resultSlice) {
    81  		t.Errorf("Comparison error after removing memStore.")
    82  	}
    83  }
    84  
    85  func TestDPA_capacity(t *testing.T) {
    86  	dbStore := initDbStore(t)
    87  	memStore := NewMemStore(dbStore, defaultCacheCapacity)
    88  	localStore := &LocalStore{
    89  		memStore,
    90  		dbStore,
    91  	}
    92  	memStore.setCapacity(0)
    93  	chunker := NewTreeChunker(NewChunkerParams())
    94  	dpa := &DPA{
    95  		Chunker:    chunker,
    96  		ChunkStore: localStore,
    97  	}
    98  	dpa.Start()
    99  	reader, slice := testDataReaderAndSlice(testDataSize)
   100  	wg := &sync.WaitGroup{}
   101  	key, err := dpa.Store(reader, testDataSize, wg, nil)
   102  	if err != nil {
   103  		t.Errorf("Store error: %v", err)
   104  	}
   105  	wg.Wait()
   106  	resultReader := dpa.Retrieve(key)
   107  	resultSlice := make([]byte, len(slice))
   108  	n, err := resultReader.ReadAt(resultSlice, 0)
   109  	if err != io.EOF {
   110  		t.Errorf("Retrieve error: %v", err)
   111  	}
   112  	if n != len(slice) {
   113  		t.Errorf("Slice size error got %d, expected %d.", n, len(slice))
   114  	}
   115  	if !bytes.Equal(slice, resultSlice) {
   116  		t.Errorf("Comparison error.")
   117  	}
   118  	// Clear memStore
   119  	memStore.setCapacity(0)
   120  	// check whether it is, indeed, empty
   121  	dpa.ChunkStore = memStore
   122  	resultReader = dpa.Retrieve(key)
   123  	if _, err = resultReader.ReadAt(resultSlice, 0); err == nil {
   124  		t.Errorf("Was able to read %d bytes from an empty memStore.", len(slice))
   125  	}
   126  	// check how it works with localStore
   127  	dpa.ChunkStore = localStore
   128  	//	localStore.dbStore.setCapacity(0)
   129  	resultReader = dpa.Retrieve(key)
   130  	for i := range resultSlice {
   131  		resultSlice[i] = 0
   132  	}
   133  	n, err = resultReader.ReadAt(resultSlice, 0)
   134  	if err != io.EOF {
   135  		t.Errorf("Retrieve error after clearing memStore: %v", err)
   136  	}
   137  	if n != len(slice) {
   138  		t.Errorf("Slice size error after clearing memStore got %d, expected %d.", n, len(slice))
   139  	}
   140  	if !bytes.Equal(slice, resultSlice) {
   141  		t.Errorf("Comparison error after clearing memStore.")
   142  	}
   143  }