From 210d5f1b76962b2722db7b4d74c3372c22bcd0ca Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Fri, 03 Jan 2014 14:34:26 +0000
Subject: [PATCH] Fixed test failure for JEChangeNumberIndexDBTest.testClear(). Problem was due to the CNIndexDB state not being properly cleaned up. I chose to rely on ReplicationTestCase.remove(ReplicationServer) to clean up the state of the ChangeNumberIndexDB.

---
 opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/changelog/je/JEChangeNumberIndexDBTest.java |   31 +++------------
 opends/src/server/org/opends/server/replication/server/changelog/je/JEChangelogDB.java                                     |   17 ++++++++
 opends/src/server/org/opends/server/replication/server/changelog/je/JEChangeNumberIndexDB.java                             |   60 ++++++++---------------------
 3 files changed, 39 insertions(+), 69 deletions(-)

diff --git a/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangeNumberIndexDB.java b/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangeNumberIndexDB.java
index 8ae8161..5f1b63e 100644
--- a/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangeNumberIndexDB.java
+++ b/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangeNumberIndexDB.java
@@ -93,7 +93,6 @@
   private final AtomicLong lastGeneratedChangeNumber;
   private DbMonitorProvider dbMonitor = new DbMonitorProvider();
   private final AtomicBoolean shutdown = new AtomicBoolean(false);
-  private volatile boolean trimDone = false;
   /**
    * A dedicated thread loops trim().
    * <p>
@@ -252,15 +251,15 @@
       notifyAll();
     }
 
-    synchronized (this)
-    { /* Can we just do a thread.join() ? */
-      while (!trimDone)
+    if (trimmingThread != null)
+    {
+      try
       {
-        try
-        {
-          wait();
-        } catch (InterruptedException e)
-        { /* do nothing */ }
+        trimmingThread.join();
+      }
+      catch (InterruptedException ignored)
+      {
+        // Nothing can be done about it, just proceed
       }
     }
 
@@ -283,12 +282,16 @@
 
         synchronized (this)
         {
-          try
+          if (!shutdown.get())
           {
-            wait(1000);
-          } catch (InterruptedException e)
-          {
-            Thread.currentThread().interrupt();
+            try
+            {
+              wait(1000);
+            }
+            catch (InterruptedException e)
+            {
+              Thread.currentThread().interrupt();
+            }
           }
         }
       }
@@ -302,35 +305,6 @@
         }
         break;
       }
-      try {
-        trim(shutdown);
-
-        synchronized (this)
-        {
-          try
-          {
-            wait(1000);
-          } catch (InterruptedException e)
-          {
-            Thread.currentThread().interrupt();
-          }
-        }
-      } catch (Exception end)
-      {
-        logError(ERR_EXCEPTION_CHANGELOG_TRIM_FLUSH
-            .get(stackTraceToSingleLineString(end)));
-        if (replicationServer != null)
-        {
-          replicationServer.shutdown();
-        }
-        break;
-      }
-    }
-
-    synchronized (this)
-    {
-      trimDone = true;
-      notifyAll();
     }
   }
 
diff --git a/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangelogDB.java b/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangelogDB.java
index 0f588a2..fbd19e8 100644
--- a/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangelogDB.java
+++ b/opends/src/server/org/opends/server/replication/server/changelog/je/JEChangelogDB.java
@@ -678,6 +678,18 @@
   @Override
   public ChangeNumberIndexDB getChangeNumberIndexDB()
   {
+    return getChangeNumberIndexDB(true);
+  }
+
+  /**
+   * Returns the {@link ChangeNumberIndexDB} object.
+   *
+   * @param startTrimmingThread
+   *          whether the trimming thread should be started
+   * @return the {@link ChangeNumberIndexDB} object
+   */
+  ChangeNumberIndexDB getChangeNumberIndexDB(boolean startTrimmingThread)
+  {
     synchronized (cnIndexDBLock)
     {
       if (cnIndexDB == null)
@@ -685,7 +697,10 @@
         try
         {
           cnIndexDB = new JEChangeNumberIndexDB(replicationServer, this.dbEnv);
-          cnIndexDB.startTrimmingThread();
+          if (startTrimmingThread)
+          {
+            cnIndexDB.startTrimmingThread();
+          }
         }
         catch (Exception e)
         {
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/changelog/je/JEChangeNumberIndexDBTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/changelog/je/JEChangeNumberIndexDBTest.java
index 54c00bd..8f7717c 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/changelog/je/JEChangeNumberIndexDBTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/changelog/je/JEChangeNumberIndexDBTest.java
@@ -26,9 +26,6 @@
  */
 package org.opends.server.replication.server.changelog.je;
 
-import java.io.File;
-import java.io.IOException;
-
 import org.opends.server.TestCaseUtils;
 import org.opends.server.replication.ReplicationTestCase;
 import org.opends.server.replication.common.CSN;
@@ -74,7 +71,7 @@
     try
     {
       replicationServer = newReplicationServer();
-      cnIndexDB = newCNIndexDB(replicationServer);
+      cnIndexDB = getCNIndexDBNoTrimming(replicationServer);
       cnIndexDB.setPurgeDelay(0);
 
       // Prepare data to be stored in the db
@@ -122,8 +119,6 @@
     }
     finally
     {
-      if (cnIndexDB != null)
-        cnIndexDB.shutdown();
       remove(replicationServer);
     }
   }
@@ -141,27 +136,15 @@
     assertEquals(record.getPreviousCookie(), cookie);
   }
 
-  private JEChangeNumberIndexDB newCNIndexDB(ReplicationServer rs) throws Exception
+  private JEChangeNumberIndexDB getCNIndexDBNoTrimming(ReplicationServer rs) throws ChangelogException
   {
-    final File testRoot = createCleanDir();
-    final ReplicationDbEnv dbEnv = new ReplicationDbEnv(testRoot.getPath(), rs);
-    final JEChangeNumberIndexDB cnIndexDB = new JEChangeNumberIndexDB(rs, dbEnv);
+    final JEChangelogDB changelogDB = (JEChangelogDB) rs.getChangelogDB();
+    final JEChangeNumberIndexDB cnIndexDB =
+        (JEChangeNumberIndexDB) changelogDB.getChangeNumberIndexDB(false);
     assertTrue(cnIndexDB.isEmpty());
     return cnIndexDB;
   }
 
-  private File createCleanDir() throws IOException
-  {
-    String buildRoot = System.getProperty(TestCaseUtils.PROPERTY_BUILD_ROOT);
-    String path = System.getProperty(TestCaseUtils.PROPERTY_BUILD_DIR, buildRoot
-            + File.separator + "build");
-    path = path + File.separator + "unit-tests" + File.separator + "JEChangeNumberIndexDB";
-    final File testRoot = new File(path);
-    TestCaseUtils.deleteDirectory(testRoot);
-    testRoot.mkdirs();
-    return testRoot;
-  }
-
   /**
    * This test makes basic operations of a JEChangeNumberIndexDB and explicitly
    * calls the clear() method instead of waiting for the periodic trim to clear
@@ -181,7 +164,7 @@
     try
     {
       replicationServer = newReplicationServer();
-      cnIndexDB = newCNIndexDB(replicationServer);
+      cnIndexDB = getCNIndexDBNoTrimming(replicationServer);
       cnIndexDB.setPurgeDelay(0);
 
       // Prepare data to be stored in the db
@@ -229,8 +212,6 @@
     }
     finally
     {
-      if (cnIndexDB != null)
-        cnIndexDB.shutdown();
       remove(replicationServer);
     }
   }

--
Gitblit v1.10.0