mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

davidely
17.32.2007 44f577424c76e320d1460cff6bc5cc81aa70a28f
Fixed race conditions in operation test cases by having them be more robust to how they check that a response was sent
5 files modified
88 ■■■■■ changed files
opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java 9 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/core/AddOperationTestCase.java 16 ●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/core/DeleteOperationTestCase.java 9 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/core/ModifyOperationTestCase.java 21 ●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/core/OperationTestCase.java 33 ●●●●● patch | view | raw | blame | history
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.
    }
  }
}
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
    {
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}
   */
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) {}
  }
}
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);
  }
}