github.com/codingfuture/orig-energi3@v0.8.4/swarm/storage/feed/testutil.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 feed
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"path/filepath"
    23  	"sync"
    24  
    25  	"github.com/ethereum/go-ethereum/p2p/enode"
    26  	"github.com/ethereum/go-ethereum/swarm/storage"
    27  )
    28  
    29  const (
    30  	testDbDirName = "feeds"
    31  )
    32  
    33  type TestHandler struct {
    34  	*Handler
    35  }
    36  
    37  func (t *TestHandler) Close() {
    38  	t.chunkStore.Close()
    39  }
    40  
    41  type mockNetFetcher struct{}
    42  
    43  func (m *mockNetFetcher) Request(hopCount uint8) {
    44  }
    45  func (m *mockNetFetcher) Offer(source *enode.ID) {
    46  }
    47  
    48  func newFakeNetFetcher(context.Context, storage.Address, *sync.Map) storage.NetFetcher {
    49  	return &mockNetFetcher{}
    50  }
    51  
    52  // NewTestHandler creates Handler object to be used for testing purposes.
    53  func NewTestHandler(datadir string, params *HandlerParams) (*TestHandler, error) {
    54  	path := filepath.Join(datadir, testDbDirName)
    55  	fh := NewHandler(params)
    56  	localstoreparams := storage.NewDefaultLocalStoreParams()
    57  	localstoreparams.Init(path)
    58  	localStore, err := storage.NewLocalStore(localstoreparams, nil)
    59  	if err != nil {
    60  		return nil, fmt.Errorf("localstore create fail, path %s: %v", path, err)
    61  	}
    62  	localStore.Validators = append(localStore.Validators, storage.NewContentAddressValidator(storage.MakeHashFunc(feedsHashAlgorithm)))
    63  	localStore.Validators = append(localStore.Validators, fh)
    64  	netStore, err := storage.NewNetStore(localStore, nil)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	netStore.NewNetFetcherFunc = newFakeNetFetcher
    69  	fh.SetStore(netStore)
    70  	return &TestHandler{fh}, nil
    71  }