Skip to content

Fix concurrency issues, remove wait band-aids from tests #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "demux-postgres",
"version": "0.0.6",
"version": "0.0.7",
"description": "Demux-js Action Handler implementation for Postgres databases",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
10 changes: 0 additions & 10 deletions src/MassiveActionHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ const dbName = "demuxmassivetest"
const dbUser = "docker"
const dbPass = "docker"

export function wait(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}

jest.setTimeout(30000)

Expand Down Expand Up @@ -62,7 +57,6 @@ describe("TestMassiveActionHandler", () => {
it("populates database correctly", async () => {
const [block1, isRollback] = await actionReader.nextBlock()
await actionHandler.handleBlock(block1, isRollback, actionReader.isFirstBlock)
await wait(500)

const groceries = await db.todo.findOne({ id: 1 })
expect(groceries).toEqual({
Expand All @@ -77,7 +71,6 @@ describe("TestMassiveActionHandler", () => {

const [block2, isNotRollback] = await actionReader.nextBlock()
await actionHandler.handleBlock(block2, isNotRollback, actionReader.isFirstBlock)
await wait(500)

const cookies = await db.task.findOne({ name: "cookies" })
expect(cookies).toEqual({
Expand All @@ -97,7 +90,6 @@ describe("TestMassiveActionHandler", () => {

const [block3, alsoNotRollback] = await actionReader.nextBlock()
await actionHandler.handleBlock(block3, alsoNotRollback, actionReader.isFirstBlock)
await wait(500)

const milk = await db.task.findOne({ name: "milk" })
const dippedCookies = await db.task.findOne({ name: "cookies" })
Expand Down Expand Up @@ -127,12 +119,10 @@ describe("TestMassiveActionHandler", () => {
const [block1, isRollback1] = await actionReader.nextBlock()
await actionHandler.handleBlock(block1, isRollback1, actionReader.isFirstBlock)
expect(actionReader.isFirstBlock).toBe(true)
await wait(500)

const [block2, isRollback2] = await actionReader.nextBlock()
await actionHandler.handleBlock(block2, isRollback2, actionReader.isFirstBlock)
expect(actionReader.isFirstBlock).toBe(false)
await wait(500)

actionHandler.reset()
const [needToSeek, seekTo] = await actionHandler.handleBlock(block1, isRollback1, true)
Expand Down
36 changes: 16 additions & 20 deletions src/MassiveActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,22 @@ export class MassiveActionHandler extends AbstractActionHandler {
}

protected async handleWithState(handle: (state: any, context?: any) => void): Promise<void> {
await new Promise((resolve, reject) => {
this.massiveInstance.withTransaction(async (tx: any) => {
let db
if (this.dbSchema === "public") {
db = tx
} else {
db = tx[this.dbSchema]
}
try {
await handle(db)
resolve(db)
} catch (err) {
console.error(err)
reject()
}
}, {
mode: new this.massiveInstance.pgp.txMode.TransactionMode({
tiLevel: this.massiveInstance.pgp.txMode.isolationLevel.serializable,
}),
})
await this.massiveInstance.withTransaction(async (tx: any) => {
let db
if (this.dbSchema === "public") {
db = tx
} else {
db = tx[this.dbSchema]
}
try {
await handle(db)
} catch (err) {
throw err // Throw error to trigger ROLLBACK
}
}, {
mode: new this.massiveInstance.pgp.txMode.TransactionMode({
tiLevel: this.massiveInstance.pgp.txMode.isolationLevel.serializable,
}),
})
}

Expand Down