* This class is an example of client.
- *
- * On a bi-core Centrino2 vPro: 18/s in 50 sequential, 30/s in 10 threads with 50 sequential
- * On a quad-core i7: 29/s in 50 sequential, 187/s in 10 threads with 50 sequential
+ *
+ * On a bi-core Centrino2 vPro: 18/s in 50 sequential, 30/s in 10 threads with
+ * 50 sequential
On a quad-core i7: 29/s
+ * in 50 sequential, 187/s in 10 threads with 50 sequential
*/
public class LocalExecClientTest extends Thread {
+ static int nit = 20;
+ static int nth = 4;
+ static String command = "echo";
+ static int port = 9999;
+ static InetSocketAddress address;
+ static LocalExecResult result;
+ static int ok = 0;
+ static int ko = 0;
+ static AtomicInteger atomicInteger = new AtomicInteger();
+ static EventLoopGroup workerGroup = new NioEventLoopGroup();
+ static EventExecutorGroup executor =
+ new DefaultEventExecutorGroup(DetectionUtils.numberThreads(),
+ new WaarpThreadFactory(
+ "LocalExecServer"));
+ // Configure the client.
+ static Bootstrap bootstrap;
+ // Configure the pipeline factory.
+ static LocalExecClientInitializer localExecClientInitializer;
+ private Channel channel;
- static int nit = 50;
- static int nth = 10;
- static String command = "/opt/R66/testexec.sh";
- static int port = 9999;
- static InetSocketAddress address;
+ {
+ DetectionUtils.setJunit(true);
+ }
- static LocalExecResult result;
- static int ok = 0;
- static int ko = 0;
- static AtomicInteger atomicInteger = new AtomicInteger();
+ /**
+ * Simple constructor
+ */
+ public LocalExecClientTest() {
+ }
- static EventLoopGroup workerGroup = new NioEventLoopGroup();
- // Configure the client.
- static Bootstrap bootstrap;
- // Configure the pipeline factory.
- static LocalExecClientInitializer localExecClientInitializer;
+ @Test
+ public void testClient() throws Exception {
+ WaarpLoggerFactory.setDefaultFactory(new WaarpSlf4JLoggerFactory(
+ WaarpLogLevel.WARN));
+ DetectionUtils.setJunit(true);
+ InetAddress addr;
+ byte[] loop = { 127, 0, 0, 1 };
+ try {
+ addr = InetAddress.getByAddress(loop);
+ } catch (UnknownHostException e) {
+ return;
+ }
+ address = new InetSocketAddress(addr, port);
- /**
- * Test & example main
- *
- * @param args
- * ignored
- * @throws Exception
- */
- public static void main(String[] aregs) throws Exception {
- WaarpLoggerFactory.setDefaultFactory(new WaarpSlf4JLoggerFactory(
- WaarpLogLevel.WARN));
- InetAddress addr;
- byte[] loop = { 127, 0, 0, 1 };
- try {
- addr = InetAddress.getByAddress(loop);
- } catch (UnknownHostException e) {
- return;
- }
- address = new InetSocketAddress(addr, port);
+ // configure the server
+ ServerBootstrap bootstrapServer = new ServerBootstrap();
+ WaarpNettyUtil.setServerBootstrap(bootstrapServer, workerGroup, 1000);
+
+ // Configure the pipeline factory.
+ LocalExecServerInitializer localExecServerInitializer =
+ new LocalExecServerInitializer(
+ LocalExecDefaultResult.MAXWAITPROCESS, executor);
+ bootstrapServer.childHandler(localExecServerInitializer);
- // Configure the client.
- bootstrap = new Bootstrap();
- WaarpNettyUtil.setBootstrap(bootstrap, workerGroup, 30000);
- // Configure the pipeline factory.
- localExecClientInitializer = new LocalExecClientInitializer();
- bootstrap.handler(localExecClientInitializer);
+ // Bind and start to accept incoming connections only on local address.
+ ChannelFuture future =
+ bootstrapServer.bind(new InetSocketAddress(addr, port));
- try {
- // Parse options.
- LocalExecClientTest client = new LocalExecClientTest();
- // run once
- long first = System.currentTimeMillis();
- if (client.connect()) {
- client.runOnce();
- client.disconnect();
- }
- long second = System.currentTimeMillis();
- // print time for one exec
- System.err.println("1=Total time in ms: " + (second - first) + " or " + (1 * 1000 / (second - first))
- + " exec/s");
- System.err.println("Result: " + ok + ":" + ko);
- ok = 0;
- ko = 0;
- // Now run multiple within one thread
- first = System.currentTimeMillis();
- for (int i = 0; i < nit; i++) {
- if (client.connect()) {
- client.runOnce();
- client.disconnect();
- }
- }
- second = System.currentTimeMillis();
- // print time for one exec
- System.err.println(nit + "=Total time in ms: " + (second - first) + " or "
- + (nit * 1000 / (second - first)) + " exec/s");
- System.err.println("Result: " + ok + ":" + ko);
- ok = 0;
- ko = 0;
- // Now run multiple within multiple threads
- // Create multiple threads
- ExecutorService executorService = Executors.newFixedThreadPool(nth);
- first = System.currentTimeMillis();
- // Starts all thread with a default number of execution
- for (int i = 0; i < nth; i++) {
- executorService.submit(new LocalExecClientTest());
- }
- Thread.sleep(500);
- executorService.shutdown();
- while (!executorService.awaitTermination(200, TimeUnit.MILLISECONDS)) {
- Thread.sleep(50);
- }
- second = System.currentTimeMillis();
+ // Configure the client.
+ bootstrap = new Bootstrap();
+ WaarpNettyUtil.setBootstrap(bootstrap, workerGroup, 1000);
+ // Configure the pipeline factory.
+ localExecClientInitializer = new LocalExecClientInitializer();
+ bootstrap.handler(localExecClientInitializer);
- // print time for one exec
- System.err.println((nit * nth) + "=Total time in ms: " + (second - first) + " or "
- + (nit * nth * 1000 / (second - first)) + " exec/s");
- System.err.println("Result: " + ok + ":" + ko);
- ok = 0;
- ko = 0;
+ // Wait for the server
+ future.sync();
- // run once
- first = System.currentTimeMillis();
- if (client.connect()) {
- client.runFinal();
- client.disconnect();
- }
- second = System.currentTimeMillis();
- // print time for one exec
- System.err.println("1=Total time in ms: " + (second - first) + " or " + (1 * 1000 / (second - first))
- + " exec/s");
- System.err.println("Result: " + ok + ":" + ko);
- ok = 0;
- ko = 0;
- } finally {
- // Shut down all thread pools to exit.
- workerGroup.shutdownGracefully();
- localExecClientInitializer.releaseResources();
+ try {
+ // Parse options.
+ LocalExecClientTest client = new LocalExecClientTest();
+ // run once
+ long first = System.currentTimeMillis();
+ if (client.connect()) {
+ client.runOnce();
+ client.disconnect();
+ }
+ long second = System.currentTimeMillis();
+ // print time for one exec
+ System.err.println("1=Total time in ms: " + (second - first) + " or " +
+ (1 * 1000 / (second - first))
+ + " exec/s");
+ System.err.println("Result: " + ok + ":" + ko);
+ assertEquals(0, ko);
+ ok = 0;
+ ko = 0;
+ // Now run multiple within one thread
+ first = System.currentTimeMillis();
+ for (int i = 0; i < nit; i++) {
+ if (client.connect()) {
+ client.runOnce();
+ client.disconnect();
}
- }
+ }
+ second = System.currentTimeMillis();
+ // print time for one exec
+ System.err.println(nit + "=Total time in ms: " + (second - first) + " or "
+ + (nit * 1000 / (second - first)) + " exec/s");
+ System.err.println("Result: " + ok + ":" + ko);
+ assertEquals(0, ko);
+ ok = 0;
+ ko = 0;
+ // Now run multiple within multiple threads
+ // Create multiple threads
+ ExecutorService executorService = Executors.newFixedThreadPool(nth);
+ first = System.currentTimeMillis();
+ // Starts all thread with a default number of execution
+ for (int i = 0; i < nth; i++) {
+ executorService.submit(new LocalExecClientTest());
+ }
+ Thread.sleep(500);
+ executorService.shutdown();
+ while (!executorService.awaitTermination(200, TimeUnit.MILLISECONDS)) {
+ Thread.sleep(50);
+ }
+ second = System.currentTimeMillis();
- /**
- * Simple constructor
- */
- public LocalExecClientTest() {
+ // print time for one exec
+ System.err.println(
+ (nit * nth) + "=Total time in ms: " + (second - first) + " or "
+ + (nit * nth * 1000 / (second - first)) + " exec/s");
+ System.err.println("Result: " + ok + ":" + ko);
+ assertEquals(0, ko);
+ ok = 0;
+ ko = 0;
+
+ // run once
+ first = System.currentTimeMillis();
+ if (client.connect()) {
+ client.runFinal();
+ client.disconnect();
+ }
+ second = System.currentTimeMillis();
+ // print time for one exec
+ System.err.println("1=Total time in ms: " + (second - first) + " or " +
+ (1 * 1000 / (second - first))
+ + " exec/s");
+ System.err.println("Result: " + ok + ":" + ko);
+ assertEquals(0, ko);
+ ok = 0;
+ ko = 0;
+ } finally {
+ future.channel().close();
+ // Shut down all thread pools to exit.
+ localExecClientInitializer.releaseResources();
+ localExecServerInitializer.releaseResources();
+ // Shut down all thread pools to exit.
+ workerGroup.shutdownGracefully();
+ localExecClientInitializer.releaseResources();
+ LocalExecServerHandler.junitSetNotShutdown();
}
+ }
- private Channel channel;
+ /**
+ * Connect to the Server
+ */
+ private boolean connect() {
+ // Start the connection attempt.
+ ChannelFuture future = bootstrap.connect(address);
- /**
- * Run method for thread
- */
- public void run() {
- if (connect()) {
- for (int i = 0; i < nit; i++) {
- this.runOnce();
- }
- disconnect();
- }
+ // Wait until the connection attempt succeeds or fails.
+ try {
+ channel = future.await().sync().channel();
+ } catch (InterruptedException e) {
}
+ if (!future.isSuccess()) {
+ System.err.println("Client Not Connected");
+ future.cause().printStackTrace();
+ fail("Cannot connect");
+ return false;
+ }
+ return true;
+ }
- /**
- * Connect to the Server
- */
- private boolean connect() {
- // Start the connection attempt.
- ChannelFuture future = bootstrap.connect(address);
-
- // Wait until the connection attempt succeeds or fails.
- try {
- channel = future.await().sync().channel();
- } catch (InterruptedException e) {
- }
- if (!future.isSuccess()) {
- System.err.println("Client Not Connected");
- future.cause().printStackTrace();
- return false;
- }
- return true;
+ /**
+ * Run method both for not threaded execution and threaded execution
+ */
+ public void runOnce() {
+ // Initialize the command context
+ LocalExecClientHandler clientHandler =
+ (LocalExecClientHandler) channel.pipeline().last();
+ // Command to execute
+ String line = command + " " + atomicInteger.incrementAndGet();
+ clientHandler.initExecClient(0, line);
+ // Wait for the end of the exec command
+ LocalExecResult localExecResult = clientHandler.waitFor(10000);
+ int status = localExecResult.getStatus();
+ if (status < 0) {
+ System.err.println(line + " Status: " + status + "\tResult: " +
+ localExecResult.getResult());
+ ko++;
+ } else {
+ ok++;
+ result = localExecResult;
}
+ }
- /**
- * Disconnect from the server
- */
- private void disconnect() {
- // Close the connection. Make sure the close operation ends because
- // all I/O operations are asynchronous in Netty.
- try {
- ChannelFuture closeFuture = WaarpSslUtility.closingSslChannel(channel);
- closeFuture.await(30000);
- } catch (InterruptedException e) {
- }
+ /**
+ * Disconnect from the server
+ */
+ private void disconnect() {
+ // Close the connection. Make sure the close operation ends because
+ // all I/O operations are asynchronous in Netty.
+ try {
+ ChannelFuture closeFuture = WaarpSslUtility.closingSslChannel(channel);
+ closeFuture.await(30000);
+ } catch (InterruptedException e) {
}
+ }
- /**
- * Run method both for not threaded execution and threaded execution
- */
- public void runOnce() {
- // Initialize the command context
- LocalExecClientHandler clientHandler =
- (LocalExecClientHandler) channel.pipeline().last();
- // Command to execute
- String line = command + " " + atomicInteger.incrementAndGet();
- clientHandler.initExecClient(0, line);
- // Wait for the end of the exec command
- LocalExecResult localExecResult = clientHandler.waitFor(10000);
- int status = localExecResult.getStatus();
- if (status < 0) {
- System.err.println(line + " Status: " + status + "\tResult: " +
- localExecResult.getResult());
- ko++;
- } else {
- ok++;
- result = localExecResult;
- }
+ /**
+ * Run method for closing Server
+ */
+ private void runFinal() {
+ // Initialize the command context
+ LocalExecClientHandler clientHandler =
+ (LocalExecClientHandler) channel.pipeline().last();
+ // Command to execute
+ clientHandler.initExecClient(-1000, "stop");
+ // Wait for the end of the exec command
+ LocalExecResult localExecResult = clientHandler.waitFor(10000);
+ int status = localExecResult.getStatus();
+ if (status < 0) {
+ System.err.println("Shutdown Status: " + status + "\nResult: " +
+ localExecResult.getResult());
+ ok++;
+ } else {
+ ok++;
+ result = localExecResult;
}
+ }
- /**
- * Run method for closing Server
- */
- private void runFinal() {
- // Initialize the command context
- LocalExecClientHandler clientHandler =
- (LocalExecClientHandler) channel.pipeline().last();
- // Command to execute
- clientHandler.initExecClient(-1000, "stop");
- // Wait for the end of the exec command
- LocalExecResult localExecResult = clientHandler.waitFor(10000);
- int status = localExecResult.getStatus();
- if (status < 0) {
- System.err.println("Shutdown Status: " + status + "\nResult: " +
- localExecResult.getResult());
- ko++;
- } else {
- ok++;
- result = localExecResult;
- }
+ /**
+ * Run method for thread
+ */
+ public void run() {
+ if (connect()) {
+ for (int i = 0; i < nit; i++) {
+ this.runOnce();
+ }
+ disconnect();
}
+ }
}
diff --git a/src/test/java/org/waarp/commandexec/client/test/package-info.java b/src/test/java/org/waarp/commandexec/client/test/package-info.java
index f18b6f7..f3ba47a 100644
--- a/src/test/java/org/waarp/commandexec/client/test/package-info.java
+++ b/src/test/java/org/waarp/commandexec/client/test/package-info.java
@@ -1,6 +1,24 @@
-/**
- * Classes implementing LocalExec Client example without SSL link
+/*******************************************************************************
+ * This file is part of Waarp Project (named also Waarp or GG).
+ *
+ * Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
+ * tags. See the COPYRIGHT.txt in the distribution for a full listing of
+ * individual contributors.
*
+ * All Waarp Project is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or (at your
+ * option) any later version.
*
+ * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * Waarp . If not, see
* This class is an example of client.
- *
- * No client authentication On a bi-core Centrino2 vPro: 5/s in 50 sequential, 29/s in 10 threads with 50 sequential
- * With client authentication On a bi-core Centrino2 vPro: 3/s in 50 sequential, 27/s in 10 threads with 50 sequential
- * No client authentication On a quad-core i7: 20/s in 50 sequential, 178/s in 10 threads with 50 sequential
- * With client authentication On a quad-core i7: 17/s in 50 sequential, 176/s in 10 threads with 50 sequential
- *
+ *
+ * No client authentication On a bi-core Centrino2 vPro: 5/s in 50 sequential,
+ * 29/s in 10 threads with 50 sequential
+ * With client authentication On a bi-core Centrino2 vPro: 3/s in 50 sequential,
+ * 27/s in 10 threads with 50
+ * sequential
No client authentication On a quad-core i7: 20/s in 50
+ * sequential, 178/s in 10 threads with 50
+ * sequential
With client authentication On a quad-core i7: 17/s in 50
+ * sequential, 176/s in 10 threads with 50
+ * sequential
*/
public class LocalExecSslClientTest extends Thread {
+ static int nit = 20;
+ static int nth = 4;
+ static String command = "echo";
+ static int port = 9999;
+ static InetSocketAddress address;
+ static LocalExecResult result;
+ static int ok = 0;
+ static int ko = 0;
+ static AtomicInteger atomicInteger = new AtomicInteger();
+ static EventLoopGroup workerGroup = new NioEventLoopGroup();
+ static EventExecutorGroup executor =
+ new DefaultEventExecutorGroup(DetectionUtils.numberThreads(),
+ new WaarpThreadFactory("LocalExecServer"));
+ // Configure the client.
+ static Bootstrap bootstrap;
+ // Configure the pipeline factory.
+ static LocalExecSslClientInitializer localExecClientInitializer;
+ private Channel channel;
- static int nit = 50;
- static int nth = 10;
- static String command = "/opt/R66/testexec.sh";
- static int port = 9999;
- static InetSocketAddress address;
- // with client authentication
- static String keyStoreFilename = "/opt/R66/AllJarsWaarpR66-2.4.28-2/config/certs/testclient2.jks";
- // without client authentication
- // static String keyStoreFilename = null;
- static String keyStorePasswd = "testclient2";
- static String keyPasswd = "client2";
- static String keyTrustStoreFilename = "/opt/R66/AllJarsWaarpR66-2.4.28-2/config/certs/testclient.jks";
- static String keyTrustStorePasswd = "testclient";
- static LocalExecResult result;
+ {
+ DetectionUtils.setJunit(true);
+ }
- static int ok = 0;
- static int ko = 0;
- static AtomicInteger atomicInteger = new AtomicInteger();
+ /**
+ * Simple constructor
+ */
+ public LocalExecSslClientTest() {
+ }
- static EventLoopGroup workerGroup = new NioEventLoopGroup();
- static EventExecutorGroup executor = new DefaultEventExecutorGroup(DetectionUtils.numberThreads(),
- new WaarpThreadFactory("LocalExecServer"));
+ @Test
+ public void testSslClient() throws Exception {
+ WaarpLoggerFactory.setDefaultFactory(new WaarpSlf4JLoggerFactory(
+ WaarpLogLevel.WARN));
+ DetectionUtils.setJunit(true);
+ InetAddress addr;
+ byte[] loop = { 127, 0, 0, 1 };
+ try {
+ addr = InetAddress.getByAddress(loop);
+ } catch (UnknownHostException e) {
+ return;
+ }
+ address = new InetSocketAddress(addr, port);
// Configure the client.
- static Bootstrap bootstrap;
+ bootstrap = new Bootstrap();
+ WaarpNettyUtil.setBootstrap(bootstrap, workerGroup, 1000);
// Configure the pipeline factory.
- static LocalExecSslClientInitializer localExecClientInitializer;
+ // First create the SSL part
+ // Load the KeyStore (No certificates)
+ ClassLoader classLoader = LocalExecSslClientTest.class.getClassLoader();
+ String keyStoreFilename = "certs/testsslnocert.jks";
+ URL url = classLoader.getResource(keyStoreFilename);
+ assertNotNull(url);
+ File file = new File(url.getFile());
+ assertTrue("File Should exists", file.exists());
+ String keyStorePasswd = "testsslnocert";
+ String keyPassword = "testalias";
+ WaarpSecureKeyStore waarpSecureKeyStore =
+ new WaarpSecureKeyStore(file.getAbsolutePath(), keyStorePasswd,
+ keyPassword);
+ WaarpSecureKeyStore waarpSecureKeyStoreClient =
+ new WaarpSecureKeyStore(file.getAbsolutePath(), keyStorePasswd,
+ keyPassword);
+ // Include certificates
+ String trustStoreFilename = "certs/testcert.jks";
+ File file2 =
+ new File(classLoader.getResource(trustStoreFilename).getFile());
+ assertTrue("File2 Should exists", file2.exists());
+ String trustStorePasswd = "testcert";
+ waarpSecureKeyStore
+ .initTrustStore(file2.getAbsolutePath(), trustStorePasswd, true);
- /**
- * Test & example main
- *
- * @param args
- * ignored
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- WaarpLoggerFactory.setDefaultFactory(new WaarpSlf4JLoggerFactory(
- WaarpLogLevel.WARN));
- InetAddress addr;
- byte[] loop = { 127, 0, 0, 1 };
- try {
- addr = InetAddress.getByAddress(loop);
- } catch (UnknownHostException e) {
- return;
- }
- address = new InetSocketAddress(addr, port);
- // Configure the client.
- bootstrap = new Bootstrap();
- WaarpNettyUtil.setBootstrap(bootstrap, workerGroup, 30000);
- // Configure the pipeline factory.
- // First create the SSL part
- WaarpSecureKeyStore WaarpSecureKeyStore;
- // For empty KeyStore
- if (keyStoreFilename == null) {
- WaarpSecureKeyStore =
- new WaarpSecureKeyStore(keyStorePasswd, keyPasswd);
- } else {
- WaarpSecureKeyStore =
- new WaarpSecureKeyStore(keyStoreFilename, keyStorePasswd, keyPasswd);
- }
+ // configure the server
+ ServerBootstrap bootstrapServer = new ServerBootstrap();
+ WaarpNettyUtil.setServerBootstrap(bootstrapServer, workerGroup, 1000);
- if (keyTrustStoreFilename != null) {
- // Load the client TrustStore
- WaarpSecureKeyStore.initTrustStore(keyTrustStoreFilename, keyTrustStorePasswd, false);
- } else {
- WaarpSecureKeyStore.initEmptyTrustStore();
- }
- WaarpSslContextFactory waarpSslContextFactory = new WaarpSslContextFactory(WaarpSecureKeyStore, false);
- localExecClientInitializer =
- new LocalExecSslClientInitializer(waarpSslContextFactory);
- bootstrap.handler(localExecClientInitializer);
+ // Configure the pipeline factory.
+ WaarpSslContextFactory waarpSslContextFactoryServer =
+ new WaarpSslContextFactory(waarpSecureKeyStore, true);
+ LocalExecSslServerInitializer localExecServerInitializer =
+ new LocalExecSslServerInitializer(
+ waarpSslContextFactoryServer,
+ LocalExecDefaultResult.MAXWAITPROCESS, executor);
+ bootstrapServer.childHandler(localExecServerInitializer);
- try {
- // Parse options.
- LocalExecSslClientTest client = new LocalExecSslClientTest();
- // run once
- long first = System.currentTimeMillis();
- if (client.connect()) {
- client.runOnce();
- client.disconnect();
- }
- long second = System.currentTimeMillis();
- // print time for one exec
- System.err.println("1=Total time in ms: " + (second - first) + " or " + (1 * 1000 / (second - first))
- + " exec/s");
- System.err.println("Result: " + ok + ":" + ko);
- ok = 0;
- ko = 0;
+ // Bind and start to accept incoming connections only on local address.
+ ChannelFuture future =
+ bootstrapServer.bind(new InetSocketAddress(addr, port));
- // Now run multiple within one thread
- first = System.currentTimeMillis();
- for (int i = 0; i < nit; i++) {
- if (client.connect()) {
- client.runOnce();
- client.disconnect();
- }
- }
- second = System.currentTimeMillis();
- // print time for one exec
- System.err.println(nit + "=Total time in ms: " + (second - first) + " or "
- + (nit * 1000 / (second - first)) + " exec/s");
- System.err.println("Result: " + ok + ":" + ko);
- ok = 0;
- ko = 0;
+ // Finalize client configuration
+ waarpSecureKeyStoreClient
+ .initTrustStore(file2.getAbsolutePath(), trustStorePasswd, false);
+ WaarpSslContextFactory waarpSslContextFactoryClient =
+ new WaarpSslContextFactory(waarpSecureKeyStoreClient);
- // Now run multiple within multiple threads
- // Create multiple threads
- ExecutorService executorService = Executors.newFixedThreadPool(nth);
- first = System.currentTimeMillis();
- // Starts all thread with a default number of execution
- for (int i = 0; i < nth; i++) {
- executorService.submit(new LocalExecSslClientTest());
- }
- Thread.sleep(500);
- executorService.shutdown();
- while (!executorService.awaitTermination(200, TimeUnit.MILLISECONDS)) {
- Thread.sleep(50);
- }
- second = System.currentTimeMillis();
+ localExecClientInitializer =
+ new LocalExecSslClientInitializer(waarpSslContextFactoryClient);
+ bootstrap.handler(localExecClientInitializer);
- // print time for one exec
- System.err.println((nit * nth) + "=Total time in ms: " + (second - first) + " or "
- + (nit * nth * 1000 / (second - first)) + " exec/s");
- System.err.println("Result: " + ok + ":" + ko);
- ok = 0;
- ko = 0;
+ // Wait for the server
+ future.sync();
- // run once
- first = System.currentTimeMillis();
- if (client.connect()) {
- client.runFinal();
- client.disconnect();
- }
- second = System.currentTimeMillis();
- // print time for one exec
- System.err.println("1=Total time in ms: " + (second - first) + " or " + (1 * 1000 / (second - first))
- + " exec/s");
- System.err.println("Result: " + ok + ":" + ko);
- ok = 0;
- ko = 0;
- } finally {
- // Shut down all thread pools to exit.
- workerGroup.shutdownGracefully();
- localExecClientInitializer.releaseResources();
+ try {
+ // Parse options.
+ LocalExecSslClientTest client = new LocalExecSslClientTest();
+ // run once
+ long first = System.currentTimeMillis();
+ if (client.connect()) {
+ client.runOnce();
+ client.disconnect();
+ }
+ long second = System.currentTimeMillis();
+ // print time for one exec
+ System.err.println("1=Total time in ms: " + (second - first) + " or " +
+ (1 * 1000 / (second - first))
+ + " exec/s");
+ System.err.println("Result: " + ok + ":" + ko);
+ ok = 0;
+ ko = 0;
+
+ // Now run multiple within one thread
+ first = System.currentTimeMillis();
+ for (int i = 0; i < nit; i++) {
+ if (client.connect()) {
+ client.runOnce();
+ client.disconnect();
}
- }
+ }
+ second = System.currentTimeMillis();
+ // print time for one exec
+ System.err.println(nit + "=Total time in ms: " + (second - first) + " or "
+ + (nit * 1000 / (second - first)) + " exec/s");
+ System.err.println("Result: " + ok + ":" + ko);
+ ok = 0;
+ ko = 0;
- /**
- * Simple constructor
- */
- public LocalExecSslClientTest() {
- }
+ // Now run multiple within multiple threads
+ // Create multiple threads
+ ExecutorService executorService = Executors.newFixedThreadPool(nth);
+ first = System.currentTimeMillis();
+ // Starts all thread with a default number of execution
+ for (int i = 0; i < nth; i++) {
+ executorService.submit(new LocalExecSslClientTest());
+ }
+ Thread.sleep(500);
+ executorService.shutdown();
+ while (!executorService.awaitTermination(200, TimeUnit.MILLISECONDS)) {
+ Thread.sleep(50);
+ }
+ second = System.currentTimeMillis();
- private Channel channel;
+ // print time for one exec
+ System.err.println(
+ (nit * nth) + "=Total time in ms: " + (second - first) + " or "
+ + (nit * nth * 1000 / (second - first)) + " exec/s");
+ System.err.println("Result: " + ok + ":" + ko);
+ ok = 0;
+ ko = 0;
- /**
- * Run method for thread
- */
- public void run() {
- if (connect()) {
- for (int i = 0; i < nit; i++) {
- this.runOnce();
- }
- disconnect();
- }
+ // run once
+ first = System.currentTimeMillis();
+ if (client.connect()) {
+ client.runFinal();
+ client.disconnect();
+ }
+ second = System.currentTimeMillis();
+ // print time for one exec
+ System.err.println("1=Total time in ms: " + (second - first) + " or " +
+ (1 * 1000 / (second - first))
+ + " exec/s");
+ System.err.println("Result: " + ok + ":" + ko);
+ assertEquals(0, ko);
+ ok = 0;
+ ko = 0;
+ } finally {
+ future.channel().close();
+ // Shut down all thread pools to exit.
+ localExecClientInitializer.releaseResources();
+ localExecServerInitializer.releaseResources();
+ // Shut down all thread pools to exit.
+ workerGroup.shutdownGracefully();
+ localExecClientInitializer.releaseResources();
+ LocalExecServerHandler.junitSetNotShutdown();
}
+ }
- /**
- * Connect to the Server
- */
- private boolean connect() {
- // Start the connection attempt.
- ChannelFuture future = bootstrap.connect(address);
+ /**
+ * Connect to the Server
+ */
+ private boolean connect() {
+ // Start the connection attempt.
+ ChannelFuture future = bootstrap.connect(address);
- // Wait until the connection attempt succeeds or fails.
- channel = WaarpSslUtility.waitforChannelReady(future);
- if (channel == null) {
- System.err.println("Client Not Connected");
- if (future.cause() != null) {
- future.cause().printStackTrace();
- }
- return false;
- }
- return true;
+ // Wait until the connection attempt succeeds or fails.
+ channel = WaarpSslUtility.waitforChannelReady(future);
+ if (channel == null) {
+ System.err.println("Client Not Connected");
+ if (future.cause() != null) {
+ future.cause().printStackTrace();
+ }
+ fail("Cannot connect");
+ return false;
}
+ return true;
+ }
- /**
- * Disconnect from the server
- */
- private void disconnect() {
- WaarpSslUtility.closingSslChannel(channel);
- WaarpSslUtility.waitForClosingSslChannel(channel, 10000);
+ /**
+ * Run method both for not threaded execution and threaded execution
+ */
+ private void runOnce() {
+ // Initialize the command context
+ LocalExecSslClientHandler clientHandler =
+ (LocalExecSslClientHandler) channel.pipeline().last();
+ // Command to execute
+ String line = command + " " + atomicInteger.incrementAndGet();
+ clientHandler.initExecClient(0, line);
+ // Wait for the end of the exec command
+ LocalExecResult localExecResult = clientHandler.waitFor(10000);
+ int status = localExecResult.getStatus();
+ if (status < 0) {
+ System.err.println("Status: " + status + "\nResult: " +
+ localExecResult.getResult());
+ ko++;
+ } else {
+ ok++;
+ result = localExecResult;
}
+ }
- /**
- * Run method both for not threaded execution and threaded execution
- */
- private void runOnce() {
- // Initialize the command context
- LocalExecSslClientHandler clientHandler =
- (LocalExecSslClientHandler) channel.pipeline().last();
- // Command to execute
- String line = command + " " + atomicInteger.incrementAndGet();
- clientHandler.initExecClient(0, line);
- // Wait for the end of the exec command
- LocalExecResult localExecResult = clientHandler.waitFor(10000);
- int status = localExecResult.getStatus();
- if (status < 0) {
- System.err.println("Status: " + status + "\nResult: " +
- localExecResult.getResult());
- ko++;
- } else {
- ok++;
- result = localExecResult;
- }
+ /**
+ * Disconnect from the server
+ */
+ private void disconnect() {
+ WaarpSslUtility.closingSslChannel(channel);
+ WaarpSslUtility.waitForClosingSslChannel(channel, 10000);
+ }
+
+ /**
+ * Run method for closing Server
+ */
+ private void runFinal() {
+ // Initialize the command context
+ LocalExecSslClientHandler clientHandler =
+ (LocalExecSslClientHandler) channel.pipeline().last();
+ // Command to execute
+ clientHandler.initExecClient(-1000, "stop");
+ // Wait for the end of the exec command
+ LocalExecResult localExecResult = clientHandler.waitFor(10000);
+ int status = localExecResult.getStatus();
+ if (status < 0) {
+ System.err.println("Status: " + status + "\nResult: " +
+ localExecResult.getResult());
+ ok++;
+ } else {
+ ok++;
+ result = localExecResult;
}
+ }
- /**
- * Run method for closing Server
- */
- private void runFinal() {
- // Initialize the command context
- LocalExecSslClientHandler clientHandler =
- (LocalExecSslClientHandler) channel.pipeline().last();
- // Command to execute
- clientHandler.initExecClient(-1000, "stop");
- // Wait for the end of the exec command
- LocalExecResult localExecResult = clientHandler.waitFor(10000);
- int status = localExecResult.getStatus();
- if (status < 0) {
- System.err.println("Status: " + status + "\nResult: " +
- localExecResult.getResult());
- ko++;
- } else {
- ok++;
- result = localExecResult;
- }
+ /**
+ * Run method for thread
+ */
+ public void run() {
+ if (connect()) {
+ for (int i = 0; i < nit; i++) {
+ this.runOnce();
+ }
+ disconnect();
}
+ }
}
diff --git a/src/test/java/org/waarp/commandexec/ssl/client/test/package-info.java b/src/test/java/org/waarp/commandexec/ssl/client/test/package-info.java
index b21102c..7c04f89 100644
--- a/src/test/java/org/waarp/commandexec/ssl/client/test/package-info.java
+++ b/src/test/java/org/waarp/commandexec/ssl/client/test/package-info.java
@@ -1,7 +1,25 @@
-/**
- * Classes implementing LocalExec Client test with SSL link
+/*******************************************************************************
+ * This file is part of Waarp Project (named also Waarp or GG).
+ *
+ * Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
+ * tags. See the COPYRIGHT.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * All Waarp Project is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or (at your
+ * option) any later version.
*
+ * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
+ * You should have received a copy of the GNU General Public License along with
+ * Waarp . If not, see