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

pandaapo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/eventmesh.git


The following commit(s) were added to refs/heads/master by this push:
     new 5850d5414 [ISSUE #4711] Properly close resources used by 
WatchFileManagerTest (#4716)
5850d5414 is described below

commit 5850d5414b6ae21646cd9194dc4e93fb5d7dedf1
Author: Tyler Bertrand <[email protected]>
AuthorDate: Tue Jan 9 20:41:55 2024 -0600

    [ISSUE #4711] Properly close resources used by WatchFileManagerTest (#4716)
    
    * Properly close resources used by WatchFileManagerTest
    
    * Create WatchService without try-with-resources
    
    The WatchService was being prematurely auto-closed, resulting in 
ClosedWatchService exceptions
    
    * Restructure WatchFileManagerTest.testWatchFile to assert the file change 
event is detected
    
    * Load properties file prior to creating FileWriter in WatchFileManagerTest
    
    This change ensures the properties in tempConfigFile are loaded into the 
properties object before the tempConfigFile is cleared by creating the new 
FileWriter
    
    * Verify onChange is called at least once
    
    Depending on when the WatchService begins monitoring for file changes, it 
is possible that creating the new FileWriter, which clears the provided file, 
will trigger an onChange call, in addition to the onChange call triggered by 
properties.store().
    
    * Close WatchService in WatchFileTask.shutdown()
---
 .../eventmesh/common/file/WatchFileTask.java       | 22 ++++++++--
 .../common/file/WatchFileManagerTest.java          | 47 +++++++++++-----------
 2 files changed, 42 insertions(+), 27 deletions(-)

diff --git 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/file/WatchFileTask.java
 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/file/WatchFileTask.java
index ac0654022..876fa9675 100644
--- 
a/eventmesh-common/src/main/java/org/apache/eventmesh/common/file/WatchFileTask.java
+++ 
b/eventmesh-common/src/main/java/org/apache/eventmesh/common/file/WatchFileTask.java
@@ -19,6 +19,7 @@ package org.apache.eventmesh.common.file;
 
 import org.apache.eventmesh.common.utils.LogUtils;
 
+import java.io.IOException;
 import java.nio.file.FileSystem;
 import java.nio.file.FileSystems;
 import java.nio.file.Path;
@@ -55,11 +56,21 @@ public class WatchFileTask extends Thread {
             throw new IllegalArgumentException("must be a file directory : " + 
directoryPath);
         }
 
-        try (WatchService watchService = FILE_SYSTEM.newWatchService()) {
-            this.watchService = watchService;
+        try {
+            this.watchService = FILE_SYSTEM.newWatchService();
+        } catch (IOException ex) {
+            throw new RuntimeException("WatchService initialization fail", ex);
+        }
+
+        try {
             path.register(this.watchService, StandardWatchEventKinds.OVERFLOW, 
StandardWatchEventKinds.ENTRY_MODIFY,
                 StandardWatchEventKinds.ENTRY_CREATE, 
StandardWatchEventKinds.ENTRY_DELETE);
-        } catch (Exception ex) {
+        } catch (IOException ex) {
+            try {
+                this.watchService.close();
+            } catch (IOException e) {
+                ex.addSuppressed(e);
+            }
             throw new UnsupportedOperationException("WatchService registry 
fail", ex);
         }
     }
@@ -72,6 +83,11 @@ public class WatchFileTask extends Thread {
 
     public void shutdown() {
         watch = false;
+        try {
+            this.watchService.close();
+        } catch (IOException e) {
+            throw new RuntimeException("Unable to close WatchService", e);
+        }
     }
 
     @Override
diff --git 
a/eventmesh-common/src/test/java/org/apache/eventmesh/common/file/WatchFileManagerTest.java
 
b/eventmesh-common/src/test/java/org/apache/eventmesh/common/file/WatchFileManagerTest.java
index 8a3c024ab..0d3163de4 100644
--- 
a/eventmesh-common/src/test/java/org/apache/eventmesh/common/file/WatchFileManagerTest.java
+++ 
b/eventmesh-common/src/test/java/org/apache/eventmesh/common/file/WatchFileManagerTest.java
@@ -17,8 +17,6 @@
 
 package org.apache.eventmesh.common.file;
 
-import org.apache.eventmesh.common.utils.ThreadUtils;
-
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
@@ -26,11 +24,11 @@ import java.io.FileWriter;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.util.Properties;
-import java.util.concurrent.TimeUnit;
 
-import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
+import org.mockito.ArgumentMatcher;
+import org.mockito.Mockito;
 
 public class WatchFileManagerTest {
 
@@ -40,31 +38,32 @@ public class WatchFileManagerTest {
     @Test
     public void testWatchFile() throws IOException, InterruptedException {
         String file = 
WatchFileManagerTest.class.getResource("/configuration.properties").getFile();
-        File f = new File(file);
+        File configFile = new File(file);
         File tempConfigFile = new File(tempConfigDir, 
"configuration.properties");
-        Files.copy(f.toPath(), tempConfigFile.toPath());
-
-        final FileChangeListener fileChangeListener = new FileChangeListener() 
{
+        Files.copy(configFile.toPath(), tempConfigFile.toPath());
 
-            @Override
-            public void onChanged(FileChangeContext changeContext) {
-                Assertions.assertEquals(tempConfigFile.getName(), 
changeContext.getFileName());
-                Assertions.assertEquals(tempConfigFile.getParent(), 
changeContext.getDirectoryPath());
-            }
+        final FileChangeListener mockFileChangeListener = 
Mockito.mock(FileChangeListener.class);
+        Mockito.when(mockFileChangeListener.support(
+                Mockito.argThat(isFileUnderTest(tempConfigFile.getParent(), 
tempConfigFile.getName())))
+        ).thenReturn(true);
 
-            @Override
-            public boolean support(FileChangeContext changeContext) {
-                return 
changeContext.getWatchEvent().context().toString().contains(tempConfigFile.getName());
-            }
-        };
-        
WatchFileManager.registerFileChangeListener(tempConfigFile.getParent(), 
fileChangeListener);
+        
WatchFileManager.registerFileChangeListener(tempConfigFile.getParent(), 
mockFileChangeListener);
 
         Properties properties = new Properties();
-        properties.load(new BufferedReader(new FileReader(tempConfigFile)));
-        properties.setProperty("eventMesh.server.newAdd", "newAdd");
-        FileWriter fw = new FileWriter(tempConfigFile);
-        properties.store(fw, "newAdd");
+        try (BufferedReader bufferedReader = new BufferedReader(new 
FileReader(tempConfigFile))) {
+            properties.load(bufferedReader);
+        }
+
+        try (FileWriter fw = new FileWriter(tempConfigFile)) {
+            properties.setProperty("eventMesh.server.newAdd", "newAdd");
+            properties.store(fw, "newAdd");
+        }
+
+        Mockito.verify(mockFileChangeListener, 
Mockito.timeout(15_000).atLeastOnce())
+                
.onChanged(Mockito.argThat(isFileUnderTest(tempConfigFile.getParent(), 
tempConfigFile.getName())));
+    }
 
-        ThreadUtils.sleep(500, TimeUnit.MILLISECONDS);
+    private ArgumentMatcher<FileChangeContext> isFileUnderTest(String 
directoryPath, String fileName) {
+        return argument -> argument.getDirectoryPath().equals(directoryPath) 
&& argument.getFileName().equals(fileName);
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to