You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2022/09/23 11:49:13 UTC

[commons-collections] 01/03: Fix flaky test failure in SynchronizedBagTest#testCollectionToArray2

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git

commit 30a9ab835dadfde94d0daa15e9a8447843076abf
Author: Partha-SUST16 <pp...@gmail.com>
AuthorDate: Fri Sep 23 12:07:45 2022 +0100

    Fix flaky test failure in SynchronizedBagTest#testCollectionToArray2
    
    Closes #336
---
 .../commons/collections4/bag/AbstractBagTest.java  |  4 ++
 .../collections4/bag/SynchronizedBagTest.java      |  5 +++
 .../collection/AbstractCollectionTest.java         | 48 +++++++++++++++++++---
 3 files changed, 52 insertions(+), 5 deletions(-)

diff --git a/src/test/java/org/apache/commons/collections4/bag/AbstractBagTest.java b/src/test/java/org/apache/commons/collections4/bag/AbstractBagTest.java
index 05e93dc98..62e6df003 100644
--- a/src/test/java/org/apache/commons/collections4/bag/AbstractBagTest.java
+++ b/src/test/java/org/apache/commons/collections4/bag/AbstractBagTest.java
@@ -674,6 +674,10 @@ public abstract class AbstractBagTest<T> extends AbstractCollectionTest<T> {
             super.verify();
         }
 
+        @Override
+        protected int getIterationBehaviour(){
+            return AbstractBagTest.this.getIterationBehaviour();
+        }
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/collections4/bag/SynchronizedBagTest.java b/src/test/java/org/apache/commons/collections4/bag/SynchronizedBagTest.java
index 06cd875d4..e01b2d56e 100644
--- a/src/test/java/org/apache/commons/collections4/bag/SynchronizedBagTest.java
+++ b/src/test/java/org/apache/commons/collections4/bag/SynchronizedBagTest.java
@@ -46,6 +46,11 @@ public class SynchronizedBagTest<T> extends AbstractBagTest<T> {
         return "4";
     }
 
+    @Override
+    protected int getIterationBehaviour(){
+        return UNORDERED;
+    }
+
 //    public void testCreate() throws Exception {
 //        Bag<T> bag = makeObject();
 //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/SynchronizedBag.emptyCollection.version4.obj");
diff --git a/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java b/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java
index 6896fb5f6..fc7b84edb 100644
--- a/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java
+++ b/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java
@@ -70,6 +70,13 @@ import org.junit.jupiter.api.Test;
  * <li>{@link #isFailFastSupported()}
  * </ul>
  * <p>
+ * <b>Indicate Collection Behaviour</b>
+ * <p>
+ * Override these if your collection makes specific behaviour guarantees:
+ * <ul>
+ * <li>{@link #getIterationBehaviour()}</li>
+ * </ul>
+ * <p>
  * <b>Fixture Methods</b>
  * <p>
  * Fixtures are used to verify that the operation results in correct state
@@ -134,6 +141,13 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
     // tests on Collection.equals nor any for Collection.hashCode.
     //
 
+    /**
+     * Flag to indicate the collection makes no ordering guarantees for the iterator. If this is not used
+     * then the behaviour is assumed to be ordered and the output order of the iterator is matched by
+     * the toArray method.
+     */
+    protected static final int UNORDERED = 0x1;
+
     // These fields are used by reset() and verify(), and any test
     // method that tests a modification.
 
@@ -487,6 +501,18 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
         };
     }
 
+    /**
+     * Return a flag specifying the iteration behaviour of the collection.
+     * This is used to change the assertions used by specific tests.
+     * The default implementation returns 0 which indicates ordered iteration behaviour.
+     *
+     * @return the iteration behaviour
+     * @see #UNORDERED
+     */
+    protected int getIterationBehaviour(){
+        return 0;
+    }
+
     // Tests
     /**
      *  Tests {@link Collection#add(Object)}.
@@ -1095,9 +1121,14 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
 
         array = getCollection().toArray(new Object[0]);
         a = getCollection().toArray();
-        assertEquals("toArrays should be equal",
-                     Arrays.asList(array), Arrays.asList(a));
 
+        if ((getIterationBehaviour() & UNORDERED) != 0) {
+            assertTrue("toArrays should contain the same elements",
+                    array.length == a.length &&
+                    (new HashSet<>(Arrays.asList(array)).equals(new HashSet<>(Arrays.asList(a)))));
+        } else {
+            assertEquals("toArrays should be equal", Arrays.asList(array), Arrays.asList(a));
+        }
         // Figure out if they're all the same class
         // TODO: It'd be nicer to detect a common superclass
         final HashSet<Class<?>> classes = new HashSet<>();
@@ -1116,9 +1147,16 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
         array = getCollection().toArray(a);
         assertEquals("toArray(Object[]) should return correct array type",
                 a.getClass(), array.getClass());
-        assertEquals("type-specific toArrays should be equal",
-                Arrays.asList(array),
-                Arrays.asList(getCollection().toArray()));
+
+        if ((getIterationBehaviour() & UNORDERED) != 0) {
+            assertTrue("type-specific toArrays should contain the same elements",
+                    array.length == getCollection().toArray().length &&
+                    (new HashSet<>(Arrays.asList(array))).equals(new HashSet<>(Arrays.asList(getCollection().toArray()))));
+        } else {
+            assertEquals("type-specific toArrays should be equal",
+                    Arrays.asList(array),
+                    Arrays.asList(getCollection().toArray()));
+        }
         verify();
     }