在 servlet 中模拟/生成 SocketTimeoutException

发布于 2024-07-29 22:45:51 字数 988 浏览 7 评论 0 原文


我正在开发一个网络项目,该项目使用:
- Java
- 码头
- 健身工具
- ETC..

我很难模拟/生成 SocketTimeoutException,更多信息如下:


The use-case-simplified:
I wrote a API that make calls to a host(WebServer). In the source-code if I receive a RemoteException and is a SocketTimeoutException, I need to retry in some other host.
All is working nice!

我们如何测试:
是一个 TDD 项目,所以我们有一堆 UT。 对于“真实”测试,我们使用上面提到的工具。 (健身)

“问题”:
我必须模拟服务器中可能发生的许多问题。 为此,我编写了一个简单的 servlet 来模拟我的 WebServer。
但对于这个用例,我需要在模拟器中模拟 SocketTimeoutException。 我正在考虑在模拟器中进行睡眠,但我认为这可能会导致客户端请求超时。


If someone have any idea or a good tip will be very nice to know!

提前致谢

I'm working a web project that use:
- Java
- Jetty
- Fitnesse tool
- etc..

I have a difficulty to simulate/generate a SocketTimeoutException, more info below:


The use-case-simplified:
I wrote a API that make calls to a host(WebServer).
In the source-code if I receive a RemoteException and is a SocketTimeoutException, I need to retry in some other host.

All is working nice!

How we test:
Is a TDD project so we have a bunch of UT. And for a 'real' test we use the tool mentioned above. (Fitnesse)

The 'problem':
I have to simulate many problems that could occur in the Server. For that I wrote a simple servlet that simulate my WebServer.
But for this use-case I need to simulate a SocketTimeoutException in my simulator. I was thinking in put a sleep in the simulator, but I think this could cause a client request timeout.


If someone have any idea or a good tip will be very nice to know!

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凡间太子 2024-08-05 22:45:51

以编程方式将套接字读取超时设置为 1 毫秒,您将始终收到 java.net.SocketTimeoutException。

华泰

Set the socket read timeout programmatically to 1 milliseconds and you will always get a java.net.SocketTimeoutException.

HTH

感受沵的脚步 2024-08-05 22:45:51

我不太清楚你的问题。 你可以尝试一下:

抛出新的SocketTimeoutException();

I'm not exactly clear on your question. Can you just try:

throw new SocketTimeoutException();

凉宸 2024-08-05 22:45:51

如果异常是基于套接字之间的“保持活动”(什么可能导致套接字超时),您可能必须在两台计算机之间建立套接字连接,然后删除它们之间的电缆(或与计算机的连接)转变)。 发生“保持活动”超时时,希望您会得到 SocketTimeoutException。

根据“保持活动”的设置(您可以通过套接字选项进行设置),可能需要几分钟到几小时(NT 大约需要 5 小时才能保持活动超时,但可以在注册表中设置) )。

如果这没有给您带来预期的异常,我不确定需要发生什么条件才能导致超时。

不妨看看这些链接,看看它们是否有帮助。

http://www.velocityreviews.com/forums/t363525- sockettimeoutException-read-timed-out.html

http://www.javakb.com/Uwe/Forum.aspx/java-programmer/29249/SocketTimeoutException-Read-timed-out

If the exception is based on the "keep alive" between sockets (what is the likely to cause the socket to timeout), you may have to make the socket connection across two computers and then remove the cable between them (or the connection to a switch). The the "keep alive" timeout occurs, hopefully you will get a SocketTimeoutException.

Depending upon the setting for the "keep alive" (You might be able to set this through socket options), it may take minutes to hours (NT was approximately 5 hour for a keep alive to time out, but it was setable in the registry).

If that does not give you the expected exception, I'm not sure what conditions need to occur to cause a timeout.

Might what to look at these links to see if they are any help.

http://www.velocityreviews.com/forums/t363525-sockettimeoutexception-read-timed-out.html

http://www.javakb.com/Uwe/Forum.aspx/java-programmer/29249/SocketTimeoutException-Read-timed-out

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文