Github user bhaisaab commented on a diff in the pull request: https://github.com/apache/cloudstack/pull/1493#discussion_r59915144 --- Diff: utils/src/test/java/com/cloud/utils/testcase/NioTest.java --- @@ -19,146 +19,198 @@ package com.cloud.utils.testcase; -import java.nio.channels.ClosedChannelException; -import java.util.Random; - -import junit.framework.TestCase; - -import org.apache.log4j.Logger; -import org.junit.Assert; - +import com.cloud.utils.concurrency.NamedThreadFactory; import com.cloud.utils.exception.NioConnectionException; import com.cloud.utils.nio.HandlerFactory; import com.cloud.utils.nio.Link; import com.cloud.utils.nio.NioClient; import com.cloud.utils.nio.NioServer; import com.cloud.utils.nio.Task; import com.cloud.utils.nio.Task.Type; +import org.apache.log4j.Logger; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; -/** - * - * - * - * - */ +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.channels.ClosedChannelException; +import java.nio.channels.Selector; +import java.nio.channels.SocketChannel; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +public class NioTest { -public class NioTest extends TestCase { + private static final Logger LOGGER = Logger.getLogger(NioTest.class); - private static final Logger s_logger = Logger.getLogger(NioTest.class); + final private int totalTestCount = 10; + private int completedTestCount = 0; - private NioServer _server; - private NioClient _client; + private NioServer server; + private List<NioClient> clients = new ArrayList<>(); + private List<NioClient> maliciousClients = new ArrayList<>(); - private Link _clientLink; + private ExecutorService clientExecutor = Executors.newFixedThreadPool(totalTestCount, new NamedThreadFactory("NioClientHandler"));; + private ExecutorService maliciousExecutor = Executors.newFixedThreadPool(5*totalTestCount, new NamedThreadFactory("MaliciousNioClientHandler"));; - private int _testCount; - private int _completedCount; + private Random randomGenerator = new Random(); + private byte[] testBytes; private boolean isTestsDone() { boolean result; synchronized (this) { - result = _testCount == _completedCount; + result = totalTestCount == completedTestCount; } return result; } - private void getOneMoreTest() { - synchronized (this) { - _testCount++; - } - } - private void oneMoreTestDone() { synchronized (this) { - _completedCount++; + completedTestCount++; } } - @Override + @Before public void setUp() { - s_logger.info("Test"); + LOGGER.info("Setting up Benchmark Test"); - _testCount = 0; - _completedCount = 0; - - _server = new NioServer("NioTestServer", 7777, 5, new NioTestServer()); - try { - _server.start(); - } catch (final NioConnectionException e) { - fail(e.getMessage()); - } + completedTestCount = 0; + testBytes = new byte[1000000]; + randomGenerator.nextBytes(testBytes); - _client = new NioClient("NioTestServer", "127.0.0.1", 7777, 5, new NioTestClient()); + // Server configured with one worker + server = new NioServer("NioTestServer", 0, 1, new NioTestServer()); try { - _client.start(); + server.start(); } catch (final NioConnectionException e) { - fail(e.getMessage()); + Assert.fail(e.getMessage()); } - while (_clientLink == null) { - try { - s_logger.debug("Link is not up! Waiting ..."); - Thread.sleep(1000); - } catch (final InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + // 5 malicious clients per valid client + for (int i = 0; i < totalTestCount; i++) { + for (int j = 0; j < 5; j++) { + final NioClient maliciousClient = new NioMaliciousClient("NioMaliciousTestClient-" + i, "127.0.0.1", server.getPort(), 1, new NioMaliciousTestClient()); + maliciousClients.add(maliciousClient); + maliciousExecutor.submit(new ThreadedNioClient(maliciousClient)); } + final NioClient client = new NioClient("NioTestClient-" + i, "127.0.0.1", server.getPort(), 1, new NioTestClient()); + clients.add(client); + clientExecutor.submit(new ThreadedNioClient(client)); } } - @Override + @After public void tearDown() { + stopClient(); + stopServer(); + } + + protected void stopClient() { + for (NioClient client : clients) { + client.stop(); + } + for (NioClient maliciousClient : maliciousClients) { + maliciousClient.stop(); + } + LOGGER.info("Clients stopped."); + } + + protected void stopServer() { + server.stop(); + LOGGER.info("Server stopped."); + } + + @Test + public void testConnection() { + final long currentTime = System.currentTimeMillis(); while (!isTestsDone()) { + if (System.currentTimeMillis() - currentTime > 600000) { + Assert.fail("Failed to complete test within 600s"); + } try { - s_logger.debug(_completedCount + "/" + _testCount + " tests done. Waiting for completion"); + LOGGER.debug(completedTestCount + "/" + totalTestCount + " tests done. Waiting for completion"); Thread.sleep(1000); } catch (final InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + Assert.fail(e.getMessage()); --- End diff -- @GabrielBrascher that's a cosmetics preference, I want some activity logged even second until the test passes. Also, in case the server blocks indefinitely, CIs like Travis will fail simply because there was no output.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---