tylerbertrand commented on code in PR #4716:
URL: https://github.com/apache/eventmesh/pull/4716#discussion_r1444860760


##########
eventmesh-common/src/test/java/org/apache/eventmesh/common/file/WatchFileManagerTest.java:
##########
@@ -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(configFile));
+                FileWriter fw = new FileWriter(tempConfigFile)
+        ) {
+            properties.load(bufferedReader);
+            properties.setProperty("eventMesh.server.newAdd", "newAdd");
+            properties.store(fw, "newAdd");
+        }
+
+        Mockito.verify(mockFileChangeListener, Mockito.timeout(15_000))

Review Comment:
   I bet it is a timing thing. Creating the `FileWriter` actually clears the 
file, so depending on when the `WatchService` begins monitoring the file, it 
may pick that up as well, resulting in two calls to `onChanged()`. I think it 
makes sense to verify it's called at least once, like you suggest.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to