Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

46
LINES

< > BotCompany Repo | #1015932 // testJDKWatchService - returns true if OK

JavaX fragment (include)

import java.nio.file.*;
import static java.nio.file.StandardWatchEventKinds.*;

sbool testJDKWatchService() {
  ret recordSuccessOrFailure('testJDKWatchService, testJDKWatchService_noLog());
};

sbool testJDKWatchService_noLog() false on exception {
  final File dir = mkdirs(createTempDir());
  final Path path = dir.toPath();
  
  final WatchService watchService = FileSystems.getDefault().newWatchService();
  final WatchKey watchKey = path.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY, OVERFLOW);
  
  final Set<S> changes = synchroSet();
  thread {
    try {
      saveTextFile(newFile(dir, "bla"), "1");
      Countdown countdown = fiveSeconds();
      while (l(changes) < 2 && !countdown.done())
        sleep(1);
    } finally {
      watchService.close();
    }
  }
  
  final Countdown countdown = fiveSeconds();
  expectException(ClosedWatchServiceException.class, r {
    while (!countdown.done()) {
      final WatchKey wk = watchService.take();

      for (WatchEvent<?> event : wk.pollEvents()) pcall {
        final Path changed = (Path) event.context();
        print("Changed: " + changed + ", " + event.kind());
        changes.add(changed.toFile().getName());
      }
      
      wk.reset();
    }
  });
    
  pcall { watchService.close(); }
  deleteDirectory(dir);
  ret eq(changes, lithashset("bla", "bla_temp"));
  
}

Author comment

Began life as a copy of #1015923

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1015932
Snippet name: testJDKWatchService - returns true if OK
Eternal ID of this version: #1015932/8
Text MD5: 6e237312dbdbe1af8b7b169cb5d8ef10
Author: stefan
Category: javax / io
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-31 14:17:29
Source code size: 1406 bytes / 46 lines
Pitched / IR pitched: No / No
Views / Downloads: 382 / 413
Version history: 7 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1019341 - jdk_newWatchService