From 6e525553f9084a97f740fb0fa54df4acb71c1d56 Mon Sep 17 00:00:00 2001
From: davidely <davidely@localhost>
Date: Wed, 17 Jan 2007 06:32:30 +0000
Subject: [PATCH] Fixed race conditions in operation test cases by having them be more robust to how they check that a response was sent

---
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/DeleteOperationTestCase.java |    9 ++++
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/ModifyOperationTestCase.java |   21 ++++++++--
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/OperationTestCase.java       |   33 ++++++++++++++++
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java                |    9 ++++
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/AddOperationTestCase.java    |   16 ++++++-
 5 files changed, 81 insertions(+), 7 deletions(-)

diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
index b3b884c..5320654 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
@@ -722,4 +722,13 @@
 
     return f.getAbsolutePath();
   }
+
+  /** Convenience method so we don't have to catch InterruptedException everywhere. */
+  public static void sleep(long ms) {
+    try {
+      Thread.sleep(ms);
+    } catch (InterruptedException e) {
+      // Ignore it.
+    }
+  }
 }
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/AddOperationTestCase.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/AddOperationTestCase.java
index 4e1a28c..cd66f54 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/AddOperationTestCase.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/AddOperationTestCase.java
@@ -35,6 +35,7 @@
 
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
+import org.testng.annotations.AfterMethod;
 
 import org.opends.server.TestCaseUtils;
 import org.opends.server.api.Backend;
@@ -64,6 +65,7 @@
 import org.opends.server.types.ObjectClass;
 import org.opends.server.types.ResultCode;
 import org.opends.server.types.WritabilityMode;
+import org.opends.server.types.DirectoryException;
 
 import static org.testng.Assert.*;
 
@@ -78,6 +80,14 @@
 public class AddOperationTestCase
        extends OperationTestCase
 {
+
+  // Some of the tests disable the backends, so we reenable them here.
+  @AfterMethod(alwaysRun=true)
+  public void reenableBackend() throws DirectoryException {
+    Backend b = DirectoryServer.getBackend(DN.decode("o=test"));
+    b.setWritabilityMode(WritabilityMode.ENABLED);
+  }
+
   /**
    * Retrieves a set of add operations that may be used for testing.
    *
@@ -870,7 +880,7 @@
     assertFalse(addResponse.getResultCode() == 0);
 
     assertEquals(ldapStatistics.getAddRequests(), addRequests+1);
-    assertEquals(ldapStatistics.getAddResponses(), addResponses+1);
+    waitForAddResponsesStat(addResponses+1);
 
     try
     {
@@ -1671,7 +1681,7 @@
     assertFalse(addResponse.getResultCode() == 0);
 
     assertEquals(ldapStatistics.getAddRequests(), addRequests+1);
-    assertEquals(ldapStatistics.getAddResponses(), addResponses+1);
+    waitForAddResponsesStat(addResponses+1);
 
     try
     {
@@ -1823,7 +1833,7 @@
     assertFalse(addResponse.getResultCode() == 0);
 
     assertEquals(ldapStatistics.getAddRequests(), addRequests+1);
-    assertEquals(ldapStatistics.getAddResponses(), addResponses+1);
+    waitForAddResponsesStat(addResponses+1);
 
     try
     {
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/DeleteOperationTestCase.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/DeleteOperationTestCase.java
index 6a01495..3e1c70b 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/DeleteOperationTestCase.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/DeleteOperationTestCase.java
@@ -34,6 +34,7 @@
 
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
+import org.testng.annotations.AfterMethod;
 
 import org.opends.server.TestCaseUtils;
 import org.opends.server.api.Backend;
@@ -58,6 +59,7 @@
 import org.opends.server.types.LockManager;
 import org.opends.server.types.ResultCode;
 import org.opends.server.types.WritabilityMode;
+import org.opends.server.types.DirectoryException;
 
 import static org.testng.Assert.*;
 
@@ -71,6 +73,13 @@
 public class DeleteOperationTestCase
        extends OperationTestCase
 {
+  // Some of the tests disable the backends, so we reenable them here.
+  @AfterMethod(alwaysRun=true)
+  public void reenableBackend() throws DirectoryException {
+    Backend b = DirectoryServer.getBackend(DN.decode("o=test"));
+    b.setWritabilityMode(WritabilityMode.ENABLED);
+  }
+
   /**
    * {@inheritDoc}
    */
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/ModifyOperationTestCase.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/ModifyOperationTestCase.java
index 87560ff..73b08df 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/ModifyOperationTestCase.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/ModifyOperationTestCase.java
@@ -35,6 +35,7 @@
 
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
+import org.testng.annotations.AfterMethod;
 
 import org.opends.server.TestCaseUtils;
 import org.opends.server.api.Backend;
@@ -69,6 +70,18 @@
 public class ModifyOperationTestCase
        extends OperationTestCase
 {
+
+  // Some of the tests disable the backends, so we reenable them here.
+  @AfterMethod(alwaysRun=true)
+  public void reenableBackend() throws DirectoryException {
+    Object[][] backendBaseDNs = getBaseDNs();
+    for (int i = 0; i < backendBaseDNs.length; i++) {
+      String backendBaseDN = backendBaseDNs[i][0].toString();
+      Backend b = DirectoryServer.getBackend(DN.decode(backendBaseDN));
+      b.setWritabilityMode(WritabilityMode.ENABLED);
+    }
+  }
+
   /**
    * Retrieves a set of modify operations that may be used for testing.
    *
@@ -279,7 +292,6 @@
 
   @DataProvider(name = "baseDNs")
   public Object[][] getBaseDNs()
-         throws Exception
   {
     return new Object[][] {
          { "dc=example,dc=com"},
@@ -3564,7 +3576,7 @@
     assertFalse(modifyResponse.getResultCode() == 0);
 
     assertEquals(ldapStatistics.getModifyRequests(), modifyRequests+1);
-    assertEquals(ldapStatistics.getModifyResponses(), modifyResponses+1);
+    waitForModifyResponsesStat(modifyResponses+1);
   }
 
 
@@ -3758,7 +3770,7 @@
     assertFalse(modifyResponse.getResultCode() == 0);
 
     assertEquals(ldapStatistics.getModifyRequests(), modifyRequests+1);
-    assertEquals(ldapStatistics.getModifyResponses(), modifyResponses+1);
+    waitForModifyResponsesStat(modifyResponses+1);
 
     DirectoryServer.setWritabilityMode(WritabilityMode.ENABLED);
   }
@@ -3957,7 +3969,7 @@
     assertFalse(modifyResponse.getResultCode() == 0);
 
     assertEquals(ldapStatistics.getModifyRequests(), modifyRequests+1);
-    assertEquals(ldapStatistics.getModifyResponses(), modifyResponses+1);
+    waitForModifyResponsesStat(modifyResponses+1);
 
     b.setWritabilityMode(WritabilityMode.ENABLED);
   }
@@ -4371,5 +4383,6 @@
       s.close();
     } catch (Exception e) {}
   }
+
 }
 
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/OperationTestCase.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/OperationTestCase.java
index dbf1ec6..5fcd526 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/OperationTestCase.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/core/OperationTestCase.java
@@ -33,6 +33,7 @@
 import org.testng.annotations.Test;
 
 import org.opends.server.TestCaseUtils;
+import org.opends.server.util.StaticUtils;
 import org.opends.server.api.ConnectionHandler;
 import org.opends.server.protocols.ldap.LDAPConnectionHandler;
 import org.opends.server.protocols.ldap.LDAPStatistics;
@@ -433,5 +434,37 @@
       assertFalse(operation.getResponseControls().contains(c));
     }
   }
+
+  /**
+   * Checking ldapStatistics.getModifyResponses() too soon can lead to
+   * problems since we can see the response before the statistic is
+   * incremented, so we're a little more forgiving.
+   */
+  protected void waitForModifyResponsesStat(long expectedValue)
+  {
+    for (int i = 0; i < 10; i++) {
+      if (ldapStatistics.getModifyResponses() == expectedValue) {
+        return;
+      }
+      TestCaseUtils.sleep(100);
+    }
+    assertEquals(ldapStatistics.getModifyResponses(), expectedValue);
+  }
+
+  /**
+   * Checking ldapStatistics.getAddResponses() too soon can lead to
+   * problems since we can see the response before the statistic is
+   * incremented, so we're a little more forgiving.
+   */
+  protected void waitForAddResponsesStat(long expectedValue)
+  {
+    for (int i = 0; i < 10; i++) {
+      if (ldapStatistics.getAddResponses() == expectedValue) {
+        return;
+      }
+      TestCaseUtils.sleep(100);
+    }
+    assertEquals(ldapStatistics.getAddResponses(), expectedValue);
+  }
 }
 

--
Gitblit v1.10.0