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