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

46
LINES

< > BotCompany Repo | #1015923 // NIO WatchService Self-Test

JavaX source code (desktop) [tags: archive use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (5336L/34K).

!7

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

p {
  File dir = programDir();
  deleteAllProgramFiles();
  makeProgramDir();
  final Path path = dir.toPath();
  
  final WatchService watchService = FileSystems.getDefault().newWatchService();
  final WatchKey watchKey = path.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY, OVERFLOW);
  
  thread {
    sleepSeconds(2);
    saveProgramTextFile("bla", "1");
    sleepSeconds(1);
    //watchKey.cancel();
    watchService.close();
  }
  
  final new Set<S> changes;
  final Countdown countdown = fiveSeconds();
  print("Waiting");
  expectException(ClosedWatchServiceException.class, r {
    while (!countdown.done()) {
      final WatchKey wk = watchService.take();
      print("Key taken");
      
      for (WatchEvent<?> event : wk.pollEvents()) pcall {
        final Path changed = (Path) event.context();
        print("Changed: " + changed + ", " + event.kind());
        changes.add(changed.toFile().getName());
      }
      
      bool valid = wk.reset();
      if (!valid)
        print("Key has been unregistered");
    }
  });
    
  //watchService.close();
  bool ok = eq(changes, lithashset("bla", "bla_temp"));
  print("Self-Test: " + ok);
}

Author comment

Began life as a copy of #1013732

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): cfunsshuasjs, mqqgnosmbjvj, tvejysmllsmz

No comments. add comment

Snippet ID: #1015923
Snippet name: NIO WatchService Self-Test
Eternal ID of this version: #1015923/12
Text MD5: a09d8f01434d6111f3abb2c3db3979ee
Transpilation MD5: 7f84b8cb8b35507eff1818165f65d10b
Author: stefan
Category: javax / io
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): Yes
Created/modified: 2018-05-31 01:31:00
Source code size: 1303 bytes / 46 lines
Pitched / IR pitched: No / No
Views / Downloads: 387 / 1017
Version history: 11 change(s)
Referenced in: #1015932 - testJDKWatchService - returns true if OK