From 46ecfe60b4b0c9aeb52ca77e6e4a0de0e497c0c3 Mon Sep 17 00:00:00 2001 From: Turtle Date: Sat, 4 Mar 2023 15:59:50 -0600 Subject: [PATCH] challenger: export mock invoice client so we can use it in tests elsewhere --- challenger/challenger_test.go | 90 +---------------------- challenger/mock_invoice_client.go | 118 ++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 88 deletions(-) create mode 100644 challenger/mock_invoice_client.go diff --git a/challenger/challenger_test.go b/challenger/challenger_test.go index 6958c05..b898b88 100644 --- a/challenger/challenger_test.go +++ b/challenger/challenger_test.go @@ -1,7 +1,6 @@ package challenger import ( - "context" "fmt" "sync" "testing" @@ -10,86 +9,14 @@ import ( "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lntypes" "github.com/stretchr/testify/require" - "google.golang.org/grpc" ) var ( defaultTimeout = 100 * time.Millisecond ) -type invoiceStreamMock struct { - lnrpc.Lightning_SubscribeInvoicesClient - - updateChan chan *lnrpc.Invoice - errChan chan error - quit chan struct{} -} - -func (i *invoiceStreamMock) Recv() (*lnrpc.Invoice, error) { - select { - case msg := <-i.updateChan: - return msg, nil - - case err := <-i.errChan: - return nil, err - - case <-i.quit: - return nil, context.Canceled - } -} - -type mockInvoiceClient struct { - invoices []*lnrpc.Invoice - updateChan chan *lnrpc.Invoice - errChan chan error - quit chan struct{} - - lastAddIndex uint64 -} - -// ListInvoices returns a paginated list of all invoices known to lnd. -func (m *mockInvoiceClient) ListInvoices(_ context.Context, - _ *lnrpc.ListInvoiceRequest, - _ ...grpc.CallOption) (*lnrpc.ListInvoiceResponse, error) { - - return &lnrpc.ListInvoiceResponse{ - Invoices: m.invoices, - }, nil -} - -// SubscribeInvoices subscribes to updates on invoices. -func (m *mockInvoiceClient) SubscribeInvoices(_ context.Context, - in *lnrpc.InvoiceSubscription, _ ...grpc.CallOption) ( - lnrpc.Lightning_SubscribeInvoicesClient, error) { - - m.lastAddIndex = in.AddIndex - - return &invoiceStreamMock{ - updateChan: m.updateChan, - errChan: m.errChan, - quit: m.quit, - }, nil -} - -// AddInvoice adds a new invoice to lnd. -func (m *mockInvoiceClient) AddInvoice(_ context.Context, in *lnrpc.Invoice, - _ ...grpc.CallOption) (*lnrpc.AddInvoiceResponse, error) { - - m.invoices = append(m.invoices, in) - - return &lnrpc.AddInvoiceResponse{ - RHash: in.RHash, - PaymentRequest: in.PaymentRequest, - AddIndex: uint64(len(m.invoices) - 1), - }, nil -} - -func (m *mockInvoiceClient) stop() { - close(m.quit) -} - -func newChallenger() (*LndChallenger, *mockInvoiceClient, chan error) { - mockClient := &mockInvoiceClient{ +func newChallenger() (*LndChallenger, *MockInvoiceClient, chan error) { + mockClient := &MockInvoiceClient{ updateChan: make(chan *lnrpc.Invoice), errChan: make(chan error, 1), quit: make(chan struct{}), @@ -111,19 +38,6 @@ func newChallenger() (*LndChallenger, *mockInvoiceClient, chan error) { }, mockClient, mainErrChan } -func newInvoice(hash lntypes.Hash, addIndex uint64, - state lnrpc.Invoice_InvoiceState) *lnrpc.Invoice { - - return &lnrpc.Invoice{ - PaymentRequest: "foo", - RHash: hash[:], - AddIndex: addIndex, - State: state, - CreationDate: time.Now().Unix(), - Expiry: 10, - } -} - func TestLndChallenger(t *testing.T) { t.Parallel() diff --git a/challenger/mock_invoice_client.go b/challenger/mock_invoice_client.go new file mode 100644 index 0000000..c60c6d6 --- /dev/null +++ b/challenger/mock_invoice_client.go @@ -0,0 +1,118 @@ +package challenger + +import ( + "context" + "sync" + "time" + + "github.com/lightningnetwork/lnd/lnrpc" + "github.com/lightningnetwork/lnd/lntypes" + "google.golang.org/grpc" +) + +type invoiceStreamMock struct { + lnrpc.Lightning_SubscribeInvoicesClient + + updateChan chan *lnrpc.Invoice + errChan chan error + quit chan struct{} +} + +func (i *invoiceStreamMock) Recv() (*lnrpc.Invoice, error) { + select { + case msg := <-i.updateChan: + return msg, nil + + case err := <-i.errChan: + return nil, err + + case <-i.quit: + return nil, context.Canceled + } +} + +type MockInvoiceClient struct { + invoices []*lnrpc.Invoice + updateChan chan *lnrpc.Invoice + errChan chan error + quit chan struct{} + + lastAddIndex uint64 +} + +// ListInvoices returns a paginated list of all invoices known to lnd. +func (m *MockInvoiceClient) ListInvoices(_ context.Context, + _ *lnrpc.ListInvoiceRequest, + _ ...grpc.CallOption) (*lnrpc.ListInvoiceResponse, error) { + + return &lnrpc.ListInvoiceResponse{ + Invoices: m.invoices, + }, nil +} + +// SubscribeInvoices subscribes to updates on invoices. +func (m *MockInvoiceClient) SubscribeInvoices(_ context.Context, + in *lnrpc.InvoiceSubscription, _ ...grpc.CallOption) ( + lnrpc.Lightning_SubscribeInvoicesClient, error) { + + m.lastAddIndex = in.AddIndex + + return &invoiceStreamMock{ + updateChan: m.updateChan, + errChan: m.errChan, + quit: m.quit, + }, nil +} + +// AddInvoice adds a new invoice to lnd. +func (m *MockInvoiceClient) AddInvoice(_ context.Context, in *lnrpc.Invoice, + _ ...grpc.CallOption) (*lnrpc.AddInvoiceResponse, error) { + + m.invoices = append(m.invoices, in) + + return &lnrpc.AddInvoiceResponse{ + RHash: in.RHash, + PaymentRequest: in.PaymentRequest, + AddIndex: uint64(len(m.invoices) - 1), + }, nil +} + +func (m *MockInvoiceClient) stop() { + close(m.quit) +} + +func NewChallenger() (*LndChallenger, *MockInvoiceClient, chan error) { + mockClient := &MockInvoiceClient{ + updateChan: make(chan *lnrpc.Invoice), + errChan: make(chan error, 1), + quit: make(chan struct{}), + } + genInvoiceReq := func(price int64) (*lnrpc.Invoice, error) { + return newInvoice(lntypes.ZeroHash, 99, lnrpc.Invoice_OPEN), + nil + } + invoicesMtx := &sync.Mutex{} + mainErrChan := make(chan error) + return &LndChallenger{ + client: mockClient, + genInvoiceReq: genInvoiceReq, + invoiceStates: make(map[lntypes.Hash]lnrpc.Invoice_InvoiceState), + quit: make(chan struct{}), + invoicesMtx: invoicesMtx, + invoicesCond: sync.NewCond(invoicesMtx), + errChan: mainErrChan, + }, mockClient, mainErrChan +} + +func newInvoice(hash lntypes.Hash, addIndex uint64, + state lnrpc.Invoice_InvoiceState) *lnrpc.Invoice { + + return &lnrpc.Invoice{ + PaymentRequest: "foo", + RHash: hash[:], + AddIndex: addIndex, + State: state, + CreationDate: time.Now().Unix(), + Expiry: 10, + } +}