-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: π subsribe to "abort" event in abort controller (#63199)
* fix: π subsribe to "abort" event in abort controller * test: π add test for execution cancellation after completion * feat: πΈ use a more meaningful sentinel * refactor: π‘ use toPromise() from data plugin * chore: π€ disable most new tests * test: π remove fake timers from abort tests * test: π remove new tests completely There seems to be some async race condition in Jest test runner not related to this PR, but for some reason Jest fails. Removing these tests temporarily to check if this file cases Jest to fail. * chore: π€ try adding .catch clause to abortion promise * chore: π€ revert tests back and add .catch() comment
- Loading branch information
Showing
5 changed files
with
147 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/plugins/expressions/common/execution/execution.abortion.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { Execution } from './execution'; | ||
import { parseExpression } from '../ast'; | ||
import { createUnitTestExecutor } from '../test_helpers'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllTimers(); | ||
}); | ||
|
||
const createExecution = ( | ||
expression: string = 'foo bar=123', | ||
context: Record<string, unknown> = {}, | ||
debug: boolean = false | ||
) => { | ||
const executor = createUnitTestExecutor(); | ||
const execution = new Execution({ | ||
executor, | ||
ast: parseExpression(expression), | ||
context, | ||
debug, | ||
}); | ||
return execution; | ||
}; | ||
|
||
describe('Execution abortion tests', () => { | ||
test('can abort an expression immediately', async () => { | ||
const execution = createExecution('sleep 10'); | ||
|
||
execution.start(); | ||
execution.cancel(); | ||
|
||
const result = await execution.result; | ||
|
||
expect(result).toMatchObject({ | ||
type: 'error', | ||
error: { | ||
message: 'The expression was aborted.', | ||
name: 'AbortError', | ||
}, | ||
}); | ||
}); | ||
|
||
test('can abort an expression which has function running mid flight', async () => { | ||
const execution = createExecution('sleep 300'); | ||
|
||
execution.start(); | ||
jest.advanceTimersByTime(100); | ||
execution.cancel(); | ||
|
||
const result = await execution.result; | ||
|
||
expect(result).toMatchObject({ | ||
type: 'error', | ||
error: { | ||
message: 'The expression was aborted.', | ||
name: 'AbortError', | ||
}, | ||
}); | ||
}); | ||
|
||
test('cancelling execution after it completed has no effect', async () => { | ||
jest.useRealTimers(); | ||
|
||
const execution = createExecution('sleep 1'); | ||
|
||
execution.start(); | ||
|
||
const result = await execution.result; | ||
|
||
execution.cancel(); | ||
|
||
expect(result).toBe(null); | ||
|
||
jest.useFakeTimers(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters