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

reorder teardown callback to after the error event, and setResults. #63

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 9 additions & 14 deletions src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ export default class Task extends EventTarget {
}
}

await this.bench.teardown(this, 'run');

if (!this.result?.error) {
if (this.result?.error) {
this.dispatchEvent(createBenchEvent('error', this));
this.bench.dispatchEvent(createBenchEvent('error', this));
} else {
samples.sort((a, b) => a - b);

const period = totalTime / this.runs;
Expand Down Expand Up @@ -165,18 +166,12 @@ export default class Task extends EventTarget {
});
}

// eslint-disable-next-line no-lone-blocks
{
if (this.result?.error) {
this.dispatchEvent(createBenchEvent('error', this));
this.bench.dispatchEvent(createBenchEvent('error', this));
}
await this.bench.teardown(this, 'run');

this.dispatchEvent(createBenchEvent('cycle', this));
this.bench.dispatchEvent(createBenchEvent('cycle', this));
// cycle and complete are equal in Task
this.dispatchEvent(createBenchEvent('complete', this));
}
this.dispatchEvent(createBenchEvent('cycle', this));
this.bench.dispatchEvent(createBenchEvent('cycle', this));
// cycle and complete are equal in Task
this.dispatchEvent(createBenchEvent('complete', this));

return this;
}
Expand Down
12 changes: 10 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ test('runs should be equal-more than time and iterations', async () => {

test('events order', async () => {
const controller = new AbortController();
const events: string[] = [];
const bench = new Bench({
signal: controller.signal,
warmupIterations: 0,
warmupTime: 0,
teardown: (task, mode) => {
if (mode === 'run') {
events.push('teardown');
}
},
});

bench
// eslint-disable-next-line @typescript-eslint/no-empty-function
.add('foo', async () => {})
Expand All @@ -60,8 +67,6 @@ test('events order', async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
});

const events: string[] = [];

const error = bench.getTask('error')!;

error.addEventListener('start', () => {
Expand Down Expand Up @@ -136,11 +141,14 @@ test('events order', async () => {
'remove',
'warmup',
'start',
'teardown',
'cycle',
'teardown',
'cycle',
'error-start',
'error-error',
'error',
'teardown',
'error-cycle',
'cycle',
'error-complete',
Expand Down