You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/11/26 17:10:19 UTC

[commons-vfs] 16/16: Attempt to fix random failures in DefaultFileMonitorTest, specifically in testFileCreated().

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

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

commit 41669f286a634f5a077762e8a49d3b96484e941d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Nov 26 12:09:59 2021 -0500

    Attempt to fix random failures in DefaultFileMonitorTest, specifically
    in testFileCreated().
    
    java.lang.AssertionError: Incorrect event CHANGED expected:<CREATED> but
    was:<CHANGED>
            at org.junit.Assert.fail(Assert.java:89)
            at org.junit.Assert.failNotEquals(Assert.java:835)
            at org.junit.Assert.assertEquals(Assert.java:120)
            at org.apache.commons.vfs2.impl.DefaultFileMonitorTest.waitFor(DefaultFileMonitorTest.java:335)
            at org.apache.commons.vfs2.impl.DefaultFileMonitorTest.testFileCreated(DefaultFileMonitorTest.java:233)
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
    Method)
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.base/java.lang.reflect.Method.invoke(Method.java:566)
            at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
            at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
            at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
            at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
            at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
            at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
            at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
            at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
            at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
            at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
            at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
            at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
            at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
            at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
            at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
            at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
            at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
            at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
            at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:364)
            at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:272)
            at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:237)
            at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:158)
            at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:428)
            at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:162)
            at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:562)
            at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:548)
---
 .../commons/vfs2/impl/DefaultFileMonitorTest.java  | 58 +++++++++++++---------
 1 file changed, 35 insertions(+), 23 deletions(-)

diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileMonitorTest.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileMonitorTest.java
index bd0ee88..89d461f 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileMonitorTest.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileMonitorTest.java
@@ -25,8 +25,10 @@ import java.io.File;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.Objects;
 import java.util.concurrent.atomic.AtomicLong;
-import java.util.concurrent.atomic.AtomicReference;
 
 import org.apache.commons.vfs2.AbstractVfsTestCase;
 import org.apache.commons.vfs2.FileChangeEvent;
@@ -65,6 +67,10 @@ public class DefaultFileMonitorTest {
         }
     }
 
+    private enum PeekLocation {
+        FIRST, LAST
+    }
+
     private enum Status {
         CHANGED, CREATED, DELETED
     }
@@ -73,17 +79,17 @@ public class DefaultFileMonitorTest {
 
         @Override
         public void fileChanged(final FileChangeEvent event) throws Exception {
-            status.set(Status.CHANGED);
+            status.add(Status.CHANGED);
         }
 
         @Override
         public void fileCreated(final FileChangeEvent event) throws Exception {
-            status.set(Status.CREATED);
+            status.add(Status.CREATED);
         }
 
         @Override
         public void fileDeleted(final FileChangeEvent event) throws Exception {
-            status.set(Status.DELETED);
+            status.add(Status.DELETED);
         }
     }
 
@@ -91,7 +97,7 @@ public class DefaultFileMonitorTest {
 
     private FileSystemManager fileSystemManager;
 
-    private final AtomicReference<Status> status = new AtomicReference<>();
+    private final Deque<Status> status = new ArrayDeque<>();
 
     private File testDir;
 
@@ -104,8 +110,14 @@ public class DefaultFileMonitorTest {
         }
     }
 
-    private Status getStatus() {
-        return status.get();
+    private Status getStatus(final PeekLocation peekLocation) {
+        switch (Objects.requireNonNull(peekLocation, "peekLocation")) {
+        case FIRST:
+            return status.peekFirst();
+        case LAST:
+            return status.peekLast();
+        }
+        throw new IllegalStateException();
     }
 
     /**
@@ -175,7 +187,7 @@ public class DefaultFileMonitorTest {
     }
 
     private void resetStatus() {
-        status.set(null);
+        status.clear();
     }
 
     @Before
@@ -205,7 +217,7 @@ public class DefaultFileMonitorTest {
                 Thread.sleep(DELAY_MILLIS * 5);
                 testFile.delete();
                 Thread.sleep(DELAY_MILLIS * 30);
-                assertNull("Event should not have occurred", getStatus());
+                assertNull("Event should not have occurred", getStatus(PeekLocation.LAST));
             }
         }
     }
@@ -222,11 +234,11 @@ public class DefaultFileMonitorTest {
                 resetStatus();
                 Thread.sleep(DELAY_MILLIS * 5);
                 testFile.delete();
-                waitFor(Status.DELETED, DELAY_MILLIS * 30);
+                waitFor(Status.DELETED, DELAY_MILLIS * 30, PeekLocation.LAST);
                 resetStatus();
                 Thread.sleep(DELAY_MILLIS * 5);
                 writeToFile(testFile);
-                waitFor(Status.CREATED, DELAY_MILLIS * 30);
+                waitFor(Status.CREATED, DELAY_MILLIS * 30, PeekLocation.LAST);
             }
         }
     }
@@ -241,7 +253,7 @@ public class DefaultFileMonitorTest {
                 monitor.start();
                 writeToFile(testFile);
                 Thread.sleep(DELAY_MILLIS * 5);
-                waitFor(Status.CREATED, DELAY_MILLIS * 5);
+                waitFor(Status.CREATED, DELAY_MILLIS * 5, PeekLocation.FIRST);
             }
         }
     }
@@ -256,7 +268,7 @@ public class DefaultFileMonitorTest {
                 monitor.addFile(fileObject);
                 monitor.start();
                 testFile.delete();
-                waitFor(Status.DELETED, DELAY_MILLIS * 5);
+                waitFor(Status.DELETED, DELAY_MILLIS * 5, PeekLocation.LAST);
             }
         }
     }
@@ -276,7 +288,7 @@ public class DefaultFileMonitorTest {
                 final long valueMillis = System.currentTimeMillis();
                 final boolean rcMillis = testFile.setLastModified(valueMillis);
                 assertTrue("setLastModified succeeded", rcMillis);
-                waitFor(Status.CHANGED, DELAY_MILLIS * 5);
+                waitFor(Status.CHANGED, DELAY_MILLIS * 5, PeekLocation.LAST);
             }
         }
     }
@@ -300,7 +312,7 @@ public class DefaultFileMonitorTest {
             monitor.start();
             try {
                 testFile.delete();
-                waitFor(Status.DELETED, DELAY_MILLIS * 5);
+                waitFor(Status.DELETED, DELAY_MILLIS * 5, PeekLocation.LAST);
             } finally {
                 monitor.stop();
             }
@@ -316,21 +328,21 @@ public class DefaultFileMonitorTest {
                 monitor.addFile(fileObject);
                 monitor.start();
                 writeToFile(testFile);
-                waitFor(Status.CREATED, DELAY_MILLIS * 10);
+                waitFor(Status.CREATED, DELAY_MILLIS * 10, PeekLocation.LAST);
                 resetStatus();
                 testFile.delete();
-                waitFor(Status.DELETED, DELAY_MILLIS * 10);
+                waitFor(Status.DELETED, DELAY_MILLIS * 10, PeekLocation.LAST);
                 resetStatus();
                 Thread.sleep(DELAY_MILLIS * 5);
                 monitor.addFile(fileObject);
                 writeToFile(testFile);
-                waitFor(Status.CREATED, DELAY_MILLIS * 10);
+                waitFor(Status.CREATED, DELAY_MILLIS * 10, PeekLocation.LAST);
             }
         }
     }
 
-    private void waitFor(final Status expected, final long timeoutMillis) throws InterruptedException {
-        if (expected == getStatus()) {
+    private void waitFor(final Status expected, final long timeoutMillis, final PeekLocation peekLocation) throws InterruptedException {
+        if (expected == getStatus(peekLocation)) {
             return;
         }
         long remaining = timeoutMillis;
@@ -338,12 +350,12 @@ public class DefaultFileMonitorTest {
         while (remaining > 0) {
             Thread.sleep(interval);
             remaining -= interval;
-            if (expected == getStatus()) {
+            if (expected == getStatus(peekLocation)) {
                 return;
             }
         }
-        assertNotNull("No event occurred", getStatus());
-        assertEquals("Incorrect event " + getStatus(), expected, getStatus());
+        assertNotNull("No event occurred", getStatus(peekLocation));
+        assertEquals("Incorrect event " + getStatus(peekLocation), expected, getStatus(peekLocation));
     }
 
     private void writeToFile(final File file) throws IOException {