Skip to content

Commit 19dda3d

Browse files
committed
Make mockEvent not crash if instance has not been created yet
1 parent 16e6f7f commit 19dda3d

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

packages/workflows-shared/src/engine.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,14 @@ export class Engine extends DurableObject<Env> {
398398
}
399399
}
400400
} else {
401+
const mockEvent = await this.ctx.storage.get(`mockEvent-${event.type}`);
402+
if (mockEvent) {
403+
return;
404+
}
405+
401406
const metadata =
402407
await this.ctx.storage.get<InstanceMetadata>(INSTANCE_METADATA);
408+
403409
if (metadata === undefined) {
404410
throw new Error("Engine was never started");
405411
}

packages/workflows-shared/src/modifier.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class InstanceModifier extends RpcTarget {
5858
return sleepNameCountHash;
5959
}
6060

61-
public async disableSleeps(steps?: StepSelector[]): Promise<void> {
61+
async disableSleeps(steps?: StepSelector[]): Promise<void> {
6262
if (!steps) {
6363
console.log("[Modifier.disableSleeps()] Disabling all sleeps");
6464
await this.#state.storage.put("disableAllSleeps", true);
@@ -74,10 +74,7 @@ export class InstanceModifier extends RpcTarget {
7474
}
7575
}
7676

77-
public async mockStepResult(
78-
step: StepSelector,
79-
stepResult: unknown
80-
): Promise<void> {
77+
async mockStepResult(step: StepSelector, stepResult: unknown): Promise<void> {
8178
console.log(
8279
"[Modifier.mockStepResult()] Mocking step result of step:",
8380
step.name
@@ -86,15 +83,15 @@ export class InstanceModifier extends RpcTarget {
8683
await this.#state.storage.put(`replace-result-${valueKey}`, stepResult);
8784
}
8885

89-
public async mockStepImplementation(
86+
async mockStepImplementation(
9087
_step: StepSelector,
9188
_implementation: () => Promise<unknown>
9289
): Promise<void> {
9390
// TODO
9491
// somehow pass the implementation to context so it can race with timeout
9592
}
9693

97-
public async mockStepError(
94+
async mockStepError(
9895
step: StepSelector,
9996
error: Error,
10097
times?: number
@@ -119,7 +116,7 @@ export class InstanceModifier extends RpcTarget {
119116
}
120117
}
121118

122-
public async forceStepTimeout(step: StepSelector, times?: number) {
119+
async forceStepTimeout(step: StepSelector, times?: number) {
123120
const valueKey = await this.#getStepCacheKey(step);
124121
if (times) {
125122
for (let time = 1; time <= times; time++) {
@@ -132,22 +129,19 @@ export class InstanceModifier extends RpcTarget {
132129
}
133130
}
134131

135-
public async mockEvent(event: UserEvent): Promise<void> {
136-
// could maybe:
137-
// WorkflowInstance.sendEvent()
138-
// Engine.receiveEvent()
139-
// flag with waitForEventKey
140-
132+
async mockEvent(event: UserEvent): Promise<void> {
141133
const myEvent: Event = {
142134
timestamp: new Date(),
143135
payload: event.payload,
144136
type: event.type,
145137
};
146138

139+
await this.#state.storage.put(`mockEvent-${event.type}`, true);
140+
147141
await this.#engine.receiveEvent(myEvent);
148142
}
149143

150-
public async forceEventTimeout(step: StepSelector): Promise<void> {
144+
async forceEventTimeout(step: StepSelector): Promise<void> {
151145
const waitForEventKey = await this.#getWaitForEventCacheKey(step);
152146
await this.#state.storage.put(`forceEventTimeout-${waitForEventKey}`, true);
153147
}

0 commit comments

Comments
 (0)