From f658dd0d78b02f5c73ffab154430bf8847879879 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 11 Sep 2020 16:33:22 +0930 Subject: [PATCH] pytest: test connection timeout. Signed-off-by: Rusty Russell --- connectd/connectd.c | 12 ++++++++---- tests/test_connection.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/connectd/connectd.c b/connectd/connectd.c index 3f6582139..157983733 100644 --- a/connectd/connectd.c +++ b/connectd/connectd.c @@ -518,7 +518,9 @@ static struct io_plan *handshake_in_success(struct io_conn *conn, /*~ If the timer goes off, we simply free everything, which hangs up. */ static void conn_timeout(struct io_conn *conn) { - tal_free(conn); + status_debug("conn timed out"); + errno = ETIMEDOUT; + io_close(conn); } /*~ When we get a connection in we set up its network address then call @@ -1672,9 +1674,11 @@ int main(int argc, char *argv[]) * status_failed on error. */ ecdh_hsmd_setup(HSM_FD, status_failed); - /* Should never exit. */ - io_loop(NULL, NULL); - abort(); + for (;;) { + struct timer *expired; + io_loop(&daemon->timers, &expired); + timer_expired(daemon, expired); + } } /*~ Getting bored? This was a pretty simple daemon! diff --git a/tests/test_connection.py b/tests/test_connection.py index eeed39b30..02780a233 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -2634,3 +2634,20 @@ def test_nonstatic_channel(node_factory, bitcoind): l1.pay(l2, 1000) l1.rpc.close(l2.info['id']) + + +@unittest.skipIf(not DEVELOPER, "needs --dev-timeout-secs") +def test_connection_timeout(node_factory): + # l1 hears nothing back after sending INIT, should time out. + l1, l2 = node_factory.get_nodes(2, + opts=[{'dev-timeout-secs': 1, + 'disconnect': ['0WIRE_INIT', '0WIRE_INIT']}, + {}]) + + with pytest.raises(RpcError, match='timed out'): + l1.rpc.connect(l2.info['id'], 'localhost', port=l2.port) + l1.daemon.wait_for_log('conn timed out') + + with pytest.raises(RpcError, match='reset by peer'): + l2.rpc.connect(l1.info['id'], 'localhost', port=l1.port) + l1.daemon.wait_for_log('conn timed out')