1

I have a scala class which uses the java nio WatchService to detect creation of new folders in a specific directory.

The WatchService works well when the app is running and I manually copy a folder into the target folder.

I have created a unit test using scalatest that initializes my class and copies a test folder into the target folder using Apache Commons

FileUtils.copyDirectory(testFolder, new File(targetFolder, testFolder.getName), false)

The watch service does not detect any new entry created in the target folder within 30 seconds. My code is inside an eventually block similar to

eventually(timeout(Span(30, Seconds)), interval(Span(1, Seconds))) {
    // CHECK IF THE SERVICE DETECTED THE NEW ENTRY
}

Any idea why this does not work in unit tests?

Fabrizio Fortino
  • 1,479
  • 1
  • 14
  • 22
  • Not sure if this is relevant but in javadoc for `FileUtils.copyDirectory` it says `Note: This method tries to preserve the files' last modified date/times using File.setLastModified(long)`. Maybe try to change last modified date of the folder as a part of the test setup after copying? – Alexander Arendar Feb 29 '16 at 18:11

1 Answers1

1

Just discovered the problem was in the way I used scalatest. I was trying to use a fixture to open/close my service in the features boundaries:

describe("The WatchService") {
  withWatchService { watchService =>
    it("should test feature 1") { /* test code here */ }
    it("should test feature 2") { /* test code here */ }
  }
}

The code above does not work: the watch service is closed before the features are completed. To make it work I have created a unique feature with the fixture nested inside it:

describe("The WatchService") {
  it("should test features") {
    withWatchService { watchService =>
      /* test code here */
    }
  }    
}
Fabrizio Fortino
  • 1,479
  • 1
  • 14
  • 22
  • I have a less complex problem. I am running a watcher service on linux machine, monitoring the folders on that same server. But the watcher is only picking up files when I manually push the documents in those folders (through WinSCP/SSH). The actual scenario is when the files come from Scanner and Fax machines, and sit in these folders. In this case, the watcher service is not picking up files. Any idea what can be the issue ? I already checked the permissions on the incoming file. It is with the root user only, the same user with which my watcher service is running. – Sanket Mehta Oct 07 '19 at 11:27
  • @SanketMehta what kind of events are you listening to when you register the watch service? Can you share your code? – Fabrizio Fortino Oct 08 '19 at 12:22
  • I am listening to all 3 events - CREATE, DELETE and MODIFY. private static void registerRecursive(final Path root,final WatchService watchService,final Map watchKeyToPathMap) { Files.walkFileTree(root, new SimpleFileVisitor() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { WatchKey key = dir.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE); watchKeyToPathMap.put(key, dir); return FileVisitResult.CONTINUE;}});} – Sanket Mehta Oct 09 '19 at 07:40
  • 1
    @SanketMehta have a look at https://stackoverflow.com/questions/48919086/java-watch-service-not-working-for-remote-files-mounted-in-the-local-server?rq=1 Maybe you are running in the same situation? – Fabrizio Fortino Oct 10 '19 at 08:50
  • Thanks a lot @Fabrizio Fortino. This link helped me out resolving the issue – Sanket Mehta Oct 16 '19 at 15:30