orcher-sdk 0.5.2

Rust SDK for Orcher, a durable workflow engine: workflows, tasks, actors, workers and a client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! [`TestWorkflowExecutor`], which runs a workflow in memory and serves its tasks from
//! mocks, without an Orcher server.

use crate::error::{Error, Result};
use crate::testing::mocks::MockRegistry;
use crate::workflow::{ScheduleTaskCommand, WorkflowCommand, WorkflowContext, WorkflowExecution};
use serde::{de::DeserializeOwned, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

/// Runs a workflow in memory, serving its tasks from a [`MockRegistry`].
///
/// Each time the workflow suspends on tasks, the executor runs their mocks and
/// replays the workflow from the start with the results injected, the same way a
/// worker replays history. Only task suspensions are handled: a workflow that waits
/// on a timer, event or child workflow returns an error.
pub struct TestWorkflowExecutor {
    mocks: Arc<RwLock<MockRegistry>>,

    /// Replay limit, so a workflow that keeps scheduling tasks cannot loop forever
    max_steps: usize,

    /// Log each step through `tracing`
    verbose: bool,
}

impl TestWorkflowExecutor {
    /// Create an executor over `mocks`, with a limit of 1000 steps.
    pub fn new(mocks: Arc<RwLock<MockRegistry>>) -> Self {
        Self {
            mocks,
            max_steps: 1000,
            verbose: false,
        }
    }

    /// Set how many times the workflow may be run before the executor gives up.
    pub fn with_max_steps(mut self, max_steps: usize) -> Self {
        self.max_steps = max_steps;
        self
    }

    /// Set whether to log each step through `tracing`.
    pub fn with_verbose(mut self, verbose: bool) -> Self {
        self.verbose = verbose;
        self
    }

    /// Run a context-aware workflow to completion, serving its tasks from mocks.
    ///
    /// Each step builds a fresh context holding every task result so far (in replay
    /// mode once there are results) and runs the workflow in it. If the workflow
    /// suspends, the executor reads the commands it scheduled from a clone of that
    /// context, runs the mock for each new task, and runs the workflow again. Tasks
    /// that already have a result are not run twice.
    ///
    /// # Errors
    ///
    /// Returns the workflow's own error, or an error if a scheduled task has no mock,
    /// the workflow suspends on something other than a task, or it takes more than
    /// the step limit.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use orcher_sdk::testing::TestWorkflowExecutor;
    /// # use orcher_sdk::testing::MockRegistry;
    /// # use orcher_sdk::workflow::{WorkflowContext, WorkflowExecution};
    /// # use orcher_sdk::prelude::*;
    /// # use std::sync::{Arc, RwLock};
    /// # async fn example() -> Result<()> {
    /// let mut registry = MockRegistry::new();
    /// registry.register("my_task".to_string(),
    ///     orcher_sdk::testing::TaskMock::Fixed {
    ///         result: serde_json::to_vec(&"result")?
    ///     }
    /// );
    ///
    /// let mocks = Arc::new(RwLock::new(registry));
    /// let mut executor = TestWorkflowExecutor::new(mocks);
    ///
    /// let execution = WorkflowExecution {
    ///     workflow_id: "test-wf-1".to_string(),
    ///     run_id: "test-run-1".to_string(),
    ///     workflow_type: "TestWorkflow".to_string(),
    ///     attempt: 1,
    ///     namespace: "test".to_string(),
    ///     task_queue: "test-queue".to_string(),
    /// };
    ///
    /// let result: String = executor.execute_workflow(
    ///     |ctx, input: String| async move {
    ///         let task_result: String = ctx.execute_task("my_task", input).await?;
    ///         Ok(task_result)
    ///     },
    ///     execution,
    ///     "input".to_string(),
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn execute_workflow<F, Fut, I, O>(
        &mut self,
        workflow_fn: F,
        execution: WorkflowExecution,
        input: I,
    ) -> Result<O>
    where
        F: Fn(WorkflowContext, I) -> Fut,
        Fut: std::future::Future<Output = Result<O>>,
        I: Clone + Serialize + DeserializeOwned,
        O: Serialize + DeserializeOwned,
    {
        let mut step = 0;
        let mut task_results: HashMap<String, Vec<u8>> = HashMap::new();

        if self.verbose {
            tracing::info!(
                workflow_id = %execution.workflow_id,
                workflow_type = %execution.workflow_type,
                "Starting workflow execution in test mode"
            );
        }

        loop {
            step += 1;
            if step > self.max_steps {
                return Err(Error::Workflow(crate::error::WorkflowError::StateError(
                    format!(
                        "Workflow exceeded maximum steps ({}). This might indicate an infinite loop or you need to configure max_steps higher.",
                        self.max_steps
                    ),
                )));
            }

            let context = self.create_context_with_results(
                execution.clone(),
                task_results.clone(),
                !task_results.is_empty(), // replay if we have task results
            );

            // Clones share state, so the commands the workflow schedules are readable
            // from this clone after the original has been moved into the workflow.
            let context_clone = context.clone();

            if self.verbose {
                tracing::debug!(
                    workflow_id = %execution.workflow_id,
                    step = step,
                    task_results_count = task_results.len(),
                    replaying = !task_results.is_empty(),
                    "Executing workflow step"
                );
            }

            let result = workflow_fn(context, input.clone()).await;

            match result {
                Ok(output) => {
                    if self.verbose {
                        tracing::info!(
                            workflow_id = %execution.workflow_id,
                            steps = step,
                            "Workflow completed successfully"
                        );
                    }
                    return Ok(output);
                }
                Err(e) => {
                    let error_msg = e.to_string();

                    // Suspension is recognized by its message, which is
                    // "Workflow suspended: {reason}".
                    if error_msg.contains("Workflow suspended") {
                        let commands = context_clone.take_commands_for_test();

                        if self.verbose {
                            tracing::debug!(
                                workflow_id = %execution.workflow_id,
                                commands_count = commands.len(),
                                "Processing commands"
                            );
                        }

                        let mut has_new_tasks = false;
                        for command in commands {
                            if let WorkflowCommand::ScheduleTask(task_cmd) = command {
                                if task_results.contains_key(&task_cmd.task_id) {
                                    if self.verbose {
                                        tracing::trace!(
                                            task_id = %task_cmd.task_id,
                                            "Skipping already-executed task"
                                        );
                                    }
                                    continue;
                                }

                                has_new_tasks = true;

                                let task_result = self.execute_mock_task(&task_cmd)?;
                                task_results.insert(task_cmd.task_id.clone(), task_result);

                                if self.verbose {
                                    tracing::debug!(
                                        workflow_id = %execution.workflow_id,
                                        task_type = %task_cmd.task_type,
                                        task_id = %task_cmd.task_id,
                                        "Executed mocked task"
                                    );
                                }
                            }
                        }

                        if !has_new_tasks {
                            // Suspended with no new task: the workflow is waiting on an
                            // event, timer or child workflow, which this executor cannot
                            // resolve.
                            return Err(Error::Workflow(crate::error::WorkflowError::StateError(
                                format!(
                                    "Workflow suspended on non-task operation at step {}. \
                                     Operations like wait_for_event, timer, or child workflows \
                                     are not yet fully supported in TestEnv. \
                                     Task execution with mocks is fully supported.",
                                    step
                                ),
                            )));
                        }

                        // Loop again with the new task results.
                    } else {
                        // A real failure of the workflow.
                        if self.verbose {
                            tracing::error!(
                                workflow_id = %execution.workflow_id,
                                error = %e,
                                step = step,
                                "Workflow execution failed"
                            );
                        }
                        return Err(e);
                    }
                }
            }
        }
    }

    /// Run a workflow that takes no context.
    ///
    /// Such a workflow cannot call tasks, so it runs once, directly, and no mocks
    /// are involved. `_execution` is ignored.
    pub async fn execute_simple_workflow<F, Fut, I, O>(
        &mut self,
        workflow_fn: F,
        _execution: WorkflowExecution,
        input: I,
    ) -> Result<O>
    where
        F: Fn(I) -> Fut,
        Fut: std::future::Future<Output = Result<O>>,
        I: Clone + Serialize + DeserializeOwned,
        O: Serialize + DeserializeOwned,
    {
        workflow_fn(input).await
    }

    /// Run the mock registered for a scheduled task and return its result as JSON bytes.
    fn execute_mock_task(&mut self, task_cmd: &ScheduleTaskCommand) -> Result<Vec<u8>> {
        let mut registry = self.mocks.write().unwrap();

        if !registry.has_mock(&task_cmd.task_type) {
            return Err(Error::Workflow(crate::error::WorkflowError::StateError(
                format!(
                    "No mock registered for task '{}'. Use env.mock_task(\"{}\").returns(...) to register a mock before executing the workflow.",
                    task_cmd.task_type,
                    task_cmd.task_type
                ),
            )));
        }

        // The executor does not know the task's types, so input and output pass
        // through `serde_json::Value`.
        let input_any: serde_json::Value =
            serde_json::from_slice(&task_cmd.input).map_err(|e| {
                Error::Serialization(format!("Failed to deserialize task input: {}", e))
            })?;

        let result_any: serde_json::Value =
            registry.execute_mock(&task_cmd.task_type, input_any)?;

        let result_bytes = serde_json::to_vec(&result_any)
            .map_err(|e| Error::Serialization(format!("Failed to serialize task result: {}", e)))?;

        Ok(result_bytes)
    }

    /// Build a workflow context with `task_results` injected, keyed by task ID.
    fn create_context_with_results(
        &self,
        execution: WorkflowExecution,
        task_results: HashMap<String, Vec<u8>>,
        replaying: bool,
    ) -> WorkflowContext {
        let context = WorkflowContext::new(execution, false, "1.0.0".to_string());

        context.set_replaying_for_test(replaying);

        for (task_id, result) in task_results {
            context.inject_task_result_for_test(task_id, result);
        }

        context
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_executor_creation() {
        let mocks = Arc::new(RwLock::new(MockRegistry::new()));
        let executor = TestWorkflowExecutor::new(mocks);
        assert_eq!(executor.max_steps, 1000);
        assert!(!executor.verbose);
    }

    #[test]
    fn test_executor_with_max_steps() {
        let mocks = Arc::new(RwLock::new(MockRegistry::new()));
        let executor = TestWorkflowExecutor::new(mocks).with_max_steps(500);
        assert_eq!(executor.max_steps, 500);
    }

    #[test]
    fn test_executor_with_verbose() {
        let mocks = Arc::new(RwLock::new(MockRegistry::new()));
        let executor = TestWorkflowExecutor::new(mocks).with_verbose(true);
        assert!(executor.verbose);
    }

    #[tokio::test]
    async fn test_simple_workflow_execution() {
        let mocks = Arc::new(RwLock::new(MockRegistry::new()));
        let mut executor = TestWorkflowExecutor::new(mocks);

        let execution = WorkflowExecution {
            workflow_id: "test-wf-1".to_string(),
            run_id: "test-run-1".to_string(),
            workflow_type: "TestWorkflow".to_string(),
            attempt: 1,
            namespace: "test".to_string(),
            task_queue: "test-queue".to_string(),
        };

        let workflow_fn = |input: i32| async move { Ok(input * 2) };
        let result: i32 = executor
            .execute_simple_workflow(workflow_fn, execution, 21)
            .await
            .unwrap();

        assert_eq!(result, 42);
    }

    #[tokio::test]
    async fn test_context_workflow_without_tasks() {
        let mocks = Arc::new(RwLock::new(MockRegistry::new()));
        let mut executor = TestWorkflowExecutor::new(mocks);

        let execution = WorkflowExecution {
            workflow_id: "test-wf-2".to_string(),
            run_id: "test-run-2".to_string(),
            workflow_type: "ContextWorkflow".to_string(),
            attempt: 1,
            namespace: "test".to_string(),
            task_queue: "test-queue".to_string(),
        };

        let workflow_fn = |ctx: WorkflowContext, input: String| async move {
            let wf_id = ctx.workflow_id();
            Ok(format!("{}-{}", wf_id, input))
        };
        let result: String = executor
            .execute_workflow(workflow_fn, execution.clone(), "hello".to_string())
            .await
            .unwrap();

        assert_eq!(result, format!("{}-hello", execution.workflow_id));
    }

    #[tokio::test]
    async fn test_context_cloning() {
        let execution = WorkflowExecution {
            workflow_id: "test-wf-3".to_string(),
            run_id: "test-run-3".to_string(),
            workflow_type: "TestWorkflow".to_string(),
            attempt: 1,
            namespace: "test".to_string(),
            task_queue: "test-queue".to_string(),
        };

        let ctx = WorkflowContext::new(execution, false, "1.0.0".to_string());
        let ctx_clone = ctx.clone();

        assert_eq!(ctx.workflow_id(), ctx_clone.workflow_id());
        assert_eq!(ctx.workflow_type(), ctx_clone.workflow_type());
        assert_eq!(ctx.is_replaying(), ctx_clone.is_replaying());
    }
}