Skip to content

Commit 7edbc20

Browse files
committed
challenger: export mock invoice client so we can use it in tests elsewhere
1 parent 392ac82 commit 7edbc20

File tree

2 files changed

+126
-7
lines changed

2 files changed

+126
-7
lines changed

challenger/challenger_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (i *invoiceStreamMock) Recv() (*lnrpc.Invoice, error) {
3838
}
3939
}
4040

41-
type mockInvoiceClient struct {
41+
type MockInvoiceClient struct {
4242
invoices []*lnrpc.Invoice
4343
updateChan chan *lnrpc.Invoice
4444
errChan chan error
@@ -48,7 +48,7 @@ type mockInvoiceClient struct {
4848
}
4949

5050
// ListInvoices returns a paginated list of all invoices known to lnd.
51-
func (m *mockInvoiceClient) ListInvoices(_ context.Context,
51+
func (m *MockInvoiceClient) ListInvoices(_ context.Context,
5252
_ *lnrpc.ListInvoiceRequest,
5353
_ ...grpc.CallOption) (*lnrpc.ListInvoiceResponse, error) {
5454

@@ -58,7 +58,7 @@ func (m *mockInvoiceClient) ListInvoices(_ context.Context,
5858
}
5959

6060
// SubscribeInvoices subscribes to updates on invoices.
61-
func (m *mockInvoiceClient) SubscribeInvoices(_ context.Context,
61+
func (m *MockInvoiceClient) SubscribeInvoices(_ context.Context,
6262
in *lnrpc.InvoiceSubscription, _ ...grpc.CallOption) (
6363
lnrpc.Lightning_SubscribeInvoicesClient, error) {
6464

@@ -72,7 +72,7 @@ func (m *mockInvoiceClient) SubscribeInvoices(_ context.Context,
7272
}
7373

7474
// AddInvoice adds a new invoice to lnd.
75-
func (m *mockInvoiceClient) AddInvoice(_ context.Context, in *lnrpc.Invoice,
75+
func (m *MockInvoiceClient) AddInvoice(_ context.Context, in *lnrpc.Invoice,
7676
_ ...grpc.CallOption) (*lnrpc.AddInvoiceResponse, error) {
7777

7878
m.invoices = append(m.invoices, in)
@@ -84,12 +84,12 @@ func (m *mockInvoiceClient) AddInvoice(_ context.Context, in *lnrpc.Invoice,
8484
}, nil
8585
}
8686

87-
func (m *mockInvoiceClient) stop() {
87+
func (m *MockInvoiceClient) stop() {
8888
close(m.quit)
8989
}
9090

91-
func newChallenger() (*LndChallenger, *mockInvoiceClient, chan error) {
92-
mockClient := &mockInvoiceClient{
91+
func newChallenger() (*LndChallenger, *MockInvoiceClient, chan error) {
92+
mockClient := &MockInvoiceClient{
9393
updateChan: make(chan *lnrpc.Invoice),
9494
errChan: make(chan error, 1),
9595
quit: make(chan struct{}),

challenger/mock_invoice_client.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package challenger
2+
3+
import (
4+
"context"
5+
"sync"
6+
"time"
7+
8+
"github.com/lightningnetwork/lnd/lnrpc"
9+
"github.com/lightningnetwork/lnd/lntypes"
10+
"google.golang.org/grpc"
11+
)
12+
13+
type invoiceStreamMock struct {
14+
lnrpc.Lightning_SubscribeInvoicesClient
15+
16+
updateChan chan *lnrpc.Invoice
17+
errChan chan error
18+
quit chan struct{}
19+
}
20+
21+
func (i *invoiceStreamMock) Recv() (*lnrpc.Invoice, error) {
22+
select {
23+
case msg := <-i.updateChan:
24+
return msg, nil
25+
26+
case err := <-i.errChan:
27+
return nil, err
28+
29+
case <-i.quit:
30+
return nil, context.Canceled
31+
}
32+
}
33+
34+
type MockInvoiceClient struct {
35+
invoices []*lnrpc.Invoice
36+
updateChan chan *lnrpc.Invoice
37+
errChan chan error
38+
quit chan struct{}
39+
40+
lastAddIndex uint64
41+
}
42+
43+
// ListInvoices returns a paginated list of all invoices known to lnd.
44+
func (m *MockInvoiceClient) ListInvoices(_ context.Context,
45+
_ *lnrpc.ListInvoiceRequest,
46+
_ ...grpc.CallOption) (*lnrpc.ListInvoiceResponse, error) {
47+
48+
return &lnrpc.ListInvoiceResponse{
49+
Invoices: m.invoices,
50+
}, nil
51+
}
52+
53+
// SubscribeInvoices subscribes to updates on invoices.
54+
func (m *MockInvoiceClient) SubscribeInvoices(_ context.Context,
55+
in *lnrpc.InvoiceSubscription, _ ...grpc.CallOption) (
56+
lnrpc.Lightning_SubscribeInvoicesClient, error) {
57+
58+
m.lastAddIndex = in.AddIndex
59+
60+
return &invoiceStreamMock{
61+
updateChan: m.updateChan,
62+
errChan: m.errChan,
63+
quit: m.quit,
64+
}, nil
65+
}
66+
67+
// AddInvoice adds a new invoice to lnd.
68+
func (m *MockInvoiceClient) AddInvoice(_ context.Context, in *lnrpc.Invoice,
69+
_ ...grpc.CallOption) (*lnrpc.AddInvoiceResponse, error) {
70+
71+
m.invoices = append(m.invoices, in)
72+
73+
return &lnrpc.AddInvoiceResponse{
74+
RHash: in.RHash,
75+
PaymentRequest: in.PaymentRequest,
76+
AddIndex: uint64(len(m.invoices) - 1),
77+
}, nil
78+
}
79+
80+
func (m *MockInvoiceClient) stop() {
81+
close(m.quit)
82+
}
83+
84+
// CHANGE NAME TO NEWTESTCHALLENGER?
85+
func NewChallenger() (*LndChallenger, *MockInvoiceClient, chan error) {
86+
mockClient := &MockInvoiceClient{
87+
updateChan: make(chan *lnrpc.Invoice),
88+
errChan: make(chan error, 1),
89+
quit: make(chan struct{}),
90+
}
91+
genInvoiceReq := func(price int64) (*lnrpc.Invoice, error) {
92+
return newInvoice(lntypes.ZeroHash, 99, lnrpc.Invoice_OPEN),
93+
nil
94+
}
95+
invoicesMtx := &sync.Mutex{}
96+
mainErrChan := make(chan error)
97+
return &LndChallenger{
98+
client: mockClient,
99+
genInvoiceReq: genInvoiceReq,
100+
invoiceStates: make(map[lntypes.Hash]lnrpc.Invoice_InvoiceState),
101+
quit: make(chan struct{}),
102+
invoicesMtx: invoicesMtx,
103+
invoicesCond: sync.NewCond(invoicesMtx),
104+
errChan: mainErrChan,
105+
}, mockClient, mainErrChan
106+
}
107+
108+
func newInvoice(hash lntypes.Hash, addIndex uint64,
109+
state lnrpc.Invoice_InvoiceState) *lnrpc.Invoice {
110+
111+
return &lnrpc.Invoice{
112+
PaymentRequest: "foo",
113+
RHash: hash[:],
114+
AddIndex: addIndex,
115+
State: state,
116+
CreationDate: time.Now().Unix(),
117+
Expiry: 10,
118+
}
119+
}

0 commit comments

Comments
 (0)