lntemp+itest: refactor testNetworkConnectionTimeout

This commit is contained in:
yyforyongyu 2022-08-04 06:20:13 +08:00
parent 6b943a042c
commit 4c16e053bf
No known key found for this signature in database
GPG key ID: 9BCD95C4FF296868
4 changed files with 42 additions and 38 deletions

View file

@ -98,6 +98,18 @@ func (h *HarnessRPC) ConnectPeer(
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
// successful.
func (h *HarnessRPC) ListChannels(

View file

@ -151,4 +151,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
Name: "update channel policy fee rate accuracy",
TestFunc: testUpdateChannelPolicyFeeRateAccuracy,
},
{
Name: "connection timeout",
TestFunc: testNetworkConnectionTimeout,
},
}

View file

@ -10,21 +10,21 @@ import (
"github.com/lightningnetwork/lnd"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntemp"
"github.com/lightningnetwork/lnd/lntemp/node"
"github.com/lightningnetwork/lnd/lntest"
"github.com/stretchr/testify/require"
)
// testNetworkConnectionTimeout checks that the connectiontimeout is taking
// effect. It creates a node with a small connection timeout value, and connects
// it to a non-routable IP address.
func testNetworkConnectionTimeout(net *lntest.NetworkHarness, t *harnessTest) {
// effect. It creates a node with a small connection timeout value, and
// connects it to a non-routable IP address.
func testNetworkConnectionTimeout(ht *lntemp.HarnessTest) {
var (
ctxt, _ = context.WithTimeout(
context.Background(), defaultTimeout,
)
// testPub is a random public key for testing only.
testPub = "0332bda7da70fefe4b6ab92f53b3c4f4ee7999" +
"f312284a8e89c8670bb3f67dbee2"
// testHost is a non-routable IP address. It's used to cause a
// connection timeout.
testHost = "10.255.255.255"
@ -32,8 +32,7 @@ func testNetworkConnectionTimeout(net *lntest.NetworkHarness, t *harnessTest) {
// First, test the global timeout settings.
// Create Carol with a connection timeout of 1 millisecond.
carol := net.NewNode(t.t, "Carol", []string{"--connectiontimeout=1ms"})
defer shutdownAndAssert(net, t, carol)
carol := ht.NewNode("Carol", []string{"--connectiontimeout=1ms"})
// Try to connect Carol to a non-routable IP address, which should give
// us a timeout error.
@ -43,12 +42,27 @@ func testNetworkConnectionTimeout(net *lntest.NetworkHarness, t *harnessTest) {
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.
// Create Dave with the default timeout setting.
dave := net.NewNode(t.t, "Dave", nil)
defer shutdownAndAssert(net, t, dave)
dave := ht.NewNode("Dave", nil)
// Try to connect Dave to a non-routable IP address, using a timeout
// value of 1ms, which should give us a timeout error immediately.
@ -59,7 +73,7 @@ func testNetworkConnectionTimeout(net *lntest.NetworkHarness, t *harnessTest) {
},
Timeout: 1,
}
assertTimeoutError(ctxt, t, dave, req)
assertTimeoutError(dave, req)
}
// testReconnectAfterIPChange verifies that if a persistent inbound node changes
@ -229,28 +243,6 @@ func testReconnectAfterIPChange(net *lntest.NetworkHarness, t *harnessTest) {
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,
req *lnrpc.ConnectPeerRequest) error {

View file

@ -259,10 +259,6 @@ var allTestCases = []*testCase{
name: "maximum channel size",
test: testMaxChannelSize,
},
{
name: "connection timeout",
test: testNetworkConnectionTimeout,
},
{
name: "stateless init",
test: testStatelessInit,