1 Replies - 239 Views - Last Post: 02 May 2019 - 02:15 AM Rate Topic: -----

#1 Rachehan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-May 19

Asyncronous Server Test

Posted 01 May 2019 - 11:31 PM

Hello. I am stuck on this. I have no idea how to make the code.

I have a web-server to be tested. It works well with one thread with sequential requests. But now I need to make an asynchronous test.

So,
1) we need 10 threads that imitate 10 simultaneous connections;
2) we have four different kinds of requests a user can send;
3) one user must start with action 1 (user creation) and finish with action 4 (user deletion). Actions 2 and 3 can be chosen randomly - separately, or both of them, or none of them;
4) as a user is created by action 1, we have to store it's data somewhere until it is deleted by action 4;
5) we need to check that the response from the server matches the exact user - the one who have sent the request.
6) it all must work simultaneously and asynchronously.

I need help with this. In theory it all sounds good, but I am not skilled enough to make it.

Actually I tried to look for some examples of such tests on the internet, and other solutions or documentations. But I didn't succeed.

This is the main class.
 public static void main(String[] args) {
        Security.addProvider(new GammaTechProvider());
        String keyName; // Создание ключа
        String pass = ""; // Буфер для хранения пароля
        String template = "C=KZ;O=Template;CN=GOST_RAUTIL_USER_1Y;"; // Шаблон сертификата
        String dname; // Distinguished Name пользователя (Уникальное DN имя)
        List<String> altDN = new ArrayList<>(); // список альтернативных DN
        byte[] data = getRandomData();

        altDN.add("CN=Test org1, SERIALNUMBER=1111111111");
        altDN.add("CN=Test org2, SERIALNUMBER=2222222222");

        for (int i = 0; i < 3; i++) {
            try {
                keyName = getRandomName();
                dname = "C=KZ; O=TEST; CN= " + keyName;

                if (testPKCS10Full(keyName, pass, dname, template, altDN)) {
                    testOCSPFull(keyName, null);
                    testTSPFull(data, null);
                    MessageDigest messageDigest = MessageDigest.getInstance("GOST3411", GammaTechProvider.PROVIDER_NAME);
                    messageDigest.reset();
                    byte[] hash = messageDigest.digest(data);
                    testTSPFull(null, hash);
                    testRevFull(keyName, pass, 4, null);
                    testOCSPFull(keyName, null);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }


I'd be greatful if someone helped me.

Firstly, I create multiple threads:
    private static final int usersNumber = 10;
        for (int i = 0; i < usersNumber; i++) {
            new ThreadClientHandler("Client " + i).start();
        }


ThreadClientHandler is a class I've found on the internet, but it has no comments or explanations, thus it is quite complex for me to understand.

private static class ThreadClientHandler extends Thread {
        private Socket socket;
        private BufferedReader in; // AutoFlush
        private PrintWriter out;
        private String clientName;

        public ThreadClientHandler(String clientName) {
            this.clientName = clientName;
        }

        public void run() {
            try {
                socket = new Socket(url, 9001);
                in = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));
                out = new PrintWriter(new OutputStreamWriter(
                        socket.getOutputStream()));

                out.println(clientName);
                out.flush();
                clientHandlerConcurrentHashMap.put(clientName, this);

                Lock.unlock();
                String inLine = null;
                while (true) {
                    // its only null if something went wrong
                    inLine = in.readLine();
                    if (inLine == null || inLine.indexOf("BYE") > -1) {
                        break;
                    } else {
                        System.out.println("CLI-REC: " + inLine);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    clientHandlerConcurrentHashMap.remove(Thread.currentThread().getName());
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(clientName + " ends..");
        }
    }


The Lock class is another class used here, I suppose to block threads.


class Lock {
    private static boolean isRunning = false;

    public static synchronized void unlock() {
        if (isRunning)
            return;
        isRunning = true;
        Lock.class.notifyAll();
    }

    public static synchronized void waitThread() {
        while (!isRunning) {
            try {
                Lock.class.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            isRunning = true;
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Asyncronous Server Test

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3623
  • View blog
  • Posts: 16,656
  • Joined: 20-September 08

Re: Asyncronous Server Test

Posted 02 May 2019 - 02:15 AM

You're looking at old material. You should begin by taking an Oracle tutorial on the java.util.concurrent package
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1