Improved exception handling thrown by client threads in ConcurrentClientsTest 51/2351/3
authorMaros Marsalek <mmarsale@cisco.com>
Mon, 4 Nov 2013 09:14:02 +0000 (10:14 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 5 Nov 2013 16:15:38 +0000 (16:15 +0000)
Change-Id: Iabb655c4bf6f03e80c7822cbbc19e79d8f1db30e
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/ConcurrentClientsTest.java

index a74a347d3bbf5d29811ddf28dd2c9c825cb460cc..1295149c0262a7982226c3ae71195b5538fe0155 100644 (file)
@@ -56,7 +56,7 @@ import java.util.Set;
 import java.util.concurrent.TimeUnit;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import java.util.concurrent.TimeUnit;
 
 import static com.google.common.base.Preconditions.checkNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
@@ -176,7 +176,11 @@ public class ConcurrentClientsTest {
 
         for (TestingThread thread : threads) {
             thread.join();
 
         for (TestingThread thread : threads) {
             thread.join();
-            assertTrue(thread.success);
+            if(thread.thrownException.isPresent()) {
+                Exception exception = thread.thrownException.get();
+                logger.error("Thread for testing client failed", exception);
+                fail("Client thread " + thread + " failed: " + exception.getMessage());
+            }
         }
     }
 
         }
     }
 
@@ -196,12 +200,16 @@ public class ConcurrentClientsTest {
 
         for (BlockingThread thread : threads) {
             thread.join();
 
         for (BlockingThread thread : threads) {
             thread.join();
-            assertTrue(thread.success);
+            if(thread.thrownException.isPresent()) {
+                Exception exception = thread.thrownException.get();
+                logger.error("Thread for testing client failed", exception);
+                fail("Client thread " + thread + " failed: " + exception.getMessage());
+            }
         }
     }
 
     class BlockingThread extends Thread {
         }
     }
 
     class BlockingThread extends Thread {
-        Boolean success;
+        private Optional<Exception> thrownException;
 
         public BlockingThread(String name) {
             super("client-" + name);
 
         public BlockingThread(String name) {
             super("client-" + name);
@@ -211,10 +219,9 @@ public class ConcurrentClientsTest {
         public void run() {
             try {
                 run2();
         public void run() {
             try {
                 run2();
-                success = true;
+                thrownException = Optional.absent();
             } catch (Exception e) {
             } catch (Exception e) {
-                success = false;
-                throw new RuntimeException(e);
+                thrownException = Optional.of(e);
             }
         }
 
             }
         }
 
@@ -254,7 +261,7 @@ public class ConcurrentClientsTest {
 
         private final String clientId;
         private final int attempts;
 
         private final String clientId;
         private final int attempts;
-        private Boolean success;
+        private Optional<Exception> thrownException;
 
         TestingThread(String clientId, int attempts) {
             this.clientId = clientId;
 
         TestingThread(String clientId, int attempts) {
             this.clientId = clientId;
@@ -275,10 +282,9 @@ public class ConcurrentClientsTest {
                 logger.info("Client with sessionid {} got result {}", sessionId, result);
                 netconfClient.close();
                 logger.info("Client with session id {} ended", sessionId);
                 logger.info("Client with sessionid {} got result {}", sessionId, result);
                 netconfClient.close();
                 logger.info("Client with session id {} ended", sessionId);
-                success = true;
+                thrownException = Optional.absent();
             } catch (final Exception e) {
             } catch (final Exception e) {
-                success = false;
-                throw new RuntimeException(e);
+                thrownException = Optional.of(e);
             }
         }
     }
             }
         }
     }