Improve TestNonBlockingCoordinator:
authorkkolinko <kkolinko@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 28 Sep 2011 21:03:09 +0000 (21:03 +0000)
committerkkolinko <kkolinko@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 28 Sep 2011 21:03:09 +0000 (21:03 +0000)
- Format code for better readbility.
- In testCoord1() change how equality is tested. Read expected value from 0th member, instead of (i-1)th. They all should be the same.
- Remove unneeded main() method.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1177060 13f79535-47bb-0310-9956-ffa450edef68

test/org/apache/catalina/tribes/group/interceptors/TestNonBlockingCoordinator.java

index 3ce3a4b..7f5a30b 100644 (file)
@@ -30,15 +30,14 @@ public class TestNonBlockingCoordinator {
 
     private GroupChannel[] channels = null;
     private NonBlockingCoordinator[] coordinators = null;
-    private int channelCount = 10;
-    private Thread[] threads = null;
+    private final int channelCount = 10;
 
     @Before
     public void setUp() throws Exception {
         System.out.println("Setup");
         channels = new GroupChannel[channelCount];
         coordinators = new NonBlockingCoordinator[channelCount];
-        threads = new Thread[channelCount];
+        Thread[] threads = new Thread[channelCount];
         for ( int i=0; i<channelCount; i++ ) {
             channels[i] = new GroupChannel();
             coordinators[i] = new NonBlockingCoordinator();
@@ -57,21 +56,36 @@ public class TestNonBlockingCoordinator {
                 }
             };
         }
-        for ( int i=0; i<channelCount; i++ ) threads[i].start();
-        for ( int i=0; i<channelCount; i++ ) threads[i].join();
+        for (int i = 0; i < channelCount; i++) {
+            threads[i].start();
+        }
+        for (int i = 0; i < channelCount; i++) {
+            threads[i].join();
+        }
         Thread.sleep(1000);
     }
 
     @Test
     public void testCoord1() throws Exception {
-        for (int i=1; i<channelCount; i++ ) 
-            assertEquals("Message count expected to be equal.",channels[i-1].getMembers().length,channels[i].getMembers().length);
+        int expectedCount = channels[0].getMembers().length;
+        for (int i = 1; i < channelCount; i++) {
+            assertEquals("Message count expected to be equal.", expectedCount,
+                    channels[i].getMembers().length);
+        }
         Member member = coordinators[0].getCoordinator();
         int cnt = 0;
-        while ( member == null && (cnt++ < 100 ) ) try {Thread.sleep(100); member = coordinators[0].getCoordinator();}catch ( Exception x){ /* Ignore */ }
-        for (int i=0; i<channelCount; i++ ) assertEquals(member,coordinators[i].getCoordinator());
-        System.out.println("Coordinator[1] is:"+member);
-        
+        while (member == null && (cnt++ < 100)) {
+            try {
+                Thread.sleep(100);
+                member = coordinators[0].getCoordinator();
+            } catch (Exception x) {
+                /* Ignore */
+            }
+        }
+        for (int i = 0; i < channelCount; i++) {
+            assertEquals(member, coordinators[i].getCoordinator());
+        }
+        System.out.println("Coordinator[1] is:" + member);
     }
 
     @Test
@@ -88,10 +102,18 @@ public class TestNonBlockingCoordinator {
         }
         int dead = index;
         Thread.sleep(1000);
-        if ( index == 0 ) index = 1; else index = 0;
+        if (index == 0) {
+            index = 1;
+        } else {
+            index = 0;
+        }
         System.out.println("Member count:"+channels[index].getMembers().length);
         member = coordinators[index].getCoordinator();
-        for (int i = 1; i < channelCount; i++) if ( i != dead ) assertEquals(member, coordinators[i].getCoordinator());
+        for (int i = 1; i < channelCount; i++) {
+            if (i != dead) {
+                assertEquals(member, coordinators[i].getCoordinator());
+            }
+        }
         System.out.println("Coordinator[2b] is:" + member);
     }
 
@@ -103,9 +125,4 @@ public class TestNonBlockingCoordinator {
         }
     }
 
-    public static void main(String[] args) throws Exception {
-        org.junit.runner.JUnitCore.main(TestNonBlockingCoordinator.class.getName());
-    }
-
-
 }