Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Oct 1, 2023
1 parent 77b55ec commit 380f2cc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
env:
ELECTRON_CACHE_DIR: ${{ github.workspace }}
FAILURE_LOG: true
CI: true

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"fix:prettier": "prettier --write \"{src,test}/**/*.ts\"",
"fix:eslint": "eslint . --format stylish --fix",
"pretest": "yarn build",
"test": "cross-env TS_NODE_PROJECT=tsconfig.json xvfb-maybe electron-mocha --require ts-node/register/transpile-only --timeout 120000 ./test/unit/**/*.ts",
"test": "cross-env TS_NODE_PROJECT=tsconfig.json xvfb-maybe electron-mocha --require ts-node/register/transpile-only --timeout 12000 ./test/unit/**/*.ts",
"pree2e": "rimraf test/e2e/dist/**/node_modules/@sentry/** test/e2e/dist/**/yarn.lock test/e2e/dist/**/package-lock.json && node scripts/clean-cache.js && yarn build && npm pack",
"e2e": "cross-env TS_NODE_PROJECT=tsconfig.json xvfb-maybe mocha --require ts-node/register/transpile-only --retries 3 ./test/e2e/*.ts"
},
Expand Down
29 changes: 18 additions & 11 deletions src/main/integrations/sentry-minidump/minidump-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ import { getCrashesDirectory, usesCrashpad } from '../../electron-normalize';
import { readDirAsync, readFileAsync, statAsync, unlinkAsync } from '../../fs';

/** Maximum number of days to keep a minidump before deleting it. */
const MAX_AGE = 30;
const MAX_AGE_DAYS = 30;
/** Minimum number of milliseconds a minidump should not be modified for before we assume writing is complete */
const MIN_NOT_MODIFIED = 1_000;
const MAX_RETRY_TIME = 5_000;
const TIME_BETWEEN_RETRIES = 200;
const MAX_RETRIES = MAX_RETRY_TIME / TIME_BETWEEN_RETRIES;
const NOT_MODIFIED_MS = 1_000;
const MAX_RETRY_MS = 5_000;
const RETRY_DELAY_MS = 500;
const MAX_RETRIES = MAX_RETRY_MS / RETRY_DELAY_MS;

const MINIDUMP_HEADER = 'MDMP';

function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

/**
* A function that loads minidumps
* @param deleteAll Whether to just delete all minidumps
* @param callback A callback to call with the attachment ready to send
*/
export type MinidumpLoader = (deleteAll: boolean, callback: (attachment: Attachment) => void) => Promise<void>;

/**
Expand Down Expand Up @@ -48,17 +53,17 @@ export function createMinidumpLoader(

let stats = await statAsync(path);

const thirtyDaysAgo = new Date().getTime() - MAX_AGE * 24 * 3_600 * 1_000;
const thirtyDaysAgo = new Date().getTime() - MAX_AGE_DAYS * 24 * 3_600 * 1_000;

if (stats.birthtimeMs < thirtyDaysAgo) {
logger.log(`Ignoring minidump as it is over ${MAX_AGE} days old`);
logger.log(`Ignoring minidump as it is over ${MAX_AGE_DAYS} days old`);
continue;
}

let retries = 0;

while (retries <= MAX_RETRIES) {
const twoSecondsAgo = new Date().getTime() - MIN_NOT_MODIFIED;
const twoSecondsAgo = new Date().getTime() - NOT_MODIFIED_MS;

if (stats.mtimeMs < twoSecondsAgo) {
const file = await readFileAsync(path);
Expand All @@ -80,9 +85,10 @@ export function createMinidumpLoader(
break;
}

logger.log(`Waiting. Minidump has been modified in the last ${MIN_NOT_MODIFIED} milliseconds.`);
logger.log(`Waiting. Minidump has been modified in the last ${NOT_MODIFIED_MS} milliseconds.`);
retries += 1;
await delay(TIME_BETWEEN_RETRIES);
await delay(RETRY_DELAY_MS);
// update the stats
stats = await statAsync(path);
}

Expand All @@ -92,10 +98,11 @@ export function createMinidumpLoader(
} catch (e) {
logger.error('Failed to load minidump', e);
} finally {
// We always attempt to delete the minidump
try {
await unlinkAsync(path);
} catch (e) {
logger.warn('Could not delete', path);
logger.warn('Could not delete minidump', path);
}
}
}
Expand Down
19 changes: 13 additions & 6 deletions test/unit/minidump-loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { uuid4 } from '@sentry/utils';
import { expect } from 'chai';
import { existsSync, utimesSync, writeFileSync } from 'fs';
import { closeSync, existsSync, openSync, utimesSync, writeFileSync, writeSync } from 'fs';
import { join } from 'path';
import * as tmp from 'tmp';

Expand All @@ -10,7 +10,7 @@ function dumpFileName(): string {
return `${uuid4()}.dmp`;
}

const VALID_LOOKING_MINIDUMP = Buffer.from(`MDMP${'x'.repeat(12_000)}`);
const VALID_LOOKING_MINIDUMP = Buffer.from(`MDMP${'X'.repeat(12_000)}`);
const LOOKS_NOTHING_LIKE_A_MINIDUMP = Buffer.from('X'.repeat(12_000));
const MINIDUMP_HEADER_BUT_TOO_SMALL = Buffer.from('MDMPdflahfalfhalkfnaklsfnalfkn');

Expand Down Expand Up @@ -69,6 +69,12 @@ describe('createMinidumpLoader', () => {
});

it("doesn't send minidumps that are over 30 days old", (done) => {
// Updating the file times does not appear to work in GitHub Actions on Windows and Linux
if (process.env.CI) {
done();
return;
}

const dumpPath = join(tempDir.name, dumpFileName());
writeFileSync(dumpPath, VALID_LOOKING_MINIDUMP);
const now = new Date().getTime() / 1000;
Expand Down Expand Up @@ -109,19 +115,20 @@ describe('createMinidumpLoader', () => {

it('waits for minidump to stop being modified', (done) => {
const dumpPath = join(tempDir.name, dumpFileName());
writeFileSync(dumpPath, VALID_LOOKING_MINIDUMP);
const file = openSync(dumpPath, 'w');
writeSync(file, VALID_LOOKING_MINIDUMP);

let count = 0;
// Write the file every 500ms
const timer = setInterval(() => {
count += 500;
writeFileSync(dumpPath, VALID_LOOKING_MINIDUMP);
writeSync(file, 'X');
}, 500);

// Stop writing after 3 seconds
setTimeout(() => {
clearInterval(timer);
}, 3_200);
closeSync(file);
}, 4_200);

const loader = createMinidumpLoader(() => Promise.resolve([dumpPath]));

Expand Down

0 comments on commit 380f2cc

Please sign in to comment.