Skip to content

Commit

Permalink
chain: add interface mocks and initial test setup
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MStreet3 committed Oct 16, 2022
1 parent f722a35 commit 683e4a7
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
102 changes: 102 additions & 0 deletions chain/mocks_test.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions chain/neutrino_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 683e4a7

Please sign in to comment.