Skip to content

Commit c34f0d2

Browse files
committed
challenger: export mock invoice client
1 parent 609fb76 commit c34f0d2

File tree

2 files changed

+120
-88
lines changed

2 files changed

+120
-88
lines changed

challenger/challenger_test.go

+2-88
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package challenger
22

33
import (
4-
"context"
54
"fmt"
65
"sync"
76
"testing"
@@ -10,86 +9,14 @@ import (
109
"github.com/lightningnetwork/lnd/lnrpc"
1110
"github.com/lightningnetwork/lnd/lntypes"
1211
"github.com/stretchr/testify/require"
13-
"google.golang.org/grpc"
1412
)
1513

1614
var (
1715
defaultTimeout = 100 * time.Millisecond
1816
)
1917

20-
type invoiceStreamMock struct {
21-
lnrpc.Lightning_SubscribeInvoicesClient
22-
23-
updateChan chan *lnrpc.Invoice
24-
errChan chan error
25-
quit chan struct{}
26-
}
27-
28-
func (i *invoiceStreamMock) Recv() (*lnrpc.Invoice, error) {
29-
select {
30-
case msg := <-i.updateChan:
31-
return msg, nil
32-
33-
case err := <-i.errChan:
34-
return nil, err
35-
36-
case <-i.quit:
37-
return nil, context.Canceled
38-
}
39-
}
40-
41-
type mockInvoiceClient struct {
42-
invoices []*lnrpc.Invoice
43-
updateChan chan *lnrpc.Invoice
44-
errChan chan error
45-
quit chan struct{}
46-
47-
lastAddIndex uint64
48-
}
49-
50-
// ListInvoices returns a paginated list of all invoices known to lnd.
51-
func (m *mockInvoiceClient) ListInvoices(_ context.Context,
52-
_ *lnrpc.ListInvoiceRequest,
53-
_ ...grpc.CallOption) (*lnrpc.ListInvoiceResponse, error) {
54-
55-
return &lnrpc.ListInvoiceResponse{
56-
Invoices: m.invoices,
57-
}, nil
58-
}
59-
60-
// SubscribeInvoices subscribes to updates on invoices.
61-
func (m *mockInvoiceClient) SubscribeInvoices(_ context.Context,
62-
in *lnrpc.InvoiceSubscription, _ ...grpc.CallOption) (
63-
lnrpc.Lightning_SubscribeInvoicesClient, error) {
64-
65-
m.lastAddIndex = in.AddIndex
66-
67-
return &invoiceStreamMock{
68-
updateChan: m.updateChan,
69-
errChan: m.errChan,
70-
quit: m.quit,
71-
}, nil
72-
}
73-
74-
// AddInvoice adds a new invoice to lnd.
75-
func (m *mockInvoiceClient) AddInvoice(_ context.Context, in *lnrpc.Invoice,
76-
_ ...grpc.CallOption) (*lnrpc.AddInvoiceResponse, error) {
77-
78-
m.invoices = append(m.invoices, in)
79-
80-
return &lnrpc.AddInvoiceResponse{
81-
RHash: in.RHash,
82-
PaymentRequest: in.PaymentRequest,
83-
AddIndex: uint64(len(m.invoices) - 1),
84-
}, nil
85-
}
86-
87-
func (m *mockInvoiceClient) stop() {
88-
close(m.quit)
89-
}
90-
91-
func newChallenger() (*LndChallenger, *mockInvoiceClient, chan error) {
92-
mockClient := &mockInvoiceClient{
18+
func newChallenger() (*LndChallenger, *MockInvoiceClient, chan error) {
19+
mockClient := &MockInvoiceClient{
9320
updateChan: make(chan *lnrpc.Invoice),
9421
errChan: make(chan error, 1),
9522
quit: make(chan struct{}),
@@ -111,19 +38,6 @@ func newChallenger() (*LndChallenger, *mockInvoiceClient, chan error) {
11138
}, mockClient, mainErrChan
11239
}
11340

114-
func newInvoice(hash lntypes.Hash, addIndex uint64,
115-
state lnrpc.Invoice_InvoiceState) *lnrpc.Invoice {
116-
117-
return &lnrpc.Invoice{
118-
PaymentRequest: "foo",
119-
RHash: hash[:],
120-
AddIndex: addIndex,
121-
State: state,
122-
CreationDate: time.Now().Unix(),
123-
Expiry: 10,
124-
}
125-
}
126-
12741
func TestLndChallenger(t *testing.T) {
12842
t.Parallel()
12943

challenger/mock_invoice_client.go

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
func NewChallenger() (*LndChallenger, *MockInvoiceClient, chan error) {
85+
mockClient := &MockInvoiceClient{
86+
updateChan: make(chan *lnrpc.Invoice),
87+
errChan: make(chan error, 1),
88+
quit: make(chan struct{}),
89+
}
90+
genInvoiceReq := func(price int64) (*lnrpc.Invoice, error) {
91+
return newInvoice(lntypes.ZeroHash, 99, lnrpc.Invoice_OPEN),
92+
nil
93+
}
94+
invoicesMtx := &sync.Mutex{}
95+
mainErrChan := make(chan error)
96+
return &LndChallenger{
97+
client: mockClient,
98+
genInvoiceReq: genInvoiceReq,
99+
invoiceStates: make(map[lntypes.Hash]lnrpc.Invoice_InvoiceState),
100+
quit: make(chan struct{}),
101+
invoicesMtx: invoicesMtx,
102+
invoicesCond: sync.NewCond(invoicesMtx),
103+
errChan: mainErrChan,
104+
}, mockClient, mainErrChan
105+
}
106+
107+
func newInvoice(hash lntypes.Hash, addIndex uint64,
108+
state lnrpc.Invoice_InvoiceState) *lnrpc.Invoice {
109+
110+
return &lnrpc.Invoice{
111+
PaymentRequest: "foo",
112+
RHash: hash[:],
113+
AddIndex: addIndex,
114+
State: state,
115+
CreationDate: time.Now().Unix(),
116+
Expiry: 10,
117+
}
118+
}

0 commit comments

Comments
 (0)