|
| 1 | +package terminal |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/lightningnetwork/lnd/lnrpc" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "google.golang.org/grpc" |
| 12 | +) |
| 13 | + |
| 14 | +// fakeStateClient is a test-only lnrpc.StateClient that replays a canned |
| 15 | +// sequence of GetState responses/errors, repeating the last entry once the |
| 16 | +// sequence is exhausted. |
| 17 | +type fakeStateClient struct { |
| 18 | + lnrpc.StateClient |
| 19 | + |
| 20 | + responses []*lnrpc.GetStateResponse |
| 21 | + errs []error |
| 22 | + calls int |
| 23 | +} |
| 24 | + |
| 25 | +func (f *fakeStateClient) GetState(_ context.Context, |
| 26 | + _ *lnrpc.GetStateRequest, _ ...grpc.CallOption) ( |
| 27 | + *lnrpc.GetStateResponse, error) { |
| 28 | + |
| 29 | + idx := f.calls |
| 30 | + if idx >= len(f.responses) { |
| 31 | + idx = len(f.responses) - 1 |
| 32 | + } |
| 33 | + f.calls++ |
| 34 | + |
| 35 | + return f.responses[idx], f.errs[idx] |
| 36 | +} |
| 37 | + |
| 38 | +// TestWaitForLndRPCReady asserts that waitForLndRPCReady correctly polls |
| 39 | +// lnd's StateService until it reports a state other than WAITING_TO_START, |
| 40 | +// and that it times out with an error if that never happens. |
| 41 | +func TestWaitForLndRPCReady(t *testing.T) { |
| 42 | + t.Parallel() |
| 43 | + |
| 44 | + activeResp := &lnrpc.GetStateResponse{ |
| 45 | + State: lnrpc.WalletState_RPC_ACTIVE, |
| 46 | + } |
| 47 | + waitingResp := &lnrpc.GetStateResponse{ |
| 48 | + State: lnrpc.WalletState_WAITING_TO_START, |
| 49 | + } |
| 50 | + |
| 51 | + testCases := []struct { |
| 52 | + name string |
| 53 | + client *fakeStateClient |
| 54 | + timeout time.Duration |
| 55 | + expectError bool |
| 56 | + }{ |
| 57 | + { |
| 58 | + name: "ready immediately", |
| 59 | + client: &fakeStateClient{ |
| 60 | + responses: []*lnrpc.GetStateResponse{ |
| 61 | + activeResp, |
| 62 | + }, |
| 63 | + errs: []error{nil}, |
| 64 | + }, |
| 65 | + timeout: time.Second, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "ready after N polls", |
| 69 | + client: &fakeStateClient{ |
| 70 | + responses: []*lnrpc.GetStateResponse{ |
| 71 | + waitingResp, waitingResp, activeResp, |
| 72 | + }, |
| 73 | + errs: []error{nil, nil, nil}, |
| 74 | + }, |
| 75 | + timeout: time.Second, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "timeout without transition", |
| 79 | + client: &fakeStateClient{ |
| 80 | + responses: []*lnrpc.GetStateResponse{ |
| 81 | + waitingResp, |
| 82 | + }, |
| 83 | + errs: []error{nil}, |
| 84 | + }, |
| 85 | + timeout: 300 * time.Millisecond, |
| 86 | + expectError: true, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "timeout while erroring", |
| 90 | + client: &fakeStateClient{ |
| 91 | + responses: []*lnrpc.GetStateResponse{nil}, |
| 92 | + errs: []error{ |
| 93 | + errors.New("connection refused"), |
| 94 | + }, |
| 95 | + }, |
| 96 | + timeout: 300 * time.Millisecond, |
| 97 | + expectError: true, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + for _, tc := range testCases { |
| 102 | + tc := tc |
| 103 | + t.Run(tc.name, func(t *testing.T) { |
| 104 | + t.Parallel() |
| 105 | + |
| 106 | + err := waitForLndRPCReady( |
| 107 | + context.Background(), tc.client, tc.timeout, |
| 108 | + ) |
| 109 | + |
| 110 | + if tc.expectError { |
| 111 | + require.Error(t, err) |
| 112 | + return |
| 113 | + } |
| 114 | + require.NoError(t, err) |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
0 commit comments