socket.io java client - does not connect when used threaded

884 views Asked by At

I'm using socket.io-client java library version 1.0.0 (included it via pom.xml)

I want to do a stress/performance-test to a socket.io based server in nodejs (the server seems to work well, not of interest here).

I'm experiencing a very strange behaviour when I establish my connections doing a Thread.sleep between each new connection. If establish the connection all at once, there is no problem. The problems occur when I wait (Thead.sleep()) some milliseconds.

Always after the 5th established connection, the connections need unusual long until they are established (connected callback received). The next connection seems to wait until the first one disconnects, but thereafter several connections are established. And a lot of connections aren't established at all, no matter how long I wait. This is really strange. Why does this happen?

It does not matter, if I establish the connections asynychronously (doing each connection in a separate thread starting all threads immediately) or synchronously one after another - it works fine if I don't wait between each new socket/thread creation.

This is the complete code:

import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;

import java.net.URISyntaxException;

public class SocketStressTest
{
    private static final String SOCKET_URI = "http://test.mydomain.com/";
    public static int AMOUNT_OF_SOCKETS = 100;
    IO.Options opts;
    private int connectionCounter = 0;
    private int disConnectionCounter = 0;


    public static void main(String[] args)
    {
        new SocketStressTest().start();
    }

    private void start()
    {
        opts = new IO.Options();
        opts.forceNew = true;
        opts.reconnection = false;

        Thread thread = new Thread(new Runnable()
        {
            public void run()
            {
                initSockets();
            }
        });

        thread.start();
        System.out.println("Generated all sockets");

    }

    private void initSockets()
    {
        for (int i = 0; i < AMOUNT_OF_SOCKETS; i++)
        {
            //when I comment out sleeping and try-catch below, everything works fine
            try
            {
                Thread.sleep(400);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }

            createSocket(i);
        }
    }

    private void createSocket(final int nr)
    {

        //does not matter if I do this threaded or not:
        Thread thread = new Thread(new Runnable()
        {
            public void run()
            {
                try
                {
                    System.out.println("nr " + nr + " creating socket");
                    final Socket socket = IO.socket(SOCKET_URI, opts);
                    socket
                            .on(Socket.EVENT_CONNECT, new Emitter.Listener()
                            {

                                public void call(Object... args)
                                {
                                    connectionCounter++;
                                    System.out.println("nr " + nr + " connected id:" + socket.id() + " concounter:" + connectionCounter);
                                    if (connectionCounter == AMOUNT_OF_SOCKETS)
                                        System.out.println("===> ALL connected!");
                                }
                            })

                            .on(Socket.EVENT_DISCONNECT, new Emitter.Listener()
                            {

                                public void call(Object... args)
                                {
                                    disConnectionCounter++;
                                    System.out.println("nr " + nr + " disconnected. discounter=" + disConnectionCounter);
                                    if (disConnectionCounter == AMOUNT_OF_SOCKETS)
                                        System.out.println("<=== ALL DISCONNECTED!");
                                }
                            });

                    socket.connect();
                    System.out.println("nr " + nr + " connect called");
                }
                catch (URISyntaxException e)
                {
                    e.printStackTrace();
                }

            }
        });

        thread.start();

    }

}

This is example output(shortened), waiting 400ms inside the loop:

Generated all sockets
nr 0 creating socket
nr 0 connect called
nr 1 creating socket
nr 1 connect called
nr 0 connected id:7JLvH0hHNF0pg36mAAW3 concounter:1
nr 1 connected id:5fj3I_bFIa1JeUlXAAW4 concounter:2
nr 2 creating socket
nr 2 connect called
nr 2 connected id:RQTLEjftWna2JPuFAAW5 concounter:3
nr 3 creating socket
nr 3 connect called
nr 3 connected id:dg1xL9ddnLqwAlDsAAW6 concounter:4
nr 4 creating socket
nr 4 connect called
nr 4 connected id:y_zIvI4BXdhmEiuwAAW7 concounter:5
nr 5 creating socket
nr 5 connect called
nr 6 creating socket
nr 6 connect called
...
nr 25 creating socket
nr 25 connect called
nr 26 creating socket
nr 26 connect called
nr 0 disconnected. discounter=1
nr 1 disconnected. discounter=2
nr 5 connected id:zCoCg1qG1vJA7pezAAW8 concounter:6
nr 6 connected id:QZJA3yhcXzpRzCwgAAW9 concounter:7
nr 7 connected id:aNZMGdiY8bTeylz3AAW- concounter:8
nr 8 connected id:vitG7xSlEXO5AhnoAAW_ concounter:9
nr 9 connected id:kWirqWwxE5V4ITRiAAXA concounter:10
nr 10 connected id:gCbDdV62pzPRq71qAAXB concounter:11
nr 11 connected id:4ERh1JvC654ky96AAAXC concounter:12
nr 12 connected id:4QMQni7Ohjk0IO7XAAXD concounter:13
nr 27 creating socket
nr 27 connect called
nr 13 connected id:Xb8i-VeDaE_G9N2PAAXE concounter:14
nr 14 connected id:AccfpvKWkWoGY7TEAAXF concounter:15

example output without waiting (commented out the sleep(400) (shortened):

Generated all sockets
nr 0 creating socket
nr 1 creating socket
...
nr 97 creating socket
nr 99 creating socket
nr 24 connect called
nr 21 connect called
nr 52 connect called
...
nr 78 connect called
nr 18 connect called
nr 24 connected id:N4MyGCp4IIWwMtJhAAXV concounter:1
nr 85 connected id:0ahc2QOlGpzPqUwjAAXW concounter:2
nr 68 connected id:W280V6PpH-gUxogOAAXX concounter:3
...
nr 38 connected id:_4c8Ll0cCM_1oQYzAAY2 concounter:98
nr 11 connected id:lIyRiuxK8pmb9voAAAY3 concounter:99
nr 12 connected id:pZkW0Y5DxXgw-Sy6AAY4 concounter:100
===> ALL connected!

I tried it on 2 different machines. Same behaviour.

My goal was to adjust my stresstests with some waiting, but with this behaviour no testing is possible. Anyone any ideas? Am I doing something significantly wrong? I'm doing the tests on windows 10. I also created a jar from it and tested it on a debian system. Same behaviour. I also created a similar javascript test for doing simultanously over 500 socket-connections - works like a charm.

So is there something strange coded in this socket.io java client library that disallows more than 5 connections simultanously?

0

There are 0 answers