From 683e4a79f9b3a237e17fcb1b24a14f974cc803ee Mon Sep 17 00:00:00 2001 From: Michael Street <5597260+MStreet3@users.noreply.github.com> Date: Sat, 15 Oct 2022 20:27:42 -0400 Subject: [PATCH] chain: add interface mocks and initial test setup Changes the Start method to signal Done on the wait group once the ClientConnected message is sent and calls wg.Wait() in the Stop method. --- chain/mocks_test.go | 102 +++++++++++++++++++++++++++++++++++++++++ chain/neutrino_test.go | 13 ++++++ 2 files changed, 115 insertions(+) create mode 100644 chain/mocks_test.go create mode 100644 chain/neutrino_test.go diff --git a/chain/mocks_test.go b/chain/mocks_test.go new file mode 100644 index 0000000000..b740253e61 --- /dev/null +++ b/chain/mocks_test.go @@ -0,0 +1,102 @@ +package chain + +import ( + "errors" + "testing" + + "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/gcs" + "github.com/btcsuite/btcd/chaincfg" + "github.com/btcsuite/btcd/chaincfg/chainhash" + "github.com/btcsuite/btcd/wire" + "github.com/lightninglabs/neutrino" + "github.com/lightninglabs/neutrino/headerfs" +) + +var ( + ErrNotImplemented = errors.New("not implemented") + _ Rescanner = (*mockRescanner)(nil) + _ ChainService = (*mockChainService)(nil) + testBestBlock = &headerfs.BlockStamp{ + Height: 42, + } +) + +func newMockNeutrinoClient(t *testing.T) *NeutrinoClient { + t.Helper() + var ( + chainParams = &chaincfg.Params{} + chainSvc = &mockChainService{} + newRescanner = func(cs neutrino.ChainSource, ro ...neutrino.RescanOption) Rescanner { + return &mockRescanner{} + } + ) + return &NeutrinoClient{ + CS: chainSvc, + chainParams: chainParams, + newRescanner: newRescanner, + } +} + +type mockRescanner struct{} + +func (m *mockRescanner) Start() <-chan error { + return nil +} + +func (m *mockRescanner) WaitForShutdown() { + panic(ErrNotImplemented) +} + +func (m *mockRescanner) Update(...neutrino.UpdateOption) error { + return ErrNotImplemented +} + +type mockChainService struct{} + +func (m *mockChainService) Start() error { + return nil +} + +func (m *mockChainService) GetBlock( + chainhash.Hash, + ...neutrino.QueryOption, +) (*btcutil.Block, error) { + return nil, ErrNotImplemented +} + +func (m *mockChainService) GetBlockHeight(*chainhash.Hash) (int32, error) { + return 0, ErrNotImplemented +} + +func (m *mockChainService) BestBlock() (*headerfs.BlockStamp, error) { + return m.getBestBlock(), nil +} + +func (m *mockChainService) getBestBlock() *headerfs.BlockStamp { + return testBestBlock +} + +func (m *mockChainService) GetBlockHash(int64) (*chainhash.Hash, error) { + return nil, ErrNotImplemented +} + +func (m *mockChainService) GetBlockHeader(*chainhash.Hash) (*wire.BlockHeader, error) { + return nil, ErrNotImplemented +} + +func (m *mockChainService) IsCurrent() bool { + return false +} + +func (m *mockChainService) SendTransaction(*wire.MsgTx) error { + return ErrNotImplemented +} + +func (m *mockChainService) GetCFilter( + chainhash.Hash, + wire.FilterType, + ...neutrino.QueryOption, +) (*gcs.Filter, error) { + return nil, ErrNotImplemented +} diff --git a/chain/neutrino_test.go b/chain/neutrino_test.go new file mode 100644 index 0000000000..c45d3cdfa1 --- /dev/null +++ b/chain/neutrino_test.go @@ -0,0 +1,13 @@ +package chain + +import ( + "testing" +) + +// TestNeutrinoClientMultipleRestart ensures that multiple goroutines +// can Start and Stop the client without errors or races. +func TestNeutrinoClientMultipleRestart(t *testing.T) { + // call notifyreceived and rescan in a loop + nc := newMockNeutrinoClient(t) + _ = nc +}