testFrequentConnections(true, 100);
testFrequentConnections(false, 1000);
}
private void testFrequentConnections(boolean ssl, int count) throws Exception {
final ServerSocket serverSocket = NetUtils.createServerSocket(PORT, ssl);
final AtomicInteger counter = new AtomicInteger(count);
Task serverThread = new Task() {
public void call() {
while (!stop) {
try {
Socket socket = serverSocket.accept();
// System.out.println("opened " + counter);
socket.close();
} catch (Exception e) {
// ignore
}
}
// System.out.println("stopped ");
}
};
serverThread.execute();
try {
Set<ConnectWorker> workers = new HashSet<ConnectWorker>();
for (int i = 0; i < WORKER_COUNT; i++) {
workers.add(new ConnectWorker(ssl, counter));
}
// ensure the server is started
Thread.sleep(100);
for (ConnectWorker worker : workers) {
worker.start();
}
for (ConnectWorker worker : workers) {
worker.join();
Exception e = worker.getException();
if (e != null) {
e.printStackTrace();
}
}
} finally {
try {
serverSocket.close();
} catch (Exception e) {
// ignore
}
serverThread.get();
}