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).

1  
!7
2  
3  
cprint TestFileBasedLock {
4  
  switchable File file;
5  
  switchable double timeout = 10;
6  
  transient FileBasedLock lock1, lock2;
7  
  transient S contents; // contents for lock file
8  
  
9  
  start-thread {
10  
    if (file == null)
11  
      setField(file := javaxCachesDir("Lock Test/lockfile-" + aGlobalID()));
12  
    print("Using file: " + file);
13  
    
14  
    contents = null;
15  
    regularTest();
16  
    crashTest();
17  
    
18  
    contents = "hello";
19  
    regularTest();
20  
    crashTest();
21  
    print("All OK!");
22  
  }
23  
  
24  
  FileBasedLock makeLock() {
25  
    FileBasedLock lock = new(file);
26  
    if (contents != null)
27  
      lock.contentsForLockFile(contents);
28  
    lock.verbose = true;
29  
    lock.timeout = timeout;
30  
    ret lock;
31  
  }
32  
  
33  
  void regularTest() {
34  
    afterwards { cleanMeUp(); }
35  
    lock1 = makeLock();
36  
    lock2 = makeLock();
37  
    assertTrueVerbose("Locking lock1", lock1.tryToLock());
38  
    
39  
    // Check lock file contents
40  
    assertEquals(unnull(contents), loadTextFile(file));
41  
    
42  
    assertFalseVerbose("Locking lock2", lock2.tryToLock());
43  
    print("Waiting for timeout (" + (timeout+1) + "s)");
44  
    sleepSeconds(timeout+1);
45  
    assertFalseVerbose("Locking lock2", lock2.tryToLock());
46  
    close(lock1);
47  
    assertTrueVerbose("Locking lock2", lock2.tryToLock());
48  
    close(lock2);
49  
    print("\nRegular test OK!\n");
50  
  }
51  
  
52  
  void crashTest {
53  
    afterwards { cleanMeUp(); }
54  
    lock1 = makeLock();
55  
    lock2 = makeLock();
56  
    assertTrueVerbose("Locking lock1", lock1.tryToLock());
57  
    assertFalseVerbose("Locking lock2", lock2.tryToLock());
58  
    lock1._simulateCrash();
59  
    print("Waiting for timeout (" + (timeout+1) + "s)");
60  
    sleepSeconds(timeout+1);
61  
    assertTrueVerbose("Locking lock2", lock2.tryToLock());
62  
    closeAll(lock1, lock2);
63  
    print("\nCrash test OK!\n");
64  
  }
65  
  
66  
  void cleanMeUp {
67  
    closeAll(lock1, lock2);
68  
  }
69  
}

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: 164 / 376
Version history: 8 change(s)
Referenced in: [show references]