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

69
LINES

< > BotCompany Repo | #1029789 // Test FileBasedLock (OK)

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 1113K of libraries. Click here for Pure Java version (8674L/40K).

!7

cprint TestFileBasedLock {
  switchable File file;
  switchable double timeout = 10;
  transient FileBasedLock lock1, lock2;
  transient S contents; // contents for lock file
  
  start-thread {
    if (file == null)
      setField(file := javaxCachesDir("Lock Test/lockfile-" + aGlobalID()));
    print("Using file: " + file);
    
    contents = null;
    regularTest();
    crashTest();
    
    contents = "hello";
    regularTest();
    crashTest();
    print("All OK!");
  }
  
  FileBasedLock makeLock() {
    FileBasedLock lock = new(file);
    if (contents != null)
      lock.contentsForLockFile(contents);
    lock.verbose = true;
    lock.timeout = timeout;
    ret lock;
  }
  
  void regularTest() {
    afterwards { cleanMeUp(); }
    lock1 = makeLock();
    lock2 = makeLock();
    assertTrueVerbose("Locking lock1", lock1.tryToLock());
    
    // Check lock file contents
    assertEquals(unnull(contents), loadTextFile(file));
    
    assertFalseVerbose("Locking lock2", lock2.tryToLock());
    print("Waiting for timeout (" + (timeout+1) + "s)");
    sleepSeconds(timeout+1);
    assertFalseVerbose("Locking lock2", lock2.tryToLock());
    close(lock1);
    assertTrueVerbose("Locking lock2", lock2.tryToLock());
    close(lock2);
    print("\nRegular test OK!\n");
  }
  
  void crashTest {
    afterwards { cleanMeUp(); }
    lock1 = makeLock();
    lock2 = makeLock();
    assertTrueVerbose("Locking lock1", lock1.tryToLock());
    assertFalseVerbose("Locking lock2", lock2.tryToLock());
    lock1._simulateCrash();
    print("Waiting for timeout (" + (timeout+1) + "s)");
    sleepSeconds(timeout+1);
    assertTrueVerbose("Locking lock2", lock2.tryToLock());
    closeAll(lock1, lock2);
    print("\nCrash test OK!\n");
  }
  
  void cleanMeUp {
    closeAll(lock1, lock2);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1029789
Snippet name: Test FileBasedLock (OK)
Eternal ID of this version: #1029789/9
Text MD5: aabc950feb2d6bba661bd35bf85f5933
Transpilation MD5: 49bb03c615fea0460f16a1b574df689e
Author: stefan
Category: javax / io
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-30 15:19:24
Source code size: 1877 bytes / 69 lines
Pitched / IR pitched: No / No
Views / Downloads: 161 / 372
Version history: 8 change(s)
Referenced in: [show references]