Skip to content

Commit

Permalink
Merge branch 'main' into refactor-old-integration-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
THardy98 authored Jan 13, 2025
2 parents b0a0c6d + 6a4ea4a commit 44436b1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
10 changes: 7 additions & 3 deletions packages/create-project/src/helpers/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,13 @@ export async function replaceSdkVersion({ root, sdkVersion }: InstallArgs): Prom
const fileName = `${root}/package.json`;

const packageJson = JSON.parse(await readFile(fileName, 'utf8'));
for (const packageName in packageJson.dependencies) {
if (packageName.startsWith('@temporalio/')) {
packageJson.dependencies[packageName] = sdkVersion;
for (const depType of ['dependencies', 'devDependencies', 'peerDependencies']) {
if (packageJson[depType]) {
for (const packageName in packageJson[depType]) {
if (packageName.startsWith('@temporalio/')) {
packageJson[depType][packageName] = sdkVersion;
}
}
}
}
await writeFile(fileName, JSON.stringify(packageJson, null, 2));
Expand Down
36 changes: 36 additions & 0 deletions packages/test/src/test-integration-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '@temporalio/client';
import * as wf from '@temporalio/workflow';
import { temporal } from '@temporalio/proto';
import { LogEntry } from '@temporalio/worker';
import { helpers, makeTestFunction } from './helpers-integration';
import { signalUpdateOrderingWorkflow } from './workflows/signal-update-ordering';
import { signalsActivitiesTimersPromiseOrdering } from './workflows/signals-timers-activities-order';
Expand All @@ -19,6 +20,8 @@ import { loadHistory, waitUntil } from './helpers';
// polling/retry strategies result in the expected behavior
const LONG_POLL_EXPIRATION_INTERVAL_SECONDS = 5.0;

const recordedLogs: { [workflowId: string]: LogEntry[] } = {};

const test = makeTestFunction({
workflowsPath: __filename,
workflowEnvironmentOpts: {
Expand All @@ -29,6 +32,7 @@ const test = makeTestFunction({
],
},
},
recordedLogs,
});

export const update = wf.defineUpdate<string[], [string]>('update');
Expand Down Expand Up @@ -1016,3 +1020,35 @@ test('Can complete update after workflow returns - pre-1.11.0 compatibility', as
await runReplayHistory({}, hist);
t.pass();
});

const logUpdate = wf.defineUpdate<[string, string], [string]>('log-update');
export async function workflowWithLogInUpdate(): Promise<void> {
const updateHandler = (msg: string): [string, string] => {
const updateInfo = wf.currentUpdateInfo();
if (!updateInfo) {
throw new Error('expected updateInfo to be defined');
}
wf.log.info(msg);
return [updateInfo.id, updateInfo.name];
};
wf.setHandler(logUpdate, updateHandler);
await wf.condition(() => false);
}

test('Workflow Worker logs update info when logging within update handler', async (t) => {
const { createWorker, startWorkflow } = helpers(t);
const worker = await createWorker();
await worker.runUntil(async () => {
const wfHandle = await startWorkflow(workflowWithLogInUpdate);
const logMsg = 'log msg';
const [updateId, updateName] = await wfHandle.executeUpdate(logUpdate, { args: [logMsg] });
t.true(
recordedLogs[wfHandle.workflowId].some(
(logEntry) =>
logEntry.meta?.updateName === updateName &&
logEntry.meta?.updateId === updateId &&
logEntry.message === logMsg
)
);
});
});
12 changes: 11 additions & 1 deletion packages/workflow/src/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { type Sink, type Sinks, proxySinks } from './sinks';
import { isCancellation } from './errors';
import { WorkflowInfo, ContinueAsNew } from './interfaces';
import { assertInWorkflowContext } from './global-attributes';
import { currentUpdateInfo, inWorkflowContext } from './workflow';

export interface WorkflowLogger extends Sink {
trace(message: string, attrs?: Record<string, unknown>): void;
Expand Down Expand Up @@ -117,11 +118,20 @@ export function executeWithLifecycleLogging(fn: () => Promise<unknown>): Promise
* Note that this function may be called from outside of the Workflow context (eg. by the worker itself).
*/
export function workflowLogAttributes(info: WorkflowInfo): Record<string, unknown> {
return {
const attributes: { [key: string]: string } = {
namespace: info.namespace,
taskQueue: info.taskQueue,
workflowId: info.workflowId,
runId: info.runId,
workflowType: info.workflowType,
};
if (inWorkflowContext()) {
const updateInfo = currentUpdateInfo();
if (updateInfo) {
// Add update info if it exists
attributes['updateId'] = updateInfo.id;
attributes['updateName'] = updateInfo.name;
}
}
return attributes;
}
11 changes: 10 additions & 1 deletion scripts/init-from-verdaccio.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ async function main() {
console.log('spawning npx @temporalio/create with args:', initArgs);
try {
const npmConfigFile = resolve(registryDir, 'npmrc-custom');
const npmConfig = `@temporalio:registry=http://127.0.0.1:4873`;
let npmConfig = `@temporalio:registry=http://127.0.0.1:4873`;

if (!process.env?.['CI']) {
// When testing on dev's local machine, uses an isolated NPM cache directory to avoid mixing
// existing @temporalio/* cached packages with the ones from the local registry. We don't do
// that in CI though, as it is not needed (i.e. there should be no such cached packages yet)
// and would slow down the tests (i.e. it requires redownloading ALL packages).
npmConfig += `\ncache=${resolve(registryDir, 'npm-cache')}`;
}

writeFileSync(npmConfigFile, npmConfig, { encoding: 'utf-8' });

await spawnNpx(
Expand Down
2 changes: 1 addition & 1 deletion scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ async function spawnNpx(args, opts) {
await waitOnChild(spawn(npx, npxArgs, { ...opts, shell }));
}

module.exports = { kill, spawnNpx, ChildProcessError, shell, sleep };
module.exports = { kill, spawnNpx, waitOnChild, ChildProcessError, shell, sleep };

0 comments on commit 44436b1

Please sign in to comment.