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 < usersNumber; i++) { new ThreadClientHandler("Client " + i).start(); } Lock.waitThread(); 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:
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; } } }
please, help somebody!