|
1 | 1 | import { TableLockManager } from '../table-lock-manager';
|
2 | 2 |
|
3 |
| -describe('Table Lock Manager', () => { |
| 3 | +const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 4 | + |
| 5 | +describe('TableLockManager', () => { |
4 | 6 | let tableLockManager: TableLockManager;
|
| 7 | + const tableName = 'testTable'; |
5 | 8 |
|
6 | 9 | beforeEach(() => {
|
7 | 10 | tableLockManager = new TableLockManager();
|
8 | 11 | });
|
9 | 12 |
|
10 |
| - it('should lock the table and release it', async () => { |
11 |
| - const tableName = 'exampleTable'; |
| 13 | + describe('write locks', () => { |
| 14 | + it('should acquire and release a write lock', async () => { |
| 15 | + await tableLockManager.lockTables([tableName], 'write'); |
| 16 | + expect(tableLockManager.isTableLocked(tableName)).toBe(true); |
| 17 | + |
| 18 | + tableLockManager.unlockTables([tableName], 'write'); |
| 19 | + expect(tableLockManager.isTableLocked(tableName)).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + it('a second writer should wait for the first writer to release the lock', async () => { |
| 23 | + let writer1Finished = false; |
| 24 | + const writer1 = async () => { |
| 25 | + await tableLockManager.lockTables([tableName], 'write'); |
| 26 | + |
| 27 | + await delay(50); // Simulate work |
| 28 | + |
| 29 | + expect(tableLockManager.isTableLocked(tableName)).toBe(true); |
| 30 | + tableLockManager.unlockTables([tableName], 'write'); |
| 31 | + writer1Finished = true; |
| 32 | + }; |
12 | 33 |
|
13 |
| - // Request the lock for the table and then release it |
14 |
| - await tableLockManager.lockTables([tableName]); |
| 34 | + let writer2Finished = false; |
| 35 | + const writer2 = async () => { |
| 36 | + await tableLockManager.lockTables([tableName], 'write'); |
15 | 37 |
|
16 |
| - expect(tableLockManager.isTableLocked(tableName)).toBe(true); |
| 38 | + // When writer2 gets the lock, writer1 should be finished. |
| 39 | + expect(writer1Finished).toBe(true); |
17 | 40 |
|
18 |
| - await tableLockManager.unlockTables([tableName]); |
| 41 | + expect(tableLockManager.isTableLocked(tableName)).toBe(true); |
| 42 | + tableLockManager.unlockTables([tableName], 'write'); |
| 43 | + writer2Finished = true; |
| 44 | + }; |
19 | 45 |
|
20 |
| - expect(tableLockManager.isTableLocked(tableName)).toBe(false); |
| 46 | + await Promise.all([writer1(), writer2()]); |
21 | 47 |
|
22 |
| - // Again request the lock for the table |
23 |
| - await tableLockManager.lockTables([tableName]); |
| 48 | + expect(writer1Finished).toBe(true); |
| 49 | + expect(writer2Finished).toBe(true); |
24 | 50 |
|
25 |
| - await tableLockManager.unlockTables([tableName]); |
| 51 | + expect(tableLockManager.isTableLocked(tableName)).toBe(false); |
| 52 | + }); |
26 | 53 | });
|
27 | 54 |
|
28 |
| - it('two consumers requesting lock for the same table', async () => { |
29 |
| - const tableName = 'exampleTable'; |
| 55 | + describe('read locks', () => { |
| 56 | + it('should allow multiple readers to acquire a lock simultaneously', async () => { |
| 57 | + const readerPromises = [ |
| 58 | + tableLockManager.lockTables([tableName], 'read'), |
| 59 | + tableLockManager.lockTables([tableName], 'read'), |
| 60 | + tableLockManager.lockTables([tableName], 'read'), |
| 61 | + ]; |
| 62 | + |
| 63 | + await Promise.all(readerPromises); |
| 64 | + expect(tableLockManager.isTableLocked(tableName)).toBe(true); |
| 65 | + |
| 66 | + tableLockManager.unlockTables([tableName], 'read'); |
| 67 | + tableLockManager.unlockTables([tableName], 'read'); |
| 68 | + tableLockManager.unlockTables([tableName], 'read'); |
| 69 | + expect(tableLockManager.isTableLocked(tableName)).toBe(false); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('mixed read/write locks', () => { |
| 74 | + it('should not allow a writer if readers have the lock', async () => { |
| 75 | + await tableLockManager.lockTables([tableName], 'read'); |
| 76 | + |
| 77 | + let writerAcquiredLock = false; |
| 78 | + const writerPromise = tableLockManager |
| 79 | + .lockTables([tableName], 'write') |
| 80 | + .then(() => { |
| 81 | + writerAcquiredLock = true; |
| 82 | + }); |
30 | 83 |
|
31 |
| - // Set up promises for the two consumers |
32 |
| - const consumer1Promise = tableLockManager.lockTables([tableName]); |
33 |
| - const consumer2Promise = tableLockManager.lockTables([tableName]); |
| 84 | + await delay(10); // Give writer time to wait |
| 85 | + expect(writerAcquiredLock).toBe(false); |
| 86 | + expect(tableLockManager.isTableLocked(tableName)).toBe(true); |
34 | 87 |
|
35 |
| - // Wait for the first consumer to get the lock |
36 |
| - await expect(consumer1Promise).resolves.toBeUndefined(); |
| 88 | + // Reader releases the lock |
| 89 | + tableLockManager.unlockTables([tableName], 'read'); |
37 | 90 |
|
38 |
| - expect(tableLockManager.isTableLocked(tableName)).toBe(true); |
| 91 | + await writerPromise; |
| 92 | + expect(writerAcquiredLock).toBe(true); |
| 93 | + expect(tableLockManager.isTableLocked(tableName)).toBe(true); |
39 | 94 |
|
40 |
| - const timeout1 = new Promise((resolve) => { |
41 |
| - setTimeout(resolve, 1000, 'TIMEOUT'); |
| 95 | + tableLockManager.unlockTables([tableName], 'write'); |
| 96 | + expect(tableLockManager.isTableLocked(tableName)).toBe(false); |
42 | 97 | });
|
43 | 98 |
|
44 |
| - // Promise.race will wait for either the promises be resolved |
45 |
| - // consumer2 will not be able to get the lock as it is already locked by consumer1 |
46 |
| - await expect(Promise.race([consumer2Promise, timeout1])).resolves.toBe( |
47 |
| - 'TIMEOUT' |
48 |
| - ); |
| 99 | + it('should not allow a reader if a writer has the lock', async () => { |
| 100 | + await tableLockManager.lockTables([tableName], 'write'); |
49 | 101 |
|
50 |
| - // Release the lock for the first consumer |
51 |
| - await tableLockManager.unlockTables([tableName]); |
| 102 | + let readerAcquiredLock = false; |
| 103 | + const readerPromise = tableLockManager |
| 104 | + .lockTables([tableName], 'read') |
| 105 | + .then(() => { |
| 106 | + readerAcquiredLock = true; |
| 107 | + }); |
52 | 108 |
|
53 |
| - // Check if the table is still locked as the consumer2 will get the lock |
54 |
| - expect(tableLockManager.isTableLocked(tableName)).toBe(true); |
| 109 | + await delay(10); |
| 110 | + expect(readerAcquiredLock).toBe(false); |
| 111 | + expect(tableLockManager.isTableLocked(tableName)).toBe(true); |
55 | 112 |
|
56 |
| - const timeout2 = new Promise((resolve) => { |
57 |
| - setTimeout(resolve, 1000, 'TIMEOUT'); |
| 113 | + // Writer releases lock |
| 114 | + tableLockManager.unlockTables([tableName], 'write'); |
| 115 | + |
| 116 | + await readerPromise; |
| 117 | + expect(readerAcquiredLock).toBe(true); |
| 118 | + |
| 119 | + tableLockManager.unlockTables([tableName], 'read'); |
| 120 | + expect(tableLockManager.isTableLocked(tableName)).toBe(false); |
58 | 121 | });
|
59 | 122 |
|
60 |
| - // This time the consumer2 will get the lock |
61 |
| - await expect( |
62 |
| - Promise.race([consumer2Promise, timeout2]) |
63 |
| - ).resolves.toBeUndefined(); |
| 123 | + it('should prioritize waiting readers over a new writer', async () => { |
| 124 | + // Writer1 acquires the lock |
| 125 | + await tableLockManager.lockTables([tableName], 'write'); |
| 126 | + |
| 127 | + // Readers start waiting |
| 128 | + const readerPromises = [ |
| 129 | + tableLockManager.lockTables([tableName], 'read'), |
| 130 | + tableLockManager.lockTables([tableName], 'read'), |
| 131 | + ]; |
| 132 | + |
| 133 | + // Writer2 starts waiting |
| 134 | + const writer2Promise = tableLockManager.lockTables([tableName], 'write'); |
| 135 | + |
| 136 | + // Release writer1's lock |
| 137 | + tableLockManager.unlockTables([tableName], 'write'); |
64 | 138 |
|
65 |
| - // Release the lock |
66 |
| - await tableLockManager.unlockTables([tableName]); |
| 139 | + // The waiting readers should get the lock next |
| 140 | + await Promise.all(readerPromises); |
| 141 | + expect(tableLockManager.isTableLocked(tableName)).toBe(true); |
67 | 142 |
|
68 |
| - expect(tableLockManager.isTableLocked(tableName)).toBe(false); |
| 143 | + let writer2AcquiredLock = false; |
| 144 | + writer2Promise.then(() => { |
| 145 | + writer2AcquiredLock = true; |
| 146 | + }); |
| 147 | + |
| 148 | + await delay(10); |
| 149 | + // Writer2 should still be waiting |
| 150 | + expect(writer2AcquiredLock).toBe(false); |
| 151 | + |
| 152 | + // Readers release locks |
| 153 | + tableLockManager.unlockTables([tableName], 'read'); |
| 154 | + tableLockManager.unlockTables([tableName], 'read'); |
| 155 | + |
| 156 | + // Now writer2 should get the lock |
| 157 | + await writer2Promise; |
| 158 | + expect(writer2AcquiredLock).toBe(true); |
| 159 | + expect(tableLockManager.isTableLocked(tableName)).toBe(true); |
| 160 | + |
| 161 | + tableLockManager.unlockTables([tableName], 'write'); |
| 162 | + expect(tableLockManager.isTableLocked(tableName)).toBe(false); |
| 163 | + }); |
69 | 164 | });
|
70 | 165 | });
|
0 commit comments