feat: add tests for node/heart.ts (#5122)
* refactor(heart): extract logic into heartbeatTimer fn To make it easier to test, I extract heartbeatTimer into it's own function. * feat(testing): add tests for heart.ts * fixup * fixup!: remove unneeded heart call * Update src/node/heart.ts Co-authored-by: Asher <ash@coder.com> * fixup!: use mockResolvedValue everywhere * fixup!: add stat test for timestamp check Co-authored-by: Asher <ash@coder.com>
This commit is contained in:
@ -33,17 +33,7 @@ export class Heart {
|
||||
if (typeof this.heartbeatTimer !== "undefined") {
|
||||
clearTimeout(this.heartbeatTimer)
|
||||
}
|
||||
this.heartbeatTimer = setTimeout(() => {
|
||||
this.isActive()
|
||||
.then((active) => {
|
||||
if (active) {
|
||||
this.beat()
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.warn(error.message)
|
||||
})
|
||||
}, this.heartbeatInterval)
|
||||
this.heartbeatTimer = setTimeout(() => heartbeatTimer(this.isActive, this.beat), this.heartbeatInterval)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,3 +45,20 @@ export class Heart {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for the heartbeatTimer.
|
||||
*
|
||||
* If heartbeat is active, call beat. Otherwise do nothing.
|
||||
*
|
||||
* Extracted to make it easier to test.
|
||||
*/
|
||||
export async function heartbeatTimer(isActive: Heart["isActive"], beat: Heart["beat"]) {
|
||||
try {
|
||||
if (await isActive()) {
|
||||
beat()
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
logger.warn((error as Error).message)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user