如何用java模拟服务器宕机/可用?
我们的应用程序有服务器/客户端。客户端支持离线和在线工作模式。 所以我需要在服务器关闭时测试客户端,重新建立连接。
问题来了。如何模拟服务器宕机。使用代码从关闭状态切换到就绪状态,或从就绪状态切换到关闭状态。
提前致谢。
约瑟夫
更新: 实际上,我无法扩展服务器接口来响应不正确的状态。在我的测试场景中,服务器是透明的。因此,错误的 url + 端口是解决此问题的解决方案。 但是当会话有效时我无法修改url。另一种方法是修改主机文件来执行此操作。我必须面对 Windows 中的权限问题。
Our application has server/client side. The client supports both offline and online work mode.
So I need to test the client when server down, regain connective.
Question comes. How to simulate server down. Use codes to switch from down to ready, or from ready to down state.
Thanks in advance.
Joseph
update:
Actually, I could not extend the server interface to response the incorrect status. In my test scenario, the server is transparent. So incorrect url + port is a solution to do this.
But I could not modify the url when the session is valid. Another method is modify the hosts file to do this. I have to face the privilege issue in Windows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
取决于您所说的“服务器关闭”是什么意思。可能的选项是:
编写一个假/虚拟服务器,它可以返回与出于测试目的而关闭相对应的错误消息。
将客户端查找的服务器的 IP 地址更改为不存在的地址,这样客户端就会认为该服务器完全宕机。
Depends on what you mean by "server down". Possible options are:
Write a fake/dummy server that can return error messages corresponding to being down for test purposes.
Change the IP address of the server that your client looks for to a non-existing one so that it will think that the server is entirely down.
基本思想是以某种方式模拟服务器的行为。您可以使用模拟框架来执行此操作。
您还可以出于测试目的创建手动模拟。让客户端上服务器的“代理”实现此接口:
您可以创建该服务器的“假”实现并返回您想要的任何内容
这种方法允许您伪造不同的场景(没有网络连接、无效凭据等) .)
您还可以使用组合在测试中从上到下切换:
The basic idea is to mock the behavior of your server somehow. You could use mocking frameworks to do so.
You could also create manual mocks for testing purposes. Let the "proxy" of the server on the client implement this interface:
You could create a "fake" implementation of that server and return whatever you'd like
This approach allows you to fake different scenarios (no network connectivity, invalid credentials, etc.)
You could also use composition to switch from up to down in your tests:
在测试服务器关闭时,请提供任何不正确的 URL 或端口(首选)。为了恢复,请提供正确的 URL/端口。
While testing server down, give any incorrect URL OR Port (Prefered). For recovery give the correct URL/Port.
这取决于您在哪里进行测试。如果您正在进行单元测试,最好的选择是 Bryan Menard 建议的模拟。
如果您在集成或生产环境中进行测试,您实际上可以切断与服务器之间的连接。
根据您的操作系统,您可以通过多种方式执行此操作。
对于基于 Windows 的系统,Fiddler 非常棒。您可以模拟几乎任何事情,包括请求的延迟,甚至只是丢弃请求。它不需要 Windows 的管理员访问权限。
对于基于 Linux 的系统,我过去使用的一种技术是使用代理服务器,或者在操作系统级别切断端口。例如,您可以使用 iptables 来执行此操作:
拒绝访问特定端口(在本例中为 25)
并再次允许:
您需要 root 访问权限才能工作,但它确实具有您不需要 root 访问权限的优点需要触摸您的服务器或客户端配置。
This depends where you are testing. If you're unit testing, the best option is the mocking suggested by Bryan Menard.
If you're testing in an integration or production environment, You can actually cut the connection between you and the server.
Depending upon your operating system, you can do this in a number of ways.
For Windows based systems, Fiddler is fantastic. You can simulate almost anything, including delays on the requests and indeed just throwing requests away. It does not require admin access for Windows.
For linux based systems, one technique I've used in the past is to use a proxy server, or cut the port at operating system level. You can do this using iptables for instance:
To deny access to a particular port (25 in this case)
and to allow it again:
You'll need root acces for this to work, but it does have the advantage that you don't need to touch your server or client configuration.
要模拟服务器宕机的情况,您可以编写一个 ServerAlwaysDown 类来扩展您的实际服务器,但为每个连接抛出 ServerException (HTTP 500)。
To emulate the server down case, you could write a ServerAlwaysDown class extending your actual server, but throwing ServerException (HTTP 500) for every connection.
如果您想彻底使用最接近生产环境的测试,请将客户端和服务器放在不同的计算机上并切断连接,然后恢复它。
If you want to be thorough use always the closest you have to a production environment for the tests, put client and servers in different machines and cut the connection, then restore it.