From 10f491150f0f0db50543d3dd06d9d742a1504a3d Mon Sep 17 00:00:00 2001
From: Ludovic Poitou <ludovic.poitou@forgerock.com>
Date: Mon, 18 Jul 2011 17:44:11 +0000
Subject: [PATCH] Another pass at tidying up replication related tests and reducing dependencies between them.

---
 opends/tests/unit-tests-testng/src/server/org/opends/server/replication/DependencyTest.java          |   24 +++
 opends/tests/unit-tests-testng/src/server/org/opends/server/replication/ExternalChangeLogTest.java   |  268 +++++++++++++++++++++++++-------------------
 opends/tests/unit-tests-testng/src/server/org/opends/server/replication/plugin/TopologyViewTest.java |    9 +
 3 files changed, 181 insertions(+), 120 deletions(-)

diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/DependencyTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/DependencyTest.java
index 265a386..2b30de4 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/DependencyTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/DependencyTest.java
@@ -23,9 +23,12 @@
  *
  *
  *      Copyright 2007-2010 Sun Microsystems, Inc.
+ *      Portions Copyright 2011 ForgeRock AS
  */
 package org.opends.server.replication;
 
+import org.opends.server.util.StaticUtils;
+import java.io.File;
 import static org.testng.Assert.*;
 
 import java.io.FileNotFoundException;
@@ -231,9 +234,11 @@
     }
     finally
     {
-      if (replServer != null)
+      if (replServer != null) {
         replServer.remove();
-
+        StaticUtils.recursiveDelete(new File(DirectoryServer.getInstanceRoot(),
+            replServer.getDbDirName()));
+      }
       if (domain != null)
         MultimasterReplication.deleteDomain(baseDn);
     }
@@ -350,8 +355,11 @@
     }
     finally
     {
-      if (replServer != null)
+      if (replServer != null) {
         replServer.remove();
+        StaticUtils.recursiveDelete(new File(DirectoryServer.getInstanceRoot(),
+            replServer.getDbDirName()));
+      }
 
       if (domain != null)
         MultimasterReplication.deleteDomain(baseDn);
@@ -511,8 +519,11 @@
     }
     finally
     {
-      if (replServer != null)
+      if (replServer != null) {
         replServer.remove();
+        StaticUtils.recursiveDelete(new File(DirectoryServer.getInstanceRoot(),
+            replServer.getDbDirName()));
+      }
 
       if (domain != null)
         MultimasterReplication.deleteDomain(baseDn);
@@ -620,8 +631,11 @@
     }
     finally
     {
-      if (replServer != null)
+      if (replServer != null) {
         replServer.remove();
+        StaticUtils.recursiveDelete(new File(DirectoryServer.getInstanceRoot(),
+            replServer.getDbDirName()));
+      }
 
       if (domain != null)
         MultimasterReplication.deleteDomain(baseDn);
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/ExternalChangeLogTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/ExternalChangeLogTest.java
index aa98b59..b2b9d65 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/ExternalChangeLogTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/ExternalChangeLogTest.java
@@ -248,7 +248,6 @@
 
     // Test with a mix of domains, a mix of DSes
     ECLTwoDomains();
-    replicationServer.clearDb();
   }
 
   @Test(enabled=true, dependsOnMethods = { "ECLReplicationServerTest"})
@@ -257,7 +256,6 @@
 
     // Test ECL after changelog triming
     ECLAfterChangelogTrim();
-    replicationServer.clearDb();
   }
 
   @Test(enabled=true, dependsOnMethods = { "ECLReplicationServerTest"})
@@ -277,7 +275,6 @@
   public void ECLReplicationServerTest4()
   {
     ECLIncludeAttributes();
-    replicationServer.clearDb();
   }
 
   @Test(enabled=true, dependsOnMethods = { "ECLReplicationServerTest"})
@@ -285,7 +282,6 @@
   {
 
     ChangeTimeHeartbeatTest();
-    replicationServer.clearDb();
 
   }
 
@@ -307,7 +303,6 @@
 
     // Test that private backend is excluded from ECL
     ECLOnPrivateBackend();
-    replicationServer.clearDb();
   }
 
   @Test(enabled=true, groups="slow", dependsOnMethods = { "ECLReplicationServerTest"})
@@ -315,7 +310,6 @@
   {
     // Test remote API (ECL through replication protocol) with empty ECL
     ECLRemoteEmpty();
-    replicationServer.clearDb();
   }
 
   @Test(enabled=true, groups="slow", dependsOnMethods = { "ECLReplicationServerTest"})
@@ -323,7 +317,6 @@
   {
     // Test with empty changelog
     ECLEmpty();
-    replicationServer.clearDb();
   }
 
   @Test(enabled=true, groups="slow", dependsOnMethods = { "ECLReplicationServerTest"})
@@ -348,24 +341,8 @@
   {
     // Test remote API (ECL through replication protocol) with NON empty ECL
     ECLRemoteNonEmpty();
-    replicationServer.clearDb();
   }
 
-  @Test(enabled=true, groups="slow", dependsOnMethods = { "ECLReplicationServerTest"})
-  public void ECLReplicationServerFullTest5()
-  {
-    // Test with a mix of domains, a mix of DSes
-    ECLTwoDomains();
-    replicationServer.clearDb();
-  }
-
-  @Test(enabled=true, groups="slow", dependsOnMethods = { "ECLReplicationServerTest"})
-  public void ECLReplicationServerFullTest6()
-  {
-    // Test ECL after changelog triming
-    ECLAfterChangelogTrim();
-    replicationServer.clearDb();
-  }
 
   @Test(enabled=true, groups="slow", dependsOnMethods = { "ECLReplicationServerTest"})
   public void ECLReplicationServerFullTest7()
@@ -388,7 +365,6 @@
   {
     // Simultaneous psearches
     ECLSimultaneousPsearches();
-    replicationServer.clearDb();
   }
 
   @Test(enabled=true, groups="slow", dependsOnMethods = { "ECLReplicationServerTest"})
@@ -411,7 +387,6 @@
     // Test directly from the java obect that the changeTimeHeartbeatState
     // stored are ok.
     ChangeTimeHeartbeatTest();
-    replicationServer.clearDb();
 
   }
 
@@ -483,16 +458,10 @@
 
     // Test Filter on replication csn
     // TODO: test with optimization when code done.
-    ECLFilterOnReplicationCsn();replicationServer.clearDb();
+    ECLFilterOnReplicationCsn();
+    replicationServer.clearDb();
   }
 
-  @Test(enabled=true, groups="slow", dependsOnMethods = { "ECLReplicationServerTest"})
-  public void ECLReplicationServerFullTest17()
-  {
-    // Test simultaneous persistent searches in draft compat mode.
-    ECLSimultaneousPsearches();replicationServer.clearDb();
-
-  }
 
   private void ECLIsNotASupportedSuffix()
   {
@@ -575,10 +544,6 @@
       assertTrue(msg instanceof DoneMsg,
       "Ending " + tn + " with incorrect message type :" +
       msg.getClass().getCanonicalName());
-
-      server1.stop();
-      server2.stop();
-      server3.stop();
       debugInfo(tn, "Ending test successfully\n\n");
     }
     catch(Exception e)
@@ -586,6 +551,16 @@
       fail("Ending test " + tn +  " with exception:"
           +  stackTraceToSingleLineString(e));
     }
+    finally
+    {
+      if (server1 != null)
+        server1.stop();
+      if (server2 != null)
+        server2.stop();
+      if (server3 != null)
+        server3.stop();
+      replicationServer.clearDb();
+    }
   }
 
   //=======================================================
@@ -668,10 +643,6 @@
       debugInfo(tn, "RESULT:" + msg);
       assertTrue(msg instanceof DoneMsg, "RESULT:" + msg);
 
-      // clean
-      serverECL.stop();
-      server01.stop();
-      server02.stop();
       debugInfo(tn, "Ending test successfully");
     }
     catch(Exception e)
@@ -679,6 +650,17 @@
       fail("Ending test " + tn + " with exception:"
           +  stackTraceToSingleLineString(e));
     }
+    finally
+    {
+       // clean
+      if (serverECL != null)
+        serverECL.stop();
+      if (server01 != null)
+        server01.stop();
+      if (server02 != null)
+        server02.stop();
+      replicationServer.clearDb();
+    }
   }
 
   /**
@@ -690,6 +672,8 @@
     String tn = "ECLEmpty";
     debugInfo(tn, "Starting test\n\n");
 
+    replicationServer.clearDb();
+
     try
     {
       // search on 'cn=changelog'
@@ -729,7 +713,7 @@
     ExternalChangelogRequestControl control =
       new ExternalChangelogRequestControl(true,
           new MultiDomainServerState(cookie));
-    ArrayList<Control> controls = new ArrayList<Control>(0);
+    ArrayList<Control> controls = new ArrayList<Control>(1);
     controls.add(control);
     return controls;
   }
@@ -770,9 +754,18 @@
   {
     String tn = "ECLOnPrivateBackend";
     debugInfo(tn, "Starting test");
+
+    replicationServer.clearDb();
+
+    ReplicationBroker server01 = null;
+    LDAPReplicationDomain domain2 = null;
+    Backend backend2 = null;
+    DN baseDn2 = null;
     try
     {
-      ReplicationBroker server01 = openReplicationSession(
+      baseDn2 = DN.decode(TEST_ROOT_DN_STRING2);
+
+      server01 = openReplicationSession(
           DN.decode(TEST_ROOT_DN_STRING),  1201,
           100, replicationServerPort,
           brokerSessionTimeout, true);
@@ -789,10 +782,9 @@
       // Initialize a second test backend o=test2, in addtion to o=test
       // Configure replication on this backend
       // Add the root entry in the backend
-      Backend backend2 = initializeTestBackend(false, TEST_ROOT_DN_STRING2,
+      backend2 = initializeTestBackend(false, TEST_ROOT_DN_STRING2,
           TEST_BACKEND_ID2);
       backend2.setPrivateBackend(true);
-      DN baseDn2 = DN.decode(TEST_ROOT_DN_STRING2);
       SortedSet<String> replServers = new TreeSet<String>();
       replServers.add("localhost:"+replicationServerPort);
 
@@ -801,8 +793,8 @@
       ExternalChangelogDomainFakeCfg eclCfg =
         new ExternalChangelogDomainFakeCfg(true, null, null);
       domainConf.setExternalChangelogDomain(eclCfg);
-      LDAPReplicationDomain domain2 =
-        MultimasterReplication.createNewDomain(domainConf);
+
+      domain2 = MultimasterReplication.createNewDomain(domainConf);
       domain2.start();
 
       sleep(1000);
@@ -902,18 +894,24 @@
           " Expected last cookie attribute value:" + expectedLastCookie +
           " Read from server: " + lastCookie + " are equal :");
 
-      // Cleaning
-      if (domain2 != null)
-        MultimasterReplication.deleteDomain(baseDn2);
-      removeTestBackend2(backend2);
-
-      server01.stop();
     }
     catch(Exception e)
     {
       fail("Ending test " + tn + " with exception:"
           +  stackTraceToSingleLineString(e));
     }
+    finally
+    {
+      // Cleaning
+      if (domain2 != null && baseDn2 != null)
+        MultimasterReplication.deleteDomain(baseDn2);
+      if (backend2 != null)
+      removeTestBackend2(backend2);
+
+      if (server01 != null)
+        server01.stop();
+      replicationServer.clearDb();
+    }
     debugInfo(tn, "Ending test successfully");
   }
 
@@ -927,6 +925,11 @@
     String tn = "ECLTwoDomains";
     debugInfo(tn, "Starting test");
 
+    ReplicationBroker s1test = null;
+    ReplicationBroker s1test2 = null;
+    ReplicationBroker s2test = null;
+    ReplicationBroker s2test2 = null;
+    
     try
     {
       // Initialize a second test backend
@@ -937,12 +940,12 @@
       LDIFWriter ldifWriter = getLDIFWriter();
 
       // --
-      ReplicationBroker s1test = openReplicationSession(
+      s1test = openReplicationSession(
           DN.decode(TEST_ROOT_DN_STRING),  1201,
           100, replicationServerPort,
           brokerSessionTimeout, true);
 
-      ReplicationBroker s2test2 = openReplicationSession(
+      s2test2 = openReplicationSession(
           DN.decode(TEST_ROOT_DN_STRING2),  1202,
           100, replicationServerPort,
           brokerSessionTimeout, true, EMPTY_DN_GENID);
@@ -1168,12 +1171,12 @@
       // Test startState ("first cookie") of the ECL
       //
       // --
-      ReplicationBroker s1test2 = openReplicationSession(
+      s1test2 = openReplicationSession(
           DN.decode(TEST_ROOT_DN_STRING2),  1203,
           100, replicationServerPort,
           brokerSessionTimeout, true, EMPTY_DN_GENID);
 
-      ReplicationBroker s2test = openReplicationSession(
+      s2test = openReplicationSession(
           DN.decode(TEST_ROOT_DN_STRING),  1204,
           100, replicationServerPort,
           brokerSessionTimeout, true);
@@ -1307,19 +1310,24 @@
       assertTrue(searchOp.getErrorMessage().toString().equalsIgnoreCase(expectedError),
           "Expected: " + expectedError + "Server output:" +
           searchOp.getErrorMessage().toString());
-      s1test.stop();
-      s1test2.stop();
-      s2test.stop();
-      s2test2.stop();
-      sleep(500);
-
-      // removeTestBackend2(backend2);
     }
     catch(Exception e)
     {
       fail("Ending test " + tn + "with exception:\n"
           +  stackTraceToSingleLineString(e));
     }
+    finally
+    {
+      if (s1test != null)
+        s1test.stop();
+      if (s1test2 != null)
+        s1test2.stop();
+      if (s2test != null)
+        s2test.stop();
+      if (s2test2 != null)
+        s2test2.stop();
+      replicationServer.clearDb();
+    }
     debugInfo(tn, "Ending test successfully");
   }
 
@@ -1330,13 +1338,17 @@
     String tn = "ECLAfterChangelogTrim";
     debugInfo(tn, "Starting test");
 
+    ReplicationBroker server01 = null;
+    ReplicationServerDomain d1 = null;
+    ReplicationServerDomain d2 = null;
+
     try
     {
       // ---
       // 1. Populate the changelog and read the cookie
 
       // Creates broker on o=test
-      ReplicationBroker server01 = openReplicationSession(
+      server01 = openReplicationSession(
           DN.decode(TEST_ROOT_DN_STRING),  1201,
           100, replicationServerPort,
           brokerSessionTimeout, true);
@@ -1373,8 +1385,8 @@
       // ---
       // 2. Now set up a very short purge delay on the replication changelogs
       // so that this test can play with a trimmed changelog.
-      ReplicationServerDomain d1 = replicationServer.getReplicationServerDomain("o=test", false);
-      ReplicationServerDomain d2 = replicationServer.getReplicationServerDomain("o=test2", false);
+      d1 = replicationServer.getReplicationServerDomain("o=test", false);
+      d2 = replicationServer.getReplicationServerDomain("o=test2", false);
       d1.setPurgeDelay(1);
       d2.setPurgeDelay(1);
 
@@ -1490,19 +1502,24 @@
 	  .toString()),
           searchOp.getErrorMessage().toString());
 
-      // Clean
-      server01.stop();
-
-      // And reset changelog purge delay for the other tests.
-      d1.setPurgeDelay(15 * 1000);
-      d2.setPurgeDelay(15 * 1000);
-
     }
     catch(Exception e)
     {
       fail("Ending test " + tn + "with exception:\n"
           +  stackTraceToSingleLineString(e));
     }
+    finally
+    {
+      if (server01 != null)
+        server01.stop();
+      // And reset changelog purge delay for the other tests.
+      if (d1 != null)
+        d1.setPurgeDelay(15 * 1000);
+      if (d2 != null)
+        d2.setPurgeDelay(15 * 1000);
+
+      replicationServer.clearDb();
+    }
     debugInfo(tn, "Ending test successfully");
   }
 
@@ -2218,7 +2235,9 @@
   {
     String tn = "ECLSimultaneousPsearches";
     debugInfo(tn, "Starting test \n\n");
-    Socket s1, s2, s3 = null;
+    Socket s1 = null, s2 = null, s3 = null;
+    ReplicationBroker server01 = null;
+    ReplicationBroker server02 = null;
     boolean compatMode = false;
     boolean changesOnly = false;
 
@@ -2239,7 +2258,7 @@
     try
     {
       // Create broker on o=test
-      ReplicationBroker server01 = openReplicationSession(
+      server01 = openReplicationSession(
           DN.decode(TEST_ROOT_DN_STRING),  1201,
           100, replicationServerPort,
           brokerSessionTimeout, true);
@@ -2247,7 +2266,7 @@
       int ts = 1;
 
       // Create broker on o=test2
-      ReplicationBroker server02 = openReplicationSession(
+      server02 = openReplicationSession(
           DN.decode(TEST_ROOT_DN_STRING2),  1202,
           100, replicationServerPort,
           brokerSessionTimeout, true, EMPTY_DN_GENID);
@@ -2665,22 +2684,35 @@
         }
       }
       debugInfo(tn, "Search 3 successfully receives additional changes");
-
-      server01.stop();
-      server02.stop();
-
-      try { s1.close(); } catch (Exception e) {};
-      try { s2.close(); } catch (Exception e) {};
-      try { s3.close(); } catch (Exception e) {};
-
-      while (!s1.isClosed()) sleep(100);
-      while (!s2.isClosed()) sleep(100);
-      while (!s3.isClosed()) sleep(100);
-    }
+   }
     catch(Exception e)
     {
       fail("Test " + tn + " fails with " +  stackTraceToSingleLineString(e));
     }
+    finally
+    {
+      if (server01 != null)
+        server01.stop();
+      if (server02 != null)
+        server02.stop();
+
+      if (s1 != null)
+      {
+        try { s1.close(); } catch (Exception ignored) {};
+        while (!s1.isClosed()) sleep(100);      
+      }
+      if (s2 != null)
+      {
+        try { s2.close(); } catch (Exception e) {};
+        while (!s2.isClosed()) sleep(100);
+      }
+      if (s3 != null)
+      {
+        try { s3.close(); } catch (Exception e) {};
+        while (!s3.isClosed()) sleep(100);
+      }
+      replicationServer.clearDb();
+    }
     debugInfo(tn, "Ends test successfully");
   }
 
@@ -2829,20 +2861,26 @@
   {
     String tn = "ChangeTimeHeartbeatTest";
     debugInfo(tn, "Starting test");
+    ReplicationBroker s1test = null;
+    ReplicationBroker s2test = null;
+    ReplicationBroker s1test2 = null;
+    ReplicationBroker s2test2 = null;
 
+    // Initialize a second test backend
+    Backend backend2 = null;
+    
     try
     {
-      // Initialize a second test backend
-      Backend backend2 = initializeTestBackend(true, TEST_ROOT_DN_STRING2,
+      backend2 = initializeTestBackend(true, TEST_ROOT_DN_STRING2,
           TEST_BACKEND_ID2);
-
+          
       // --
-      ReplicationBroker s1test = openReplicationSession(
+      s1test = openReplicationSession(
           DN.decode(TEST_ROOT_DN_STRING),  1201,
           100, replicationServerPort,
           brokerSessionTimeout, true);
 
-      ReplicationBroker s2test2 = openReplicationSession(
+      s2test2 = openReplicationSession(
           DN.decode(TEST_ROOT_DN_STRING2),  1202,
           100, replicationServerPort,
           brokerSessionTimeout, true, EMPTY_DN_GENID);
@@ -2877,12 +2915,12 @@
       sleep(500);
 
       // --
-      ReplicationBroker s1test2 = openReplicationSession(
+      s1test2 = openReplicationSession(
           DN.decode(TEST_ROOT_DN_STRING2),  1203,
           100, replicationServerPort,
           brokerSessionTimeout, true, EMPTY_DN_GENID);
 
-      ReplicationBroker s2test = openReplicationSession(
+      s2test = openReplicationSession(
           DN.decode(TEST_ROOT_DN_STRING),  1204,
           100, replicationServerPort,
           brokerSessionTimeout, true);
@@ -2932,31 +2970,27 @@
           + " eligibleCN=" + rsd2.getEligibleCN()
           + " rs eligibleCN=" + replicationServer.getEligibleCN());
       // FIXME:ECL Enable this test by adding an assert on the right value
-
-      s1test2.stop();
-      s2test2.stop();
-      removeTestBackend2(backend2);
-
-      Thread.sleep(1000);
-
-      rsd2.getChangeTimeHeartbeatState();
-      debugInfo(tn, rsd2.getBaseDn()
-          + " DbServerState=" + rsd2.getDbServerState()
-          + " ChangeTimeHeartBeatState=" + rsd2.getChangeTimeHeartbeatState()
-          + " eligibleCN=" + rsd2.getEligibleCN()
-          + " rs eligibleCN=" + replicationServer.getEligibleCN());
-
-      s1test.stop();
-      s2test.stop();
-
-      Thread.sleep(1000);
-
     }
     catch(Exception e)
     {
       fail("Ending test " + tn + " with exception:"
           +  stackTraceToSingleLineString(e));
     }
+    finally
+    {
+      if (s1test2 != null)
+        s1test2.stop();
+      if (s2test2 != null)
+        s2test2.stop();
+      if (backend2 != null)
+        removeTestBackend2(backend2);
+      if (s1test != null)
+        s1test.stop();
+      if (s2test != null)
+        s2test.stop();
+
+      replicationServer.clearDb();
+    }
     debugInfo(tn, "Ending test successfully");
   }
 
@@ -4366,6 +4400,10 @@
         fail("Ending test "+tn+" with exception in test cleanup:\n" +
             stackTraceToSingleLineString(e));
       }
+      finally
+      {
+        replicationServer.clearDb();
+      }
     }
     debugInfo(tn, "Ending test with success");
   }
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/plugin/TopologyViewTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/plugin/TopologyViewTest.java
index 295a834..6ceb4a3 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/plugin/TopologyViewTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/plugin/TopologyViewTest.java
@@ -38,6 +38,7 @@
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.fail;
 
+import java.io.File;
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.ServerSocket;
@@ -53,6 +54,7 @@
 import org.opends.messages.Severity;
 import org.opends.server.TestCaseUtils;
 import org.opends.server.admin.std.meta.ReplicationDomainCfgDefn.AssuredType;
+import org.opends.server.core.DirectoryServer;
 import org.opends.server.loggers.debug.DebugTracer;
 import org.opends.server.replication.ReplicationTestCase;
 import org.opends.server.replication.common.AssuredMode;
@@ -64,6 +66,7 @@
 import org.opends.server.replication.server.ReplicationServer;
 import org.opends.server.types.DN;
 import org.opends.server.types.DirectoryException;
+import org.opends.server.util.StaticUtils;
 import org.testng.annotations.Test;
 
 /**
@@ -221,6 +224,8 @@
     {
       rs1.clearDb();
       rs1.remove();
+      StaticUtils.recursiveDelete(new File(DirectoryServer.getInstanceRoot(),
+            rs1.getDbDirName()));
       rs1 = null;
     }
 
@@ -228,6 +233,8 @@
     {
       rs2.clearDb();
       rs2.remove();
+      StaticUtils.recursiveDelete(new File(DirectoryServer.getInstanceRoot(),
+            rs2.getDbDirName()));
       rs2 = null;
     }
 
@@ -235,6 +242,8 @@
     {
       rs3.clearDb();
       rs3.remove();
+      StaticUtils.recursiveDelete(new File(DirectoryServer.getInstanceRoot(),
+            rs3.getDbDirName()));
       rs3 = null;
     }
     rs1Port = -1;

--
Gitblit v1.10.0