From ae8c5336f3a8e1fc82d65d0c0ba12f78ad02c13e Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Mon, 30 Jun 2014 12:49:28 +0000
Subject: [PATCH] Chasing random test failures. Added better diagnostics to ResourceLimitsPolicyTest in case of test failure.

---
 /dev/null                                                                                                    |  209 ----------------------------------
 opends/tests/unit-tests-testng/src/server/org/opends/server/core/networkgroups/ResourceLimitsPolicyTest.java |  150 ++++++++++--------------
 2 files changed, 61 insertions(+), 298 deletions(-)

diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/core/networkgroups/MockResourceLimitsQOSPolicyCfg.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/core/networkgroups/MockResourceLimitsQOSPolicyCfg.java
deleted file mode 100644
index 94eb560..0000000
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/core/networkgroups/MockResourceLimitsQOSPolicyCfg.java
+++ /dev/null
@@ -1,209 +0,0 @@
-/*
- * CDDL HEADER START
- *
- * The contents of this file are subject to the terms of the
- * Common Development and Distribution License, Version 1.0 only
- * (the "License").  You may not use this file except in compliance
- * with the License.
- *
- * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
- * or http://forgerock.org/license/CDDLv1.0.html.
- * See the License for the specific language governing permissions
- * and limitations under the License.
- *
- * When distributing Covered Code, include this CDDL HEADER in each
- * file and include the License file at legal-notices/CDDLv1_0.txt.
- * If applicable, add the following below this CDDL HEADER, with the
- * fields enclosed by brackets "[]" replaced with your own identifying
- * information:
- *      Portions Copyright [yyyy] [name of copyright owner]
- *
- * CDDL HEADER END
- *
- *
- *      Copyright 2009-2010 Sun Microsystems, Inc.
- */
-
-package org.opends.server.core.networkgroups;
-
-
-
-import org.opends.server.admin.server.ConfigurationChangeListener;
-import org.opends.server.admin.std.server.QOSPolicyCfg;
-import org.opends.server.admin.std.server.ResourceLimitsQOSPolicyCfg;
-import org.opends.server.types.DN;
-
-
-
-/**
- * Stub configuration used in tests.
- */
-public abstract class MockResourceLimitsQOSPolicyCfg implements
-    ResourceLimitsQOSPolicyCfg
-{
-
-  /**
-   * {@inheritDoc}
-   */
-  public final void addChangeListener(
-      ConfigurationChangeListener<QOSPolicyCfg> listener)
-  {
-    // Stub.
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public final void removeChangeListener(
-      ConfigurationChangeListener<QOSPolicyCfg> listener)
-  {
-    // Stub.
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public final DN dn()
-  {
-    // Stub.
-    return null;
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public final void addResourceLimitsChangeListener(
-      ConfigurationChangeListener<ResourceLimitsQOSPolicyCfg> listener)
-  {
-    // Stub.
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public final Class<? extends ResourceLimitsQOSPolicyCfg> configurationClass()
-  {
-    // Stub.
-    return null;
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public final void removeResourceLimitsChangeListener(
-      ConfigurationChangeListener<ResourceLimitsQOSPolicyCfg> listener)
-  {
-    // Stub.
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public String getJavaClass()
-  {
-    // Stub.
-    return null;
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public int getMaxConcurrentOpsPerConnection()
-  {
-    return 0;
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public int getMaxConnections()
-  {
-    return 0;
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public int getMaxConnectionsFromSameIP()
-  {
-    return 0;
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public int getMaxOpsPerConnection()
-  {
-    return 0;
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public int getMinSubstringLength()
-  {
-    return 0;
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public Integer getSizeLimit()
-  {
-    return null;
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public Long getTimeLimit()
-  {
-    return null;
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public long getMaxOpsInterval()
-  {
-    return 1;
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public int getMaxOpsPerInterval()
-  {
-    return 0;
-  }
-}
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/core/networkgroups/ResourceLimitsPolicyTest.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/core/networkgroups/ResourceLimitsPolicyTest.java
index 1620c23..8d207b3 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/core/networkgroups/ResourceLimitsPolicyTest.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/core/networkgroups/ResourceLimitsPolicyTest.java
@@ -22,7 +22,7 @@
  *
  *
  *      Copyright 2006-2010 Sun Microsystems, Inc.
- *      Portions copyright 2013 ForgeRock AS
+ *      Portions copyright 2013-2014 ForgeRock AS
  */
 package org.opends.server.core.networkgroups;
 
@@ -32,6 +32,8 @@
 import org.opends.messages.Message;
 import org.opends.server.DirectoryServerTestCase;
 import org.opends.server.TestCaseUtils;
+import org.opends.server.admin.std.server.ResourceLimitsQOSPolicyCfg;
+import org.opends.server.api.ClientConnection;
 import org.opends.server.protocols.internal.InternalClientConnection;
 import org.opends.server.protocols.internal.InternalSearchOperation;
 import org.opends.server.protocols.ldap.LDAPFilter;
@@ -42,6 +44,7 @@
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
+import static org.mockito.Mockito.*;
 import static org.testng.Assert.*;
 
 /**
@@ -82,7 +85,7 @@
   @DataProvider (name = "SearchFilterSet")
   public Object[][] initSearchFilterSet()
   {
-    Object[][] myData = {
+    return new Object[][] {
       // Presence filter
       { 5, "(cn=*)", true},
       // Substring filter
@@ -100,8 +103,6 @@
       { 5, "(|(objectclass=*)(cn=Dir*))", false },
       { 5, "(|(objectclass=*)(cn=Direc*))",  true }
     };
-
-    return myData;
   }
 
 
@@ -113,76 +114,50 @@
 
   /**
    * Tests the max number of connections resource limit.
-   * @throws Exception If the test failed unexpectedly.
    */
   @Test (groups = "virtual")
-  public void testMaxNumberOfConnections()
-          throws Exception
+  public void testMaxNumberOfConnections() throws Exception
   {
-    List<Message> messages = new ArrayList<Message>();
-
-    ResourceLimitsPolicyFactory factory = new ResourceLimitsPolicyFactory();
-    ResourceLimitsPolicy limits =
-        factory.createQOSPolicy(new MockResourceLimitsQOSPolicyCfg()
-              {
-
-                @Override
-                public int getMaxConnections()
-                {
-                  return 1;
-                }
-
-              });
+    final ResourceLimitsQOSPolicyCfg cfg = mock(ResourceLimitsQOSPolicyCfg.class);
+    when(cfg.getMaxConnections()).thenReturn(1);
+    final ResourceLimitsPolicy limits = createQOSPolicy(cfg);
 
     InternalClientConnection conn1 = new InternalClientConnection(DN.NULL_DN);
     limits.addConnection(conn1);
 
-    assertTrue(limits.isAllowed(conn1, null, true, messages));
+    assertOperationIsAllowed(limits, conn1, null, true);
 
     InternalClientConnection conn2 = new InternalClientConnection(DN.NULL_DN);
     limits.addConnection(conn2);
-    assertFalse(limits.isAllowed(conn2, null, true, messages));
+    assertOperationIsAllowed(limits, conn2, null, false);
 
     limits.removeConnection(conn1);
-    assertTrue(limits.isAllowed(conn2, null, true, messages));
+    assertOperationIsAllowed(limits, conn2, null, true);
 
     limits.removeConnection(conn2);
   }
 
   /**
    * Tests the max number of connections from same IP resource limit.
-   * @throws Exception If the test failed unexpectedly.
    */
   @Test (groups = "virtual")
-  public void testMaxNumberOfConnectionsFromSameIp()
-          throws Exception
+  public void testMaxNumberOfConnectionsFromSameIp() throws Exception
   {
-    List<Message> messages = new ArrayList<Message>();
-
-    ResourceLimitsPolicyFactory factory = new ResourceLimitsPolicyFactory();
-    ResourceLimitsPolicy limits =
-        factory.createQOSPolicy(new MockResourceLimitsQOSPolicyCfg()
-              {
-
-                @Override
-                public int getMaxConnectionsFromSameIP()
-                {
-                  return 1;
-                }
-
-              });
+    final ResourceLimitsQOSPolicyCfg cfg = mock(ResourceLimitsQOSPolicyCfg.class);
+    when(cfg.getMaxConnectionsFromSameIP()).thenReturn(1);
+    final ResourceLimitsPolicy limits = createQOSPolicy(cfg);
 
     InternalClientConnection conn1 = new InternalClientConnection(DN.NULL_DN);
     limits.addConnection(conn1);
 
-    assertTrue(limits.isAllowed(conn1, null, true, messages));
+    assertOperationIsAllowed(limits, conn1, null, true);
 
     InternalClientConnection conn2 = new InternalClientConnection(DN.NULL_DN);
     limits.addConnection(conn2);
-    assertFalse(limits.isAllowed(conn2, null, true, messages));
+    assertOperationIsAllowed(limits, conn2, null, false);
 
     limits.removeConnection(conn1);
-    assertTrue(limits.isAllowed(conn2, null, true, messages));
+    assertOperationIsAllowed(limits, conn2, null, true);
 
     limits.removeConnection(conn2);
   }
@@ -192,7 +167,6 @@
    * @param minLength minimum search filter substring length
    * @param searchFilter the search filter to test
    * @param success boolean indicating the expected result
-   * @throws Exception If the test failed unexpectedly.
    */
   @Test (dataProvider = "SearchFilterSet", groups = "virtual")
   public void testMinSubstringLength(
@@ -201,20 +175,9 @@
           boolean success)
           throws Exception
   {
-    List<Message> messages = new ArrayList<Message>();
-
-    ResourceLimitsPolicyFactory factory = new ResourceLimitsPolicyFactory();
-    ResourceLimitsPolicy limits =
-        factory.createQOSPolicy(new MockResourceLimitsQOSPolicyCfg()
-              {
-
-                @Override
-                public int getMinSubstringLength()
-                {
-                  return minLength;
-                }
-
-              });
+    final ResourceLimitsQOSPolicyCfg cfg = mock(ResourceLimitsQOSPolicyCfg.class);
+    when(cfg.getMinSubstringLength()).thenReturn(minLength);
+    final ResourceLimitsPolicy limits = createQOSPolicy(cfg);
 
     InternalClientConnection conn1 = new InternalClientConnection(DN.NULL_DN);
     limits.addConnection(conn1);
@@ -224,37 +187,22 @@
         SearchScope.BASE_OBJECT,
         LDAPFilter.decode(searchFilter).toSearchFilter());
 
-    assertEquals(limits.isAllowed(conn1, search, true, messages), success);
+    assertOperationIsAllowed(limits, conn1, search, success);
     limits.removeConnection(conn1);
   }
 
-
   /**
    * Tests the 'max number of operations per interval' resource limit.
-   * @throws Exception If the test failed unexpectedly.
    */
   @Test (groups = "virtual")
-  public void testMaxThroughput()
-          throws Exception
+  public void testMaxThroughput() throws Exception
   {
-    List<Message> messages = new ArrayList<Message>();
     final long interval = 1000; // Unit is milliseconds
 
-    ResourceLimitsPolicyFactory factory = new ResourceLimitsPolicyFactory();
-    ResourceLimitsPolicy limits = factory.createQOSPolicy(
-      new MockResourceLimitsQOSPolicyCfg() {
-        @Override
-        public int getMaxOpsPerInterval()
-        {
-          return 1;
-        }
-
-        @Override
-        public long getMaxOpsInterval()
-        {
-          return interval;
-        }
-      });
+    final ResourceLimitsQOSPolicyCfg cfg = mock(ResourceLimitsQOSPolicyCfg.class);
+    when(cfg.getMaxOpsPerInterval()).thenReturn(1);
+    when(cfg.getMaxOpsInterval()).thenReturn(interval);
+    final ResourceLimitsPolicy limits = createQOSPolicy(cfg);
 
     InternalClientConnection conn = new InternalClientConnection(DN.NULL_DN);
     limits.addConnection(conn);
@@ -262,23 +210,47 @@
     final DN dn = DN.decode("dc=example,dc=com");
     final SearchFilter all = SearchFilter.createFilterFromString("(objectclass=*)");
 
-    // First operation is allowed
-    InternalSearchOperation search1 =
+    final InternalSearchOperation search1 =
         conn.processSearch(dn, SearchScope.BASE_OBJECT, all);
-    assertTrue(limits.isAllowed(conn, search1, true, messages));
+    assertOperationIsAllowed(limits, conn, search1, true,
+        "First operation should be allowed");
 
-    // Second operation in the same interval is refused
-    InternalSearchOperation search2 =
+    final InternalSearchOperation search2 =
         conn.processSearch(dn, SearchScope.BASE_OBJECT, all);
-    assertFalse(limits.isAllowed(conn, search2, true, messages));
+    assertOperationIsAllowed(limits, conn,
+        search2, false,
+        "Second operation in the same interval should be disallowed");
 
     // Wait for the end of the interval => counters are reset
     Thread.sleep(interval);
 
-    // The operation is allowed
-    InternalSearchOperation search3 =
+    final InternalSearchOperation search3 =
         conn.processSearch(dn, SearchScope.BASE_OBJECT, all);
-    assertTrue(limits.isAllowed(conn, search3, true, messages));
+    assertOperationIsAllowed(limits, conn, search3, true,
+        "Third operation should be allowed");
+  }
+
+  private void assertOperationIsAllowed(ResourceLimitsPolicy limits,
+      ClientConnection conn, InternalSearchOperation operation, boolean expected)
+  {
+    assertOperationIsAllowed(limits, conn, operation, expected, null);
+  }
+
+  private void assertOperationIsAllowed(ResourceLimitsPolicy limits,
+      ClientConnection conn, InternalSearchOperation operation,
+      boolean expected, String assertMsg)
+  {
+    final String msg = assertMsg != null ? assertMsg :
+      "Operation should be " + (expected ? "" : "dis") + "allowed";
+
+    final List<Message> messages = new ArrayList<Message>();
+    final boolean actual = limits.isAllowed(conn, operation, true, messages);
+    assertEquals(actual, expected, msg + ". Messages=" + messages);
+  }
+
+  private ResourceLimitsPolicy createQOSPolicy(ResourceLimitsQOSPolicyCfg cfg) throws Exception
+  {
+    return new ResourceLimitsPolicyFactory().createQOSPolicy(cfg);
   }
 
 }

--
Gitblit v1.10.0