github.com/xxRanger/go-ethereum@v1.8.23/swarm/storage/localstore/mode_set_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 localstore
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/syndtr/goleveldb/leveldb"
    24  )
    25  
    26  // TestModeSetAccess validates ModeSetAccess index values on the provided DB.
    27  func TestModeSetAccess(t *testing.T) {
    28  	db, cleanupFunc := newTestDB(t, nil)
    29  	defer cleanupFunc()
    30  
    31  	chunk := generateRandomChunk()
    32  
    33  	wantTimestamp := time.Now().UTC().UnixNano()
    34  	defer setNow(func() (t int64) {
    35  		return wantTimestamp
    36  	})()
    37  
    38  	err := db.NewSetter(ModeSetAccess).Set(chunk.Address())
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  
    43  	t.Run("pull index", newPullIndexTest(db, chunk, wantTimestamp, nil))
    44  
    45  	t.Run("pull index count", newItemsCountTest(db.pullIndex, 1))
    46  
    47  	t.Run("gc index", newGCIndexTest(db, chunk, wantTimestamp, wantTimestamp))
    48  
    49  	t.Run("gc index count", newItemsCountTest(db.gcIndex, 1))
    50  
    51  	t.Run("gc size", newIndexGCSizeTest(db))
    52  }
    53  
    54  // TestModeSetSync validates ModeSetSync index values on the provided DB.
    55  func TestModeSetSync(t *testing.T) {
    56  	db, cleanupFunc := newTestDB(t, nil)
    57  	defer cleanupFunc()
    58  
    59  	chunk := generateRandomChunk()
    60  
    61  	wantTimestamp := time.Now().UTC().UnixNano()
    62  	defer setNow(func() (t int64) {
    63  		return wantTimestamp
    64  	})()
    65  
    66  	err := db.NewPutter(ModePutUpload).Put(chunk)
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  
    71  	err = db.NewSetter(ModeSetSync).Set(chunk.Address())
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  
    76  	t.Run("retrieve indexes", newRetrieveIndexesTestWithAccess(db, chunk, wantTimestamp, wantTimestamp))
    77  
    78  	t.Run("push index", newPushIndexTest(db, chunk, wantTimestamp, leveldb.ErrNotFound))
    79  
    80  	t.Run("gc index", newGCIndexTest(db, chunk, wantTimestamp, wantTimestamp))
    81  
    82  	t.Run("gc index count", newItemsCountTest(db.gcIndex, 1))
    83  
    84  	t.Run("gc size", newIndexGCSizeTest(db))
    85  }
    86  
    87  // TestModeSetRemove validates ModeSetRemove index values on the provided DB.
    88  func TestModeSetRemove(t *testing.T) {
    89  	db, cleanupFunc := newTestDB(t, nil)
    90  	defer cleanupFunc()
    91  
    92  	chunk := generateRandomChunk()
    93  
    94  	err := db.NewPutter(ModePutUpload).Put(chunk)
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  
    99  	err = db.NewSetter(modeSetRemove).Set(chunk.Address())
   100  	if err != nil {
   101  		t.Fatal(err)
   102  	}
   103  
   104  	t.Run("retrieve indexes", func(t *testing.T) {
   105  		wantErr := leveldb.ErrNotFound
   106  		_, err := db.retrievalDataIndex.Get(addressToItem(chunk.Address()))
   107  		if err != wantErr {
   108  			t.Errorf("got error %v, want %v", err, wantErr)
   109  		}
   110  		t.Run("retrieve data index count", newItemsCountTest(db.retrievalDataIndex, 0))
   111  
   112  		// access index should not be set
   113  		_, err = db.retrievalAccessIndex.Get(addressToItem(chunk.Address()))
   114  		if err != wantErr {
   115  			t.Errorf("got error %v, want %v", err, wantErr)
   116  		}
   117  		t.Run("retrieve access index count", newItemsCountTest(db.retrievalAccessIndex, 0))
   118  	})
   119  
   120  	t.Run("pull index", newPullIndexTest(db, chunk, 0, leveldb.ErrNotFound))
   121  
   122  	t.Run("pull index count", newItemsCountTest(db.pullIndex, 0))
   123  
   124  	t.Run("gc index count", newItemsCountTest(db.gcIndex, 0))
   125  
   126  	t.Run("gc size", newIndexGCSizeTest(db))
   127  
   128  }