Skip to content
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

Clear current timeout on heartbeat #43

Merged
merged 1 commit into from
Dec 27, 2022
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
1 change: 0 additions & 1 deletion .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ module.exports = {
require: ["ts-node/register/transpile-only", "test/index.ts"],
extension: ["ts"],
recursive: true,
exit: true,
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@
"dependencies": {
"winston": "^3.7.2"
}
}
}
8 changes: 6 additions & 2 deletions src/heartbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ export class Heartbeat {
private lastMessageReceived = new Date()
private lastMessageSent = new Date()
private idleCounter: number = 0
private timeout: NodeJS.Timeout | null = null

constructor(private readonly connection: HeartbeatConnection, private readonly logger: Logger) {}

start(secondsInterval: number) {
if (secondsInterval <= 0) return
this.interval = secondsInterval * 1000
setTimeout(() => this.heartbeat(), this.interval)
this.timeout = setTimeout(() => this.heartbeat(), this.interval)
}

stop() {
// TODO -> Wait the cycle of heartbeat...
if (this.timeout) {
clearTimeout(this.timeout)
}
this.interval = 0
}

Expand All @@ -48,7 +52,7 @@ export class Heartbeat {
}
await this.idleDetection()
if (this.interval <= 0) return
setTimeout(() => this.heartbeat(), this.interval)
this.timeout = setTimeout(() => this.heartbeat(), this.interval)
}

private sendHeartbeat() {
Expand Down
8 changes: 7 additions & 1 deletion test/unit/create_stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("Stream", () => {
}
let connection: Connection

before(async () => {
beforeEach(async () => {
connection = await connect({
hostname: "localhost",
port: 5552,
Expand All @@ -33,6 +33,12 @@ describe("Stream", () => {
await rabbit.deleteQueue("%2F", streamName)
} catch (error) {}
})
afterEach(async () => {
try {
await connection.close()
} catch (error) {}
})

after(() => rabbit.closeAllConnections())

describe("Create", () => {
Expand Down
13 changes: 9 additions & 4 deletions test/unit/delete_stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe("Delete command", () => {
let connection: Connection
const queue_name = `queue_${(Math.random() * 10) | 0}`

before(async () => {
beforeEach(async () => {
connection = await connect({
hostname: "localhost",
port: 5552,
Expand All @@ -19,13 +19,18 @@ describe("Delete command", () => {
heartbeat: 0, // not user
})
})

after(() => rabbit.closeAllConnections())

afterEach(async () => {
await rabbit.deleteAllQueues()
})

afterEach(async () => {
try {
await connection.close()
} catch (error) {}
})

after(() => rabbit.closeAllConnections())

it("delete a nonexisting stream (raises error)", async () => {
await expectToThrowAsync(
() => connection?.deleteStream({ stream: "AAA" }),
Expand Down
13 changes: 10 additions & 3 deletions test/unit/heartbeat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ describe("heartbeat", () => {
it("sent heartbeat every seconds", async () => {
const connectionMock = new ConnectionMock()
const hb = new Heartbeat(connectionMock, createConsoleLog())

hb.start(1)

await eventually(async () => {
expect(connectionMock.getSendCount()).eq(4)
}, 6000)
await eventually(async () => expect(connectionMock.getSendCount()).eq(4), 6000)
hb.stop()
}).timeout(10000)

Expand All @@ -43,5 +42,13 @@ describe("heartbeat", () => {
expect(connectionMock.getSendCount()).lessThanOrEqual(1)
}).timeout(10000)

it("stop current timeout so we could exit immediately", () => {
const connectionMock = new ConnectionMock()
const hb = new Heartbeat(connectionMock, createConsoleLog())
hb.start(200)

hb.stop()
})

it("start two times same object raise exception")
})