testutil.go 2.2 KB

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