|
| 1 | +using JsonFileInstanceStore; |
| 2 | +using System; |
| 3 | +using System.Activities; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using StringToObject = System.Collections.Generic.IDictionary<string, object>; |
| 7 | + |
| 8 | +namespace WorkflowApplicationTestExtensions |
| 9 | +{ |
| 10 | + public static class WorkflowApplicationTestExtensions |
| 11 | + { |
| 12 | + public const string AutoResumedBookmarkNamePrefix = "AutoResumedBookmark_"; |
| 13 | + |
| 14 | + public record WorkflowApplicationResult(StringToObject Outputs, int PersistenceCount); |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Simple API to wait for the workflow to complete or propagate to the caller any error. |
| 18 | + /// Also, when PersistableIdle, will automatically Unload, Load, resume some bookmarks |
| 19 | + /// (those named "AutoResumedBookmark_...") and continue execution. |
| 20 | + /// </summary> |
| 21 | + public static WorkflowApplicationResult RunUntilCompletion(this WorkflowApplication application) |
| 22 | + { |
| 23 | + var persistenceCount = 0; |
| 24 | + var output = new TaskCompletionSource<WorkflowApplicationResult>(); |
| 25 | + application.Completed += (WorkflowApplicationCompletedEventArgs args) => |
| 26 | + { |
| 27 | + if (args.TerminationException is { } ex) |
| 28 | + { |
| 29 | + output.TrySetException(ex); |
| 30 | + } |
| 31 | + if (args.CompletionState == ActivityInstanceState.Canceled) |
| 32 | + { |
| 33 | + throw new OperationCanceledException("Workflow canceled."); |
| 34 | + } |
| 35 | + output.TrySetResult(new(args.Outputs, persistenceCount)); |
| 36 | + }; |
| 37 | + |
| 38 | + application.Aborted += args => output.TrySetException(args.Reason); |
| 39 | + |
| 40 | + application.InstanceStore = new FileInstanceStore(Environment.CurrentDirectory); |
| 41 | + application.PersistableIdle += (WorkflowApplicationIdleEventArgs args) => |
| 42 | + { |
| 43 | + Debug.WriteLine("PersistableIdle"); |
| 44 | + var bookmarks = args.Bookmarks; |
| 45 | + Task.Delay(100).ContinueWith(_ => |
| 46 | + { |
| 47 | + try |
| 48 | + { |
| 49 | + if (++persistenceCount > 100) |
| 50 | + { |
| 51 | + throw new Exception("Persisting too many times, aborting test."); |
| 52 | + } |
| 53 | + application = CloneWorkflowApplication(application); |
| 54 | + application.Load(args.InstanceId); |
| 55 | + foreach (var bookmark in bookmarks) |
| 56 | + { |
| 57 | + application.ResumeBookmark(new Bookmark(bookmark.BookmarkName), null); |
| 58 | + } |
| 59 | + } |
| 60 | + catch (Exception ex) |
| 61 | + { |
| 62 | + output.TrySetException(ex); |
| 63 | + } |
| 64 | + }); |
| 65 | + return PersistableIdleAction.Unload; |
| 66 | + }; |
| 67 | + |
| 68 | + application.BeginRun(null, null); |
| 69 | + |
| 70 | + try |
| 71 | + { |
| 72 | + output.Task.Wait(TimeSpan.FromSeconds(15)); |
| 73 | + } |
| 74 | + catch (Exception ex) when (ex is not OperationCanceledException) |
| 75 | + { |
| 76 | + } |
| 77 | + return output.Task.GetAwaiter().GetResult(); |
| 78 | + } |
| 79 | + |
| 80 | + private static WorkflowApplication CloneWorkflowApplication(WorkflowApplication application) |
| 81 | + { |
| 82 | + var clone = new WorkflowApplication(application.WorkflowDefinition, application.DefinitionIdentity) |
| 83 | + { |
| 84 | + Aborted = application.Aborted, |
| 85 | + Completed = application.Completed, |
| 86 | + PersistableIdle = application.PersistableIdle, |
| 87 | + InstanceStore = application.InstanceStore, |
| 88 | + }; |
| 89 | + // TODO: clone extensions |
| 90 | + return clone; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments