testutil.go 2.1 KB

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