Skip to content

Commit

Permalink
fixed remaining tests
Browse files Browse the repository at this point in the history
  • Loading branch information
THardy98 committed Dec 19, 2024
1 parent 62a6b9c commit 60265cc
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 167 deletions.
7 changes: 2 additions & 5 deletions packages/test/src/configured-integration-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { ConnectionInjectorInterceptor } from './activities/interceptors';
import {
configurableHelpers,
createLocalTestEnvironment,
createTestWorkflowBundle,
HelperTestBundleOptions,
makeConfigurableEnvironmentTestFn,
} from './helpers-integration';
import { ByteSkewerPayloadCodec, registerDefaultCustomSearchAttributes, Worker } from './helpers';
Expand All @@ -30,10 +28,9 @@ interface TestContext {

const codecs = [undefined, new ByteSkewerPayloadCodec()];

export function makeTestFn(opts: HelperTestBundleOptions): TestFn<TestContext> {
export function makeTestFn(makeBundle: () => Promise<WorkflowBundle>): TestFn<TestContext> {
return makeConfigurableEnvironmentTestFn<TestContext>({
createTestContext: async (_t: ExecutionContext) => {
const workflowBundle = await createTestWorkflowBundle(opts);
const configs: TestConfig[] = [];
await Promise.all(
codecs.map(async (codec) => {
Expand Down Expand Up @@ -67,7 +64,7 @@ export function makeTestFn(opts: HelperTestBundleOptions): TestFn<TestContext> {
})
);
return {
workflowBundle,
workflowBundle: await makeBundle(),
configs,
};
},
Expand Down
45 changes: 2 additions & 43 deletions packages/test/src/test-integration-split-one.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
WorkflowFailedError,
} from '@temporalio/client';
import {
ActivityCancellationType,
ApplicationFailure,
CancelledFailure,
RetryState,
Expand All @@ -27,17 +26,15 @@ import { UnsafeWorkflowInfo, WorkflowInfo } from '@temporalio/workflow/lib/inter

import {
CancellationScope,
condition,
defineQuery,
executeChild,
isCancellation,
proxyActivities,
setHandler,
sleep,
startChild,
workflowInfo,
} from '@temporalio/workflow';
import { configurableHelpers } from './helpers-integration';
import { configurableHelpers, createTestWorkflowBundle } from './helpers-integration';
import * as activities from './activities';
import { cleanOptionalStackTrace, u8, Worker } from './helpers';
import { configMacro, makeTestFn } from './configured-integration-helpers';
Expand All @@ -53,7 +50,7 @@ const { EVENT_TYPE_TIMER_STARTED, EVENT_TYPE_TIMER_FIRED, EVENT_TYPE_TIMER_CANCE
const timerEventTypes = new Set([EVENT_TYPE_TIMER_STARTED, EVENT_TYPE_TIMER_FIRED, EVENT_TYPE_TIMER_CANCELED]);
const CHANGE_MARKER_NAME = 'core_patch';

const test = makeTestFn({ workflowsPath: __filename });
const test = makeTestFn(() => createTestWorkflowBundle({ workflowsPath: __filename }));
test.macro(configMacro);

test('Workflow not found results in task retry', configMacro, async (t, config) => {
Expand Down Expand Up @@ -132,44 +129,6 @@ test('cancel-fake-progress', configMacro, async (t, config) => {
t.pass();
});

export async function cancellableHTTPRequest(url: string): Promise<void> {
const { cancellableFetch } = proxyActivities<typeof activities>({
startToCloseTimeout: '20s',
heartbeatTimeout: '3s',
cancellationType: ActivityCancellationType.WAIT_CANCELLATION_COMPLETED,
});
let activityStarted = false;
setHandler(workflows.activityStartedSignal, () => void (activityStarted = true));
try {
await CancellationScope.cancellable(async () => {
const promise = cancellableFetch(url, true);
await condition(() => activityStarted);
CancellationScope.current().cancel();
await promise;
});
} catch (err) {
if (!isCancellation(err)) {
throw err;
}
}
}

// TODO(thomas): fix, withZeroesHTTPServer uses node:http
/*
test('cancel-http-request', configMacro, async (t, config) => {
const { env, createWorkerWithDefaults } = config;
const { executeWorkflow } = configurableHelpers(t, t.context.workflowBundle, env);
const worker = await createWorkerWithDefaults(t, { activities });
await withZeroesHTTPServer(async (port) => {
const url = `http://127.0.0.1:${port}`;
await worker.runUntil(executeWorkflow(cancellableHTTPRequest, {
args: [url],
}));
});
t.pass();
});
*/

export async function activityFailure(useApplicationFailure: boolean): Promise<void> {
const { throwAnError } = proxyActivities({
startToCloseTimeout: '5s',
Expand Down
143 changes: 143 additions & 0 deletions packages/test/src/test-integration-split-three.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import path from 'node:path';
import v8 from 'node:v8';
import { readFileSync } from 'node:fs';
import pkg from '@temporalio/worker/lib/pkg';
import { bundleWorkflowCode } from '@temporalio/worker';
import { configMacro, makeTestFn } from './configured-integration-helpers';
import { configurableHelpers } from './helpers-integration';
import { withZeroesHTTPServer } from './zeroes-http-server';
import * as activities from './activities';
import { cleanOptionalStackTrace } from './helpers';
import * as workflows from './workflows';

const test = makeTestFn(() => bundleWorkflowCode({ workflowsPath: require.resolve('./workflows') }));
test.macro(configMacro);

test('cancel-http-request', configMacro, async (t, config) => {
const { env, createWorkerWithDefaults } = config;
const { executeWorkflow } = configurableHelpers(t, t.context.workflowBundle, env);
const worker = await createWorkerWithDefaults(t, { activities });
await withZeroesHTTPServer(async (port) => {
const url = `http://127.0.0.1:${port}`;
await worker.runUntil(
executeWorkflow(workflows.cancellableHTTPRequest, {
args: [url],
})
);
});
t.pass();
});

// TODO(thomas): fix
if ('promiseHooks' in v8) {
// Skip in old node versions
test('Stack trace query returns stack that makes sense', configMacro, async (t, config) => {
const { env, createWorkerWithDefaults } = config;
const { executeWorkflow } = configurableHelpers(t, t.context.workflowBundle, env);
const worker = await createWorkerWithDefaults(t, { activities });
const rawStacks = await worker.runUntil(executeWorkflow(workflows.stackTracer));

const [stack1, stack2] = rawStacks.map((r) =>
r
.split('\n\n')
.map((s) => cleanOptionalStackTrace(`\n${s}`))
.join('\n')
);
// Can't get the Trigger stack cleaned, this is okay for now
// NOTE: we check endsWith because under certain conditions we might see Promise.race in the trace
t.true(
stack1.endsWith(
`
at Function.all (<anonymous>)
at stackTracer (test/src/workflows/stack-tracer.ts)
at stackTracer (test/src/workflows/stack-tracer.ts)
at Promise.then (<anonymous>)
at Trigger.then (workflow/src/trigger.ts)`
),
`Got invalid stack:\n--- clean ---\n${stack1}\n--- raw ---\n${rawStacks[0]}`
);

t.is(
stack2,
`
at executeChild (workflow/src/workflow.ts)
at stackTracer (test/src/workflows/stack-tracer.ts)
at new Promise (<anonymous>)
at timerNextHandler (workflow/src/workflow.ts)
at sleep (workflow/src/workflow.ts)
at stackTracer (test/src/workflows/stack-tracer.ts)
at stackTracer (test/src/workflows/stack-tracer.ts)`
);
});

test('Enhanced stack trace returns trace that makes sense', configMacro, async (t, config) => {
const { env, createWorkerWithDefaults } = config;

const { executeWorkflow } = configurableHelpers(t, t.context.workflowBundle, env);
const worker = await createWorkerWithDefaults(t, { activities });
const enhancedStack = await worker.runUntil(executeWorkflow(workflows.enhancedStackTracer));

const stacks = enhancedStack.stacks.map((s) => ({
locations: s.locations.map((l) => ({
...l,
...(l.file_path
? { file_path: l.file_path.replace(path.resolve(__dirname, '../../../'), '').replace(/\\/g, '/') }
: undefined),
})),
}));
t.is(enhancedStack.sdk.name, 'typescript');
t.is(enhancedStack.sdk.version, pkg.version); // Expect workflow and worker versions to match
t.deepEqual(stacks, [
{
locations: [
{
function_name: 'Function.all',
internal_code: false,
},
{
file_path: '/packages/test/src/workflows/stack-tracer.ts',
function_name: 'enhancedStackTracer',
line: 32,
column: 35,
internal_code: false,
},
],
},
{
locations: [
{
file_path: '/packages/test/src/workflows/stack-tracer.ts',
function_name: 'enhancedStackTracer',
line: 32,
column: 35,
internal_code: false,
},
],
},
{
locations: [
{
function_name: 'Promise.then',
internal_code: false,
},
{
file_path: '/packages/workflow/src/trigger.ts',
function_name: 'Trigger.then',
line: 47,
column: 24,
internal_code: false,
},
],
},
]);
const expectedSources = ['../src/workflows/stack-tracer.ts', '../../workflow/src/trigger.ts'].map((p) => [
path.resolve(__dirname, p),
[{ content: readFileSync(path.resolve(__dirname, p), 'utf8'), line_offset: 0 }],
]);
t.deepEqual(Object.entries(enhancedStack.sources), expectedSources);
});
}
121 changes: 2 additions & 119 deletions packages/test/src/test-integration-split-two.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { msToNumber, tsToMs } from '@temporalio/common/lib/time';
import { decode as payloadDecode, decodeFromPayloadsAtIndex } from '@temporalio/common/lib/internal-non-workflow';

import { condition, defineQuery, setHandler, sleep } from '@temporalio/workflow';
import { configurableHelpers } from './helpers-integration';
import { configurableHelpers, createTestWorkflowBundle } from './helpers-integration';
import * as activities from './activities';
import * as workflows from './workflows';
import { makeTestFn, configMacro } from './configured-integration-helpers';
Expand All @@ -24,7 +24,7 @@ import { makeTestFn, configMacro } from './configured-integration-helpers';
// - review the files where these workflows are shared
export * from './workflows';

const test = makeTestFn({ workflowsPath: __filename });
const test = makeTestFn(() => createTestWorkflowBundle({ workflowsPath: __filename }));
test.macro(configMacro);

test('WorkflowOptions are passed correctly with defaults', configMacro, async (t, config) => {
Expand Down Expand Up @@ -678,123 +678,6 @@ test(
}
);

// TODO(thomas): fix
/*
if ('promiseHooks' in v8) {
// Skip in old node versions
test('Stack trace query returns stack that makes sense', configMacro, async (t, config) => {
const { env, createWorkerWithDefaults } = config;
const { executeWorkflow, createWorker } = configurableHelpers(t, t.context.workflowBundle, env);
const worker = await createWorkerWithDefaults(t, { activities });
const rawStacks = await worker.runUntil(executeWorkflow(workflows.stackTracer));
const [stack1, stack2] = rawStacks.map((r) =>
r
.split('\n\n')
.map((s) => cleanOptionalStackTrace(`\n${s}`))
.join('\n')
);
// Can't get the Trigger stack cleaned, this is okay for now
// NOTE: we check endsWith because under certain conditions we might see Promise.race in the trace
t.true(
stack1.endsWith(
`
at Function.all (<anonymous>)
at stackTracer (test/src/workflows/stack-tracer.ts)
at stackTracer (test/src/workflows/stack-tracer.ts)
at Promise.then (<anonymous>)
at Trigger.then (workflow/src/trigger.ts)`
),
`Got invalid stack:\n--- clean ---\n${stack1}\n--- raw ---\n${rawStacks[0]}`
);
t.is(
stack2,
`
at executeChild (workflow/src/workflow.ts)
at stackTracer (test/src/workflows/stack-tracer.ts)
at new Promise (<anonymous>)
at timerNextHandler (workflow/src/workflow.ts)
at sleep (workflow/src/workflow.ts)
at stackTracer (test/src/workflows/stack-tracer.ts)
at stackTracer (test/src/workflows/stack-tracer.ts)`
);
});
test('Enhanced stack trace returns trace that makes sense', configMacro, async (t, config) => {
const { env, createWorkerWithDefaults } = config;
const { executeWorkflow, createWorker } = configurableHelpers(t, t.context.workflowBundle, env);
const worker = await createWorkerWithDefaults(t, { activities });
const enhancedStack = await worker.runUntil(executeWorkflow(workflows.enhancedStackTracer));
const stacks = enhancedStack.stacks.map((s) => ({
locations: s.locations.map((l) => ({
...l,
...(l.file_path
? { file_path: l.file_path.replace(path.resolve(__dirname, '../../../'), '').replace(/\\/g, '/') }
: undefined),
})),
}));
t.is(enhancedStack.sdk.name, 'typescript');
t.is(enhancedStack.sdk.version, pkg.version); // Expect workflow and worker versions to match
t.deepEqual(stacks, [
{
locations: [
{
function_name: 'Function.all',
internal_code: false,
},
{
file_path: '/packages/test/src/workflows/stack-tracer.ts',
function_name: 'enhancedStackTracer',
line: 32,
column: 35,
internal_code: false,
},
],
},
{
locations: [
{
file_path: '/packages/test/src/workflows/stack-tracer.ts',
function_name: 'enhancedStackTracer',
line: 32,
column: 35,
internal_code: false,
},
],
},
{
locations: [
{
function_name: 'Promise.then',
internal_code: false,
},
{
file_path: '/packages/workflow/src/trigger.ts',
function_name: 'Trigger.then',
line: 47,
column: 24,
internal_code: false,
},
],
},
]);
const expectedSources = ['../src/workflows/stack-tracer.ts', '../../workflow/src/trigger.ts'].map((p) => [
path.resolve(__dirname, p),
[{ content: readFileSync(path.resolve(__dirname, p), 'utf8'), line_offset: 0 }],
]);
t.deepEqual(Object.entries(enhancedStack.sources), expectedSources);
});
}
*/

const mutateWorkflowStateQuery = defineQuery<void>('mutateWorkflowState');
export async function queryAndCondition(): Promise<void> {
let mutated = false;
Expand Down
Loading

0 comments on commit 60265cc

Please sign in to comment.