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

feat(worker): expose AbortController to the worker sandbox #1576

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions packages/test/src/test-worker-exposes-abortcontroller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import test from 'ava';
import { v4 as uuid4 } from 'uuid';
import { Client } from '@temporalio/client';
import { RUN_INTEGRATION_TESTS, Worker } from './helpers';
import { defaultOptions } from './mock-native-worker';
import { abortController } from './workflows';


if (RUN_INTEGRATION_TESTS) {
for (const [reuseV8Context, debugMode] of [
[true, true],
[true, false],
[false, true],
[false, false],
]) {
test(`Worker runtime exposes AbortController as a global (reuseV8Context: ${reuseV8Context}, debugMode: ${debugMode})`, async (t) => {
const worker = await Worker.create({ ...defaultOptions, taskQueue: 'test-worker-exposes-abortcontroller', reuseV8Context, debugMode });
const client = new Client();
const result = await worker.runUntil(
client.workflow.execute(abortController, {
args: [],
taskQueue: 'test-worker-exposes-abortcontroller',
workflowId: uuid4(),
workflowExecutionTimeout: '5s',
})
);
t.is(result, 'abort successful');
});
}
}
16 changes: 16 additions & 0 deletions packages/test/src/workflows/abort-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export async function abortController(): Promise<string | null> {
let aborted: string | null = null;

const controller = new AbortController();
const { signal } = controller;

const abortEventListener = () => {
aborted = signal.reason;
};

signal.addEventListener("abort", abortEventListener);
controller.abort("abort successful");
signal.removeEventListener("abort", abortEventListener);

return aborted;
}
1 change: 1 addition & 0 deletions packages/test/src/workflows/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './abort-controller';
export * from './activity-failure';
export * from './activity-failures';
export * from './args-and-return';
Expand Down
1 change: 1 addition & 0 deletions packages/worker/src/workflow/reusable-vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class ReusableVMWorkflowCreator implements WorkflowCreator {
__webpack_module_cache__,
TextEncoder,
TextDecoder,
AbortController,
};
this._context = vm.createContext(globals, { microtaskMode: 'afterEvaluate' });
this.injectConsole();
Expand Down
1 change: 1 addition & 0 deletions packages/worker/src/workflow/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class VMWorkflowCreator implements WorkflowCreator {
__webpack_module_cache__: {},
TextEncoder,
TextDecoder,
AbortController,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't get a test to falsify this change - only the change in reusable-vm makes the tests pass or fail. Is there something I'm missing in terms of how to get this VM class to be the one that's used?

};
const context = vm.createContext(globals, { microtaskMode: 'afterEvaluate' });
this.script.runInContext(context);
Expand Down