1

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.

Joseph
  • 806
  • 5
  • 17
  • 34

6 Answers6

2

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.

Ashkan Aryan
  • 3,504
  • 4
  • 30
  • 44
2

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();
    }
}
Community
  • 1
  • 1
Bryan Menard
  • 13,234
  • 4
  • 31
  • 47
1

While testing server down, give any incorrect URL OR Port (Prefered). For recovery give the correct URL/Port.

Santosh
  • 17,667
  • 4
  • 54
  • 79
1

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)

/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.

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
0

To emulate the server down case, you could write a ServerAlwaysDown class extending your actual server, but throwing ServerException (HTTP 500) for every connection.

Mister Smith
  • 27,417
  • 21
  • 110
  • 193
0

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.

jasalguero
  • 4,142
  • 2
  • 31
  • 52