简体   繁体   English

如何在Java中模拟服务器停机/可用?

[英]How to simulate server down/available in java?

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. 因此,错误的url +端口是解决此问题的方法。 But I could not modify the url when the session is valid. 但是,当会话有效时,我无法修改url。 Another method is modify the hosts file to do this. 另一种方法是修改主机文件以执行此操作。 I have to face the privilege issue in Windows. 我必须面对Windows中的特权问题。

Depends on what you mean by "server down". 取决于您所说的“服务器停机”。 Possible options are: 可能的选项是:

  1. Write a fake/dummy server that can return error messages corresponding to being down for test purposes. 编写一个伪造/虚拟的服务器,该服务器可以返回与测试失败有关的错误消息。

  2. 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. 将客户端要查找的服务器的IP地址更改为不存在的IP地址,以使其认为服务器已完全关闭。

While testing server down, give any incorrect URL OR Port (Prefered). 在测试服务器停机时,提供任何不正确的URL或端口(首选)。 For recovery give the correct URL/Port. 为了恢复,请提供正确的URL /端口。

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: 让客户端上服务器的“代理”实现此接口:

public interface IServer
{
    bool foo();
}

You could create a "fake" implementation of that server and return whatever you'd like 您可以创建该服务器的“伪”实现,并返回您想要的任何内容

public class FakeOfflineServer implements IServer
{
    public bool foo()
    {
        // throw some exception here.
    }
}

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: 您还可以使用合成在测试中从上到下切换:

public bool FakeServer implements IServer
{
    private IServer offline = new FakeOfflineServer();
    private IServer online = new Server();

    public bool isUp = false;

    private IServer getServer()
    {
        return isUp ? online : offline;
    }

    public bool foo()
    {
        return getServer().foo();
    }
}

This depends where you are testing. 这取决于您在哪里进行测试。 If you're unit testing, the best option is the mocking suggested by Bryan Menard. 如果您正在进行单元测试,最好的选择是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. 对于基于Windows的系统, Fiddler很棒。 You can simulate almost anything, including delays on the requests and indeed just throwing requests away. 您可以模拟几乎所有内容,包括请求的延迟,甚至只是丢弃请求。 It does not require admin access for Windows. 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. 对于基于linux的系统,我过去使用的一种技术是使用代理服务器,或在操作系统级别削减端口。 You can do this using iptables for instance: 您可以使用iptables例如:

To deny access to a particular port (25 in this case) 拒绝访问特定端口(在这种情况下为25)

/sbin/iptables -I OUTPUT -p tcp --dest 127.0.0.1 --dport 25 -j DROP

and to allow it again: 并再次允许它:

/sbin/iptables --delete OUTPUT 1

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. 您需要使用root acces才能工作,但这确实具有不需要触摸服务器或客户端配置的优点。

为了模拟服务器宕机的情况,您可以编写一个ServerAlwaysDown类来扩展您的实际服务器,但是会为每个连接抛出ServerException(HTTP 500)。

如果要彻底使用,请始终与测试的生产环境保持最接近的状态,将客户端和服务器放在不同的计算机上并切断连接,然后恢复连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM