-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core-transaction-pool): better locking (#3802)
- Loading branch information
Showing
12 changed files
with
295 additions
and
248 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Lock } from "../../../../packages/core-kernel/src/utils/lock"; | ||
|
||
describe("Lock", () => { | ||
it("should run exclusive executions in series", async () => { | ||
let resolve: () => void; | ||
const promise = new Promise((r) => (resolve = r)); | ||
|
||
let executions = 0; | ||
const fn = async () => { | ||
executions++; | ||
await promise; | ||
return executions; | ||
}; | ||
|
||
const lock = new Lock(); | ||
const promises = [lock.runExclusive(fn), lock.runExclusive(fn), lock.runExclusive(fn)]; | ||
resolve(); | ||
|
||
expect(await Promise.all(promises)).toEqual([1, 2, 3]); | ||
}); | ||
|
||
it("should run non-exclusive executions in parallel", async () => { | ||
let resolve: () => void; | ||
const promise = new Promise((r) => (resolve = r)); | ||
|
||
let executions = 0; | ||
const fn = async () => { | ||
executions++; | ||
await promise; | ||
return executions; | ||
}; | ||
|
||
const lock = new Lock(); | ||
const promises = [lock.runNonExclusive(fn), lock.runNonExclusive(fn), lock.runNonExclusive(fn)]; | ||
resolve(); | ||
|
||
expect(await Promise.all(promises)).toEqual([3, 3, 3]); | ||
}); | ||
|
||
it("should run exclusive execution after non-exclusive had finished", async () => { | ||
let resolve: () => void; | ||
const promise = new Promise((r) => (resolve = r)); | ||
|
||
let executions = 0; | ||
const fn = async () => { | ||
executions++; | ||
await promise; | ||
return executions; | ||
}; | ||
|
||
const lock = new Lock(); | ||
const promises = [lock.runNonExclusive(fn), lock.runNonExclusive(fn), lock.runExclusive(fn)]; | ||
resolve(); | ||
|
||
expect(await Promise.all(promises)).toEqual([2, 2, 3]); | ||
}); | ||
|
||
it("should run non-exclusive execution after exclusive had finished", async () => { | ||
let resolve: () => void; | ||
const promise = new Promise((r) => (resolve = r)); | ||
|
||
let executions = 0; | ||
const fn = async () => { | ||
executions++; | ||
await promise; | ||
return executions; | ||
}; | ||
|
||
const lock = new Lock(); | ||
const promises = [lock.runExclusive(fn), lock.runNonExclusive(fn), lock.runNonExclusive(fn)]; | ||
resolve(); | ||
|
||
expect(await Promise.all(promises)).toEqual([1, 3, 3]); | ||
}); | ||
}); |
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
2 changes: 1 addition & 1 deletion
2
packages/core-kernel/src/contracts/transaction-pool/sender-mempool.ts
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.