From 68e36768d087941f41d7aead271382172df0ef30 Mon Sep 17 00:00:00 2001 From: Eric <5089238+emizzle@users.noreply.github.com> Date: Wed, 2 Jul 2025 13:15:42 +1000 Subject: [PATCH] Add test for connection timed out --- testmodule/providers/jsonrpc/testErrors.nim | 25 ++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/testmodule/providers/jsonrpc/testErrors.nim b/testmodule/providers/jsonrpc/testErrors.nim index 680b606..6940dbf 100644 --- a/testmodule/providers/jsonrpc/testErrors.nim +++ b/testmodule/providers/jsonrpc/testErrors.nim @@ -1,9 +1,11 @@ import std/importutils import std/sequtils import std/typetraits +import std/net import stew/byteutils import pkg/asynctest/chronos/unittest +import pkg/chronos/apps/http/httpclient import pkg/serde import pkg/questionable import pkg/ethers/providers/jsonrpc @@ -164,6 +166,23 @@ suite "Network errors - HTTP": expect RpcNetworkError: discard await provider.send("eth_accounts") - # We don't need to recreate each and every possible exception condition (eg - # "Connection timed out"), as they are all wrapped up in RpcPostError and - # converted. The tests above cover this conversion. + test "raises RpcNetworkError when connection times out": + privateAccess(JsonRpcProvider) + privateAccess(RpcHttpClient) + let rpcClient = await provider.client + let client: RpcHttpClient = (RpcHttpClient)(rpcClient) + client.httpSession.connectTimeout = 10.millis + + let blockingSocket = newSocket() + blockingSocket.setSockOpt(OptReuseAddr, true) + blockingSocket.bindAddr(Port(9999)) + + await client.connect("http://localhost:9999") + + expect RpcNetworkError: + # msg: Failed to send POST Request with JSON-RPC: Connection timed out + discard await provider.send("eth_accounts") + + # We don't need to recreate each and every possible exception condition, as + # they are all wrapped up in RpcPostError and converted to RpcNetworkError. + # The tests above cover this conversion.