-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
255 changed files
with
9,182 additions
and
3,204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Run a block with a timeout | ||
* | ||
* We can't use the jest timeout feature: | ||
* | ||
* - `jest.concurrent()` does not do any concurrency management. It starts all | ||
* tests at the same time. | ||
* - Our tests use locking to make sure only one test is running at a time per | ||
* region. | ||
* | ||
* The wait time for the locks is included in the jest test timeout. We therefore | ||
* need to set it unreasonably high (as long as the last test may need to wait | ||
* if all tests are executed using only 1 region, and they effectively execute | ||
* sequentially), which makes it not useful to detect stuck tests. | ||
* | ||
* The `withTimeout()` modifier makes it possible to measure only a specific | ||
* block of code. In our case: the effective test code, excluding the wait time. | ||
*/ | ||
export function withTimeout<A>(seconds: number, block: (x: A) => Promise<void>) { | ||
return (x: A) => { | ||
const timeOut = new Promise<void>((_ok, ko) => { | ||
const timerHandle = setTimeout( | ||
() => ko(new Error(`Timeout: test took more than ${seconds}s to complete`)), | ||
seconds * 1000); | ||
timerHandle.unref(); | ||
}); | ||
|
||
return Promise.race([ | ||
block(x), | ||
timeOut, | ||
]); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { XpMutexPool } from '../lib/xpmutex'; | ||
|
||
const POOL = XpMutexPool.fromName('test-pool'); | ||
|
||
test('acquire waits', async () => { | ||
const mux = POOL.mutex('testA'); | ||
let secondLockAcquired = false; | ||
|
||
// Current "process" acquires lock | ||
const lock = await mux.acquire(); | ||
|
||
// Start a second "process" that tries to acquire the lock | ||
const secondProcess = (async () => { | ||
const secondLock = await mux.acquire(); | ||
try { | ||
secondLockAcquired = true; | ||
} finally { | ||
await secondLock.release(); | ||
} | ||
})(); | ||
|
||
// Once we release the lock the second process is free to take it | ||
expect(secondLockAcquired).toBe(false); | ||
await lock.release(); | ||
|
||
// We expect the variable to become true | ||
await waitFor(() => secondLockAcquired); | ||
expect(secondLockAcquired).toBe(true); | ||
|
||
await secondProcess; | ||
}); | ||
|
||
|
||
/** | ||
* Poll for some condition every 10ms | ||
*/ | ||
function waitFor(pred: () => boolean): Promise<void> { | ||
return new Promise((ok) => { | ||
const timerHandle = setInterval(() => { | ||
if (pred()) { | ||
clearInterval(timerHandle); | ||
ok(); | ||
} | ||
}, 5); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.