lntemp+itest: refactor testNetworkConnectionTimeout

This commit is contained in:
yyforyongyu committed 2022-11-17 21:05:33 +08:00
1 parent 6b943a042c
commit 4c16e053bf
4 files changed
+42 -38

No files matched your search

+12
View File
@@ -98,6 +98,18 @@ func (h *HarnessRPC) ConnectPeer(
return resp return resp
} }
// ConnectPeerAssertErr makes a RPC call to ConnectPeer and asserts an error
// returned.
func (h *HarnessRPC) ConnectPeerAssertErr(req *lnrpc.ConnectPeerRequest) error {
ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout)
defer cancel()
_, err := h.LN.ConnectPeer(ctxt, req)
require.Error(h, err, "expected an error from ConnectPeer")
return err
}
// ListChannels list the channels for the given node and asserts it's // ListChannels list the channels for the given node and asserts it's
// successful. // successful.
func (h *HarnessRPC) ListChannels( func (h *HarnessRPC) ListChannels(
+4
View File
@@ -151,4 +151,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
Name: "update channel policy fee rate accuracy", Name: "update channel policy fee rate accuracy",
TestFunc: testUpdateChannelPolicyFeeRateAccuracy, TestFunc: testUpdateChannelPolicyFeeRateAccuracy,
}, },
{
Name: "connection timeout",
TestFunc: testNetworkConnectionTimeout,
},
} }
+26 -34
View File
@@ -10,21 +10,21 @@ import (
"github.com/lightningnetwork/lnd" "github.com/lightningnetwork/lnd"
"github.com/lightningnetwork/lnd/lncfg" "github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntemp"
"github.com/lightningnetwork/lnd/lntemp/node"
"github.com/lightningnetwork/lnd/lntest" "github.com/lightningnetwork/lnd/lntest"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// testNetworkConnectionTimeout checks that the connectiontimeout is taking // testNetworkConnectionTimeout checks that the connectiontimeout is taking
// effect. It creates a node with a small connection timeout value, and connects // effect. It creates a node with a small connection timeout value, and
// it to a non-routable IP address. // connects it to a non-routable IP address.
func testNetworkConnectionTimeout(net *lntest.NetworkHarness, t *harnessTest) { func testNetworkConnectionTimeout(ht *lntemp.HarnessTest) {
var ( var (
ctxt, _ = context.WithTimeout(
context.Background(), defaultTimeout,
)
// testPub is a random public key for testing only. // testPub is a random public key for testing only.
testPub = "0332bda7da70fefe4b6ab92f53b3c4f4ee7999" + testPub = "0332bda7da70fefe4b6ab92f53b3c4f4ee7999" +
"f312284a8e89c8670bb3f67dbee2" "f312284a8e89c8670bb3f67dbee2"
// testHost is a non-routable IP address. It's used to cause a // testHost is a non-routable IP address. It's used to cause a
// connection timeout. // connection timeout.
testHost = "10.255.255.255" testHost = "10.255.255.255"
@@ -32,8 +32,7 @@ func testNetworkConnectionTimeout(net *lntest.NetworkHarness, t *harnessTest) {
// First, test the global timeout settings. // First, test the global timeout settings.
// Create Carol with a connection timeout of 1 millisecond. // Create Carol with a connection timeout of 1 millisecond.
carol := net.NewNode(t.t, "Carol", []string{"--connectiontimeout=1ms"}) carol := ht.NewNode("Carol", []string{"--connectiontimeout=1ms"})
defer shutdownAndAssert(net, t, carol)
// Try to connect Carol to a non-routable IP address, which should give // Try to connect Carol to a non-routable IP address, which should give
// us a timeout error. // us a timeout error.
@@ -43,12 +42,27 @@ func testNetworkConnectionTimeout(net *lntest.NetworkHarness, t *harnessTest) {
Host: testHost, Host: testHost,
}, },
} }
assertTimeoutError(ctxt, t, carol, req)
// assertTimeoutError asserts that a connection timeout error is
// raised. A context with a default timeout is used to make the
// request. If our customized connection timeout is less than the
// default, we won't see the request context times out, instead a
// network connection timeout will be returned.
assertTimeoutError := func(hn *node.HarnessNode,
req *lnrpc.ConnectPeerRequest) {
err := hn.RPC.ConnectPeerAssertErr(req)
// Check that the network returns a timeout error.
require.Containsf(ht, err.Error(), "i/o timeout",
"expected to get a timeout error, instead got: %v", err)
}
assertTimeoutError(carol, req)
// Second, test timeout on the connect peer request. // Second, test timeout on the connect peer request.
// Create Dave with the default timeout setting. // Create Dave with the default timeout setting.
dave := net.NewNode(t.t, "Dave", nil) dave := ht.NewNode("Dave", nil)
defer shutdownAndAssert(net, t, dave)
// Try to connect Dave to a non-routable IP address, using a timeout // Try to connect Dave to a non-routable IP address, using a timeout
// value of 1ms, which should give us a timeout error immediately. // value of 1ms, which should give us a timeout error immediately.
@@ -59,7 +73,7 @@ func testNetworkConnectionTimeout(net *lntest.NetworkHarness, t *harnessTest) {
}, },
Timeout: 1, Timeout: 1,
} }
assertTimeoutError(ctxt, t, dave, req) assertTimeoutError(dave, req)
} }
// testReconnectAfterIPChange verifies that if a persistent inbound node changes // testReconnectAfterIPChange verifies that if a persistent inbound node changes
@@ -229,28 +243,6 @@ func testReconnectAfterIPChange(net *lntest.NetworkHarness, t *harnessTest) {
assertConnected(t, dave, charlie) assertConnected(t, dave, charlie)
} }
// assertTimeoutError asserts that a connection timeout error is raised. A
// context with a default timeout is used to make the request. If our customized
// connection timeout is less than the default, we won't see the request context
// times out, instead a network connection timeout will be returned.
func assertTimeoutError(ctxt context.Context, t *harnessTest,
node *lntest.HarnessNode, req *lnrpc.ConnectPeerRequest) {
t.t.Helper()
err := connect(ctxt, node, req)
// a DeadlineExceeded error will appear in the context if the above
// ctxtTimeout value is reached.
require.NoError(t.t, ctxt.Err(), "context time out")
// Check that the network returns a timeout error.
require.Containsf(
t.t, err.Error(), "i/o timeout",
"expected to get a timeout error, instead got: %v", err,
)
}
func connect(ctxt context.Context, node *lntest.HarnessNode, func connect(ctxt context.Context, node *lntest.HarnessNode,
req *lnrpc.ConnectPeerRequest) error { req *lnrpc.ConnectPeerRequest) error {
-4
View File
@@ -259,10 +259,6 @@ var allTestCases = []*testCase{
name: "maximum channel size", name: "maximum channel size",
test: testMaxChannelSize, test: testMaxChannelSize,
}, },
{
name: "connection timeout",
test: testNetworkConnectionTimeout,
},
{ {
name: "stateless init", name: "stateless init",
test: testStatelessInit, test: testStatelessInit,