|
| 1 | +import { Mutex } from "./mutex"; |
| 2 | +import { timeFunction } from "../../support/time-function"; |
| 3 | +import { |
| 4 | + ConcurrencyExceedTimeoutException, |
| 5 | + ConcurrencyInterruptedException, |
| 6 | + ConcurrencyInvalidTimeoutException |
| 7 | +} from "../exceptions"; |
| 8 | + |
| 9 | +describe("Mutex", () => { |
| 10 | + describe("Input validation", () => { |
| 11 | + const mutex = new Mutex(); |
| 12 | + |
| 13 | + it("should throw a timeout exception when try-locking negative timeout", async () => { |
| 14 | + for (const timeout of [-1, -10, -100]) { |
| 15 | + await expect(() => mutex.tryLock(timeout)).rejects.toThrow( |
| 16 | + ConcurrencyInvalidTimeoutException |
| 17 | + ); |
| 18 | + } |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + describe("`lock` usage", () => { |
| 23 | + const delay = 50; |
| 24 | + const offset = 3; |
| 25 | + |
| 26 | + it("should work with a basic usage", async () => { |
| 27 | + // semaphore as a mutex |
| 28 | + const mutex = new Mutex(); |
| 29 | + |
| 30 | + expect(mutex.isLocked).toBeFalse(); |
| 31 | + expect(mutex.queueLength).toBe(0); |
| 32 | + |
| 33 | + const [elapsed] = await timeFunction(async () => { |
| 34 | + setTimeout(() => { |
| 35 | + expect(mutex.isLocked).toBeTrue(); |
| 36 | + expect(mutex.queueLength).toBe(1); |
| 37 | + mutex.unlock(); |
| 38 | + expect(mutex.isLocked).toBeFalse(); |
| 39 | + expect(mutex.queueLength).toBe(0); |
| 40 | + }, delay); |
| 41 | + |
| 42 | + // Will wait until the end of the `setTimeout` |
| 43 | + await mutex.lock(); |
| 44 | + }); |
| 45 | + |
| 46 | + expect(elapsed).toBeGreaterThanOrEqual(delay - offset); |
| 47 | + expect(elapsed).toBeLessThanOrEqual(delay + offset); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should work with many lock", async () => { |
| 51 | + const mutex = new Mutex(); |
| 52 | + |
| 53 | + const min = delay / 2; |
| 54 | + const med = delay; |
| 55 | + const max = delay * 2; |
| 56 | + |
| 57 | + setTimeout(() => { |
| 58 | + expect(mutex.isLocked).toBeTrue(); |
| 59 | + expect(mutex.queueLength).toBe(3); |
| 60 | + mutex.unlock(); |
| 61 | + expect(mutex.isLocked).toBeTrue(); |
| 62 | + expect(mutex.queueLength).toBe(2); |
| 63 | + }, min); |
| 64 | + |
| 65 | + setTimeout(() => { |
| 66 | + expect(mutex.isLocked).toBeTrue(); |
| 67 | + expect(mutex.queueLength).toBe(2); |
| 68 | + mutex.unlock(); |
| 69 | + expect(mutex.isLocked).toBeTrue(); |
| 70 | + expect(mutex.queueLength).toBe(1); |
| 71 | + }, med); |
| 72 | + |
| 73 | + setTimeout(() => { |
| 74 | + expect(mutex.isLocked).toBeTrue(); |
| 75 | + expect(mutex.queueLength).toBe(1); |
| 76 | + mutex.unlock(); |
| 77 | + expect(mutex.isLocked).toBeFalse(); |
| 78 | + expect(mutex.queueLength).toBe(0); |
| 79 | + }, max); |
| 80 | + |
| 81 | + const [elapsed, times] = await timeFunction(() => |
| 82 | + Promise.all([ |
| 83 | + timeFunction(() => mutex.lock()), |
| 84 | + timeFunction(() => mutex.lock()), |
| 85 | + timeFunction(() => mutex.lock()) |
| 86 | + ]) |
| 87 | + ); |
| 88 | + |
| 89 | + const [elapsed1, elapsed2, elapsed3] = times |
| 90 | + .map(([elapsed]) => elapsed) |
| 91 | + .sort((a, b) => a - b); |
| 92 | + |
| 93 | + expect(elapsed1).toBeGreaterThanOrEqual(min - offset); |
| 94 | + expect(elapsed1).toBeLessThanOrEqual(min + offset); |
| 95 | + expect(elapsed2).toBeGreaterThanOrEqual(med - offset); |
| 96 | + expect(elapsed2).toBeLessThanOrEqual(med + offset); |
| 97 | + expect(elapsed3).toBeGreaterThanOrEqual(max - offset); |
| 98 | + expect(elapsed3).toBeLessThanOrEqual(max + offset); |
| 99 | + |
| 100 | + // The global elapsed time is very close to the slowest timeout |
| 101 | + expect(elapsed).toBeGreaterThanOrEqual(max - offset); |
| 102 | + expect(elapsed).toBeLessThanOrEqual(max + offset); |
| 103 | + |
| 104 | + expect(mutex.isLocked).toBeFalse(); |
| 105 | + expect(mutex.queueLength).toBe(0); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe("`tryLock` usage", () => { |
| 110 | + const delay = 50; |
| 111 | + const offset = 3; |
| 112 | + |
| 113 | + it("should work with tryLock/unlock", async () => { |
| 114 | + const mutex = new Mutex(); |
| 115 | + |
| 116 | + const [elapsed] = await timeFunction(async () => { |
| 117 | + setTimeout(() => mutex.unlock(), delay); |
| 118 | + await mutex.tryLock(delay * 5); |
| 119 | + }); |
| 120 | + |
| 121 | + expect(elapsed).toBeGreaterThanOrEqual(delay - offset); |
| 122 | + expect(elapsed).toBeLessThanOrEqual(delay + offset); |
| 123 | + }); |
| 124 | + |
| 125 | + it("should thrown an error when the time exceeds", async () => { |
| 126 | + const mutex = new Mutex(); |
| 127 | + |
| 128 | + setTimeout(() => { |
| 129 | + expect(mutex.queueLength).toBe(1); |
| 130 | + expect(mutex.isLocked).toBeTrue(); |
| 131 | + }, delay / 2); |
| 132 | + |
| 133 | + await expect(() => mutex.tryLock(delay)).rejects.toThrow( |
| 134 | + ConcurrencyExceedTimeoutException |
| 135 | + ); |
| 136 | + |
| 137 | + // The queue is reset, since the `tryAcquire` failed |
| 138 | + expect(mutex.queueLength).toBe(0); |
| 139 | + expect(mutex.isLocked).toBeFalse(); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + it("should interrupt all", async () => { |
| 144 | + const delay = 100; |
| 145 | + const mutex = new Mutex(); |
| 146 | + const reason = "test"; |
| 147 | + |
| 148 | + setTimeout(() => { |
| 149 | + expect(mutex.isLocked).toBeTrue(); |
| 150 | + expect(mutex.queueLength).toBe(3); |
| 151 | + mutex.interrupt(reason); |
| 152 | + }, delay); |
| 153 | + |
| 154 | + const errors = await Promise.all([ |
| 155 | + mutex.lock().catch((err: unknown) => err), |
| 156 | + mutex.tryLock(delay * 2).catch((err: unknown) => err), |
| 157 | + mutex.lock().catch((err: unknown) => err) |
| 158 | + ]); |
| 159 | + |
| 160 | + for (const error of errors) { |
| 161 | + expect(error).toBeInstanceOf(ConcurrencyInterruptedException); |
| 162 | + expect((error as ConcurrencyInterruptedException).getReason()).toBe(reason); |
| 163 | + } |
| 164 | + |
| 165 | + expect(mutex.isLocked).toBeFalse(); |
| 166 | + expect(mutex.queueLength).toBe(0); |
| 167 | + }); |
| 168 | +}); |
0 commit comments