-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package address | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"github.com/btcsuite/btcd/btcec/v2" | ||
"github.com/btcsuite/btcd/btcutil" | ||
"github.com/lightninglabs/loop/loopdb" | ||
"github.com/lightninglabs/loop/swapserverrpc" | ||
"github.com/lightninglabs/loop/test" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc" | ||
"testing" | ||
) | ||
|
||
var ( | ||
defaultAddresssId = mustDecodeID("17cecc61ab4aafebdc0542dabdae0d0cb8907ec1c9c8ae387bc5a3309ca8b600") | ||
|
||
defaultPubkeyBytes, _ = hex.DecodeString("021c97a90a411ff2b10dc2a8e32de2f29d2fa49d41bfbb52bd416e460db0747d0d") | ||
|
||
defaultPubkey, _ = btcec.ParsePubKey(defaultPubkeyBytes) | ||
|
||
defaultValue = btcutil.Amount(100) | ||
|
||
defaultExpiry = uint32(100) | ||
) | ||
|
||
type mockStaticAddressClient struct { | ||
mock.Mock | ||
} | ||
|
||
type mockSwapServerClient struct { | ||
mock.Mock | ||
} | ||
|
||
type mockSwapClient struct { | ||
mock.Mock | ||
Server *mockSwapServerClient | ||
} | ||
|
||
func (m *mockStaticAddressClient) ServerNewAddress(ctx context.Context, | ||
in *swapserverrpc.ServerNewAddressRequest, opts ...grpc.CallOption) ( | ||
*swapserverrpc.ServerNewAddressResponse, error) { | ||
|
||
args := m.Called(ctx, in, opts) | ||
return args.Get(0).(*swapserverrpc.ServerNewAddressResponse), | ||
args.Error(1) | ||
} | ||
|
||
func (m *mockSwapServerClient) FetchL402(ctx context.Context, | ||
in *swapserverrpc.FetchL402Request, | ||
opts ...grpc.CallOption) (*swapserverrpc.FetchL402Response, error) { | ||
|
||
args := m.Called(ctx, in, opts) | ||
|
||
return args.Get(0).(*swapserverrpc.FetchL402Response), | ||
args.Error(1) | ||
} | ||
|
||
func TestManager(t *testing.T) { | ||
ctxb, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
testContext := newManagerTestContext(t) | ||
|
||
// Start the manager. | ||
go func() { | ||
err := testContext.manager.Run(ctxb) | ||
require.NoError(t, err) | ||
}() | ||
|
||
// Create a new reservation. | ||
_, err := testContext.manager.NewAddress(ctxb) | ||
require.NoError(t, err) | ||
} | ||
|
||
// ManagerTestContext is a helper struct that contains all the necessary | ||
// components to test the reservation manager. | ||
type ManagerTestContext struct { | ||
manager *Manager | ||
context test.Context | ||
mockLnd *test.LndMockServices | ||
reservationNotificationChan chan *swapserverrpc.ServerReservationNotification | ||
mockStaticAddressClient *mockStaticAddressClient | ||
} | ||
|
||
// newManagerTestContext creates a new test context for the reservation manager. | ||
func newManagerTestContext(t *testing.T) *ManagerTestContext { | ||
mockLnd := test.NewMockLnd() | ||
lndContext := test.NewContext(t, mockLnd) | ||
|
||
dbFixture := loopdb.NewTestDB(t) | ||
|
||
store := NewSqlStore(dbFixture.BaseDB) | ||
|
||
mockStaticAddressClient := new(mockStaticAddressClient) | ||
mockSwapClient := new(mockSwapClient) | ||
|
||
sendChan := make(chan *swapserverrpc.ServerReservationNotification) | ||
|
||
mockStaticAddressClient.On( | ||
"ReservationNotificationStream", mock.Anything, mock.Anything, | ||
mock.Anything, | ||
).Return( | ||
&dummyReservationNotificationServer{ | ||
SendChan: sendChan, | ||
}, nil, | ||
) | ||
|
||
mockStaticAddressClient.On( | ||
"OpenReservation", mock.Anything, mock.Anything, mock.Anything, | ||
).Return( | ||
&swapserverrpc.ServerOpenReservationResponse{}, nil, | ||
) | ||
|
||
cfg := &ManagerConfig{ | ||
SwapClient: mockSwapClient, | ||
Check failure on line 118 in staticaddr/address/manager_test.go GitHub Actions / build and lint code
|
||
Store: store, | ||
WalletKit: mockLnd.WalletKit, | ||
ChainParams: mockLnd.ChainParams, | ||
AddressClient: mockStaticAddressClient, | ||
} | ||
|
||
manager := NewManager(cfg) | ||
|
||
return &ManagerTestContext{ | ||
manager: manager, | ||
context: lndContext, | ||
mockLnd: mockLnd, | ||
mockStaticAddressClient: mockStaticAddressClient, | ||
reservationNotificationChan: sendChan, | ||
} | ||
} | ||
|
||
type dummyReservationNotificationServer struct { | ||
grpc.ClientStream | ||
|
||
// SendChan is the channel that is used to send notifications. | ||
SendChan chan *swapserverrpc.ServerReservationNotification | ||
} | ||
|
||
func (d *dummyReservationNotificationServer) Recv() ( | ||
*swapserverrpc.ServerReservationNotification, error) { | ||
|
||
return <-d.SendChan, nil | ||
} | ||
|
||
func mustDecodeID(id string) []byte { | ||
bytes, err := hex.DecodeString(id) | ||
if err != nil { | ||
panic(err) | ||
} | ||
var decoded []byte | ||
copy(decoded[:], bytes) | ||
return decoded | ||
} |