我在我们的软件中有一个错误,当我收到连接超时时发生。这些错误是非常罕见的(通常是当我的连接被我们的内部网络丢弃时)。我怎样才能人为地产生这种效果来测试我们的软件呢?

如果重要,应用程序是用c++ /MFC编写的,使用CAsyncSocket类。

编辑:

我尝试使用一个不存在的主机,我得到套接字错误:

无效的参数

我的下一个尝试是使用Alexander的建议,连接到不同的端口,例如81(在我自己的服务器上)。这很有效。与断开连接完全相同(等待60秒,然后出错)。谢谢你!


当前回答

最简单的方法是使用CurrPorts删除连接。

然而,为了单元测试你的异常处理代码,也许你应该考虑抽象你的网络连接代码,并编写一个存根、mock或decorator来按需抛出异常。然后,您将能够测试应用程序错误处理逻辑,而不必实际使用网络。

其他回答

我想提醒大家注意mitmproxy。

通过配置(取自他们的示例)200:b@100:dr,您将获得一个随机断开的连接。

请连接不可路由的IP地址,如10.255.255.1。

我经常用来模拟随机连接超时的技术是使用ssh本地端口转发。

ssh -L 12345:realserver.com:80 localhost

这将把localhost:12345上的流量转发到realserver.com:80 如果你想,你也可以在你自己的本地机器上循环这个:

ssh -L 12345:localhost:8080 localhost

因此,您可以将应用程序指向本地主机和自定义端口,并且流量将被路由到目标主机:端口。然后你可以退出这个shell(你可能还需要在退出后ctrl+c shell),它会杀死转发,导致你的应用程序看到连接丢失。

Depending on what firewall software you have installed/available, you should be able to block the outgoing port and depending on how your firewall is setup it should just drop the connection request packet. No connection request, no connection, timeout ensues. This would probably work better if it was implemented at a router level (they tend to drop packets instead of sending resets, or whatever the equivalent is for the situation) but there's bound to be a software package that'd do the trick too.

最简单的方法是使用CurrPorts删除连接。

然而,为了单元测试你的异常处理代码,也许你应该考虑抽象你的网络连接代码,并编写一个存根、mock或decorator来按需抛出异常。然后,您将能够测试应用程序错误处理逻辑,而不必实际使用网络。