/[sudobot]/trunk/tests/framework/concurrent/Mutex.test.ts
ViewVC logotype

Annotation of /trunk/tests/framework/concurrent/Mutex.test.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 575 - (hide annotations)
Mon Jul 29 17:59:26 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 2614 byte(s)
chore: add trunk
1 rakinar2 575 import Mutex from "@framework/concurrent/Mutex";
2     import { setTimeout } from "timers/promises";
3     import { describe, expect, it } from "vitest";
4    
5     describe("Mutex", () => {
6     describe("basic usage", () => {
7     it("should lock and unlock", async () => {
8     // Arrange
9     const mutex = new Mutex();
10    
11     // Act
12     await mutex.lock();
13     mutex.unlock();
14    
15     // Assert
16     expect(mutex.isUnlocked()).toBe(true);
17     });
18    
19     it("should lock and unlock with extraneous releases", async () => {
20     // Arrange
21     const mutex = new Mutex({ ignoreExtraneousUnlocks: true });
22    
23     // Act
24     await mutex.lock();
25     mutex.unlock();
26    
27     // Assert
28     expect(mutex.isUnlocked()).toBe(true);
29     });
30    
31     it("should lock and unlock with extraneous releases and ignore them", async () => {
32     // Arrange
33     const mutex = new Mutex({ ignoreExtraneousUnlocks: true });
34    
35     // Act
36     await mutex.lock();
37     mutex.unlock();
38     mutex.unlock();
39    
40     // Assert
41     expect(mutex.isUnlocked()).toBe(true);
42     });
43    
44     it("should lock and unlock with extraneous releases and throw an error if opted-in", async () => {
45     // Arrange
46     const mutex = new Mutex({
47     ignoreExtraneousUnlocks: false
48     });
49    
50     // Act
51     await mutex.lock();
52     mutex.unlock();
53    
54     // Assert
55     expect(() => mutex.unlock()).toThrowError("This mutex is not locked yet.");
56     });
57     });
58    
59     describe("race condition checks", () => {
60     it(
61     "should lock and unlock with multiple locks",
62     {
63     repeats: 10
64     },
65     async () => {
66     // Arrange
67     const mutex = new Mutex();
68     const output: number[] = [];
69     const fn1 = async () => {
70     await mutex.lock();
71     await setTimeout(700 * Math.random());
72     output.push(1);
73     mutex.unlock();
74     };
75    
76     const fn2 = async () => {
77     await mutex.lock();
78     await setTimeout(240 * Math.random());
79     output.push(2);
80     mutex.unlock();
81     };
82    
83     // Act
84     await Promise.all([fn1(), fn2()]);
85    
86     // Assert
87     expect(output).toEqual([1, 2]);
88     }
89     );
90     });
91     });

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26