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

Discard earliest heartbeat once there are 30 heartbeats #8724

Merged
merged 6 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use max heartbeat const in tests
  • Loading branch information
dlarocque committed Jan 23, 2025
commit 2ae1a6647f223c85b4f82424887893ba0ed2b373
14 changes: 7 additions & 7 deletions packages/app/src/heartbeatService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
countBytes,
HeartbeatServiceImpl,
extractHeartbeatsForHeader,
getEarliestHeartbeatIdx
getEarliestHeartbeatIdx,
MAX_NUM_STORED_HEARTBEATS
} from './heartbeatService';
import {
Component,
Expand Down Expand Up @@ -277,30 +278,30 @@ describe('HeartbeatServiceImpl', () => {
// Trigger heartbeats until we reach the limit
const numHeartbeats =
heartbeatService._heartbeatsCache?.heartbeats.length!;
for (let i = numHeartbeats; i <= 30; i++) {
for (let i = numHeartbeats; i <= MAX_NUM_STORED_HEARTBEATS; i++) {
await heartbeatService.triggerHeartbeat();
clock.tick(24 * 60 * 60 * 1000);
}

expect(heartbeatService._heartbeatsCache?.heartbeats.length).to.equal(30);
expect(heartbeatService._heartbeatsCache?.heartbeats.length).to.equal(MAX_NUM_STORED_HEARTBEATS);
const earliestHeartbeatDate = getEarliestHeartbeatIdx(
heartbeatService._heartbeatsCache?.heartbeats!
);
const earliestHeartbeat =
heartbeatService._heartbeatsCache?.heartbeats[earliestHeartbeatDate]!;
await heartbeatService.triggerHeartbeat();
expect(heartbeatService._heartbeatsCache?.heartbeats.length).to.equal(30);
expect(heartbeatService._heartbeatsCache?.heartbeats.length).to.equal(MAX_NUM_STORED_HEARTBEATS);
expect(
heartbeatService._heartbeatsCache?.heartbeats.indexOf(earliestHeartbeat)
).to.equal(-1);
});
it('triggerHeartbeat() never exceeds 30 heartbeats', async () => {
it('triggerHeartbeat() never exceeds MAX_NUM_STORED_HEARTBEATS heartbeats', async () => {
dlarocque marked this conversation as resolved.
Show resolved Hide resolved
for (let i = 0; i <= 50; i++) {
await heartbeatService.triggerHeartbeat();
clock.tick(24 * 60 * 60 * 1000);
expect(
heartbeatService._heartbeatsCache?.heartbeats.length
).to.be.lessThanOrEqual(30);
).to.be.lessThanOrEqual(MAX_NUM_STORED_HEARTBEATS);
}
});
});
Expand All @@ -310,7 +311,6 @@ describe('HeartbeatServiceImpl', () => {
let writeStub: SinonStub;
const userAgentString = USER_AGENT_STRING_1;
const mockIndexedDBHeartbeats = [
// Chosen so one will exceed 30 day limit and one will not.
{
agent: 'old-user-agent',
date: '1969-12-01'
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/heartbeatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
import { logger } from './logger';

const MAX_HEADER_BYTES = 1024;
const MAX_NUM_STORED_HEARTBEATS = 30;
export const MAX_NUM_STORED_HEARTBEATS = 30;

export class HeartbeatServiceImpl implements HeartbeatService {
/**
Expand Down
Loading