Skip to content

Commit 8bb1860

Browse files
committed
Add mockStepResult
1 parent 2741124 commit 8bb1860

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

packages/workflows-shared/src/context.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,14 @@ export class Context extends RpcTarget {
274274
await this.#state.storage.put(stepStateKey, stepState);
275275
const priorityQueueHash = `${cacheKey}-${stepState.attemptedCount}`;
276276

277-
result = await Promise.race([doWrapperClosure(), timeoutPromise()]);
277+
const replaceResult = await this.#state.storage.get(
278+
`replace-result-${valueKey}`
279+
);
280+
if (replaceResult) {
281+
result = replaceResult;
282+
} else {
283+
result = await Promise.race([doWrapperClosure(), timeoutPromise()]);
284+
}
278285

279286
// if we reach here, means that the clouse ran successfully and we can remove the timeout from the PQ
280287
// @ts-expect-error priorityQueue is initiated in init

packages/workflows-shared/src/modifier.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,45 @@ export class InstanceModifier extends RpcTarget {
2323
this.#state = state;
2424
}
2525

26+
async #getWaitForEventCacheKey(step: StepSelector): Promise<string> {
27+
let count = 1;
28+
if (step.index) {
29+
count = step.index;
30+
}
31+
const name = `${step.name}-${count}`;
32+
const hash = await computeHash(name);
33+
const cacheKey = `${hash}-${count}`;
34+
const waitForEventKey = `${cacheKey}-value`;
35+
36+
return waitForEventKey;
37+
}
38+
39+
async #getStepCacheKey(step: StepSelector): Promise<string> {
40+
const hash = await computeHash(step.name);
41+
let count = 1;
42+
if (step.index) {
43+
count = step.index;
44+
}
45+
const cacheKey = `${hash}-${count}`;
46+
const valueKey = `${cacheKey}-value`;
47+
48+
return valueKey;
49+
}
50+
2651
async disableSleeps(): Promise<void> {
2752
await this.#state.storage.put("disableSleeps", true);
2853
}
2954

55+
async mockStepResult(step: StepSelector, stepResult: unknown): Promise<void> {
56+
const valueKey = await this.#getStepCacheKey(step);
57+
await this.#state.storage.put(`replace-result-${valueKey}`, stepResult);
58+
}
59+
3060
async mockEvent(event: UserEvent): Promise<void> {
31-
console.log("SENDING THE EVENT WITH TYPE", event.type);
3261
// could maybe:
3362
// WorkflowInstance.sendEvent()
3463
// Engine.receiveEvent()
35-
//const waitForEventKey = await this.#getWaitForEventCacheKey(step);
64+
// flag with waitForEventKey
3665

3766
const myEvent: Event = {
3867
timestamp: new Date(),
@@ -43,19 +72,6 @@ export class InstanceModifier extends RpcTarget {
4372
await this.#engine.receiveEvent(myEvent);
4473
}
4574

46-
async #getWaitForEventCacheKey(step: StepSelector): Promise<string> {
47-
let count = 1;
48-
if (step.index) {
49-
count = step.index;
50-
}
51-
const name = `${step.name}-${count}`;
52-
const hash = await computeHash(name);
53-
const cacheKey = `${hash}-${count}`;
54-
const waitForEventKey = `${cacheKey}-value`;
55-
56-
return waitForEventKey;
57-
}
58-
5975
async forceEventTimeout(step: StepSelector): Promise<void> {
6076
const waitForEventKey = await this.#getWaitForEventCacheKey(step);
6177
await this.#state.storage.put(`forceEventTimeout-${waitForEventKey}`, true);

0 commit comments

Comments
 (0)