public void WhenPersistedInstanceLoadedRunEpisodeShouldComplete()
        {
            var activity = new TestBookmark<int> { BookmarkName = "Test" };

            var workflowApplication = new WorkflowApplication(activity)
                {
                   InstanceStore = new MemoryStore(), PersistableIdle = args => PersistableIdleAction.Unload,
                };

            // Episodes can end with until unloaded, aborted, completed, timeout
            // Run episode until unloaded because of persistable idle event
            var workflowIdleEpisodeResult =
                workflowApplication.RunEpisode(Constants.Timeout) as WorkflowIdleEpisodeResult;

            Assert.IsNotNull(workflowIdleEpisodeResult);

            // Cannot resume the same WorkflowApplication - it will cause a System.InvalidOperationException
            // Message=WorkflowInstance (guid) cannot be modified after it has started running.
            var workflowApplicationResume = new WorkflowApplication(activity) { InstanceStore = new MemoryStore(), };

            // Load the instance with a new WorkflowApplication
            workflowApplicationResume.Load(workflowIdleEpisodeResult.InstanceId);

            // Resume and complete
            var result = workflowApplicationResume.ResumeEpisodeBookmark("Test", 1);

            Assert.IsInstanceOfType(result, typeof(WorkflowCompletedEpisodeResult));
            Assert.AreEqual(ActivityInstanceState.Closed, result.State);
            AssertOutArgument.AreEqual(((WorkflowCompletedEpisodeResult)result).Outputs, "Result", 1);
        }
        public void WhenstringleWithPersistenceUnloadRunShouldReturnIdleUnloaded()
        {
            var activity = new TestBookmark<int> { BookmarkName = "Test" };

            var workflowApplication = new WorkflowApplication(activity)
                {
                   InstanceStore = new MemoryStore(), PersistableIdle = args => PersistableIdleAction.Unload,
                };

            // Episodes can end with until unloaded, aborted, completed, timeout
            // Run episode until unloaded because of persistable idle event
            var workflowIdleEpisodeResult =
                workflowApplication.RunEpisode(Constants.Timeout) as WorkflowIdleEpisodeResult;

            Assert.IsNotNull(workflowIdleEpisodeResult);
            Assert.AreEqual(ActivityInstanceState.Executing, workflowIdleEpisodeResult.State);
            Assert.IsTrue(workflowIdleEpisodeResult.Unloaded);
        }
        public void WhenNotInitializedIsInitializedIsFalse()
        {
            const string BookmarkName = "Test";

            var activity = new TestBookmark<int> { BookmarkName = BookmarkName };

            var workflowApplication = new WorkflowApplication(activity);

            Assert.IsFalse(workflowApplication.IsInitialized());
        }
        public void WhenNotAbortedIsAbortedIsFalse()
        {
            const string BookmarkName = "Test";

            var activity = new TestBookmark<int> { BookmarkName = BookmarkName };

            var workflowApplication = new WorkflowApplication(activity);

            workflowApplication.RunEpisode(BookmarkName, Constants.Timeout);

            Assert.IsFalse(workflowApplication.IsAborted());
        }
        public void WhenActivityWithBookmarkAndPersistenceGoesIdleRunAsyncShouldReturnWorkflowIdleEpisodeResult()
        {
            var activity = new TestBookmark<int> { BookmarkName = "Test" };
            var workflowApplication = new WorkflowApplication(activity) { InstanceStore = new MemoryStore() };

            var result = workflowApplication.RunEpisodeAsync("Test", Constants.Timeout).Result;

            Assert.IsInstanceOfType(result, typeof(WorkflowIdleEpisodeResult));

            var completedResult = (WorkflowIdleEpisodeResult)result;
            Assert.AreEqual(ActivityInstanceState.Executing, completedResult.State);
            Assert.AreEqual(1, completedResult.IdleArgs.Bookmarks.Count);
        }
        public void IsReadOnlyDetectsReadOnly()
        {
            const string BookmarkName = "Test";

            var activity = new TestBookmark<int> { BookmarkName = BookmarkName };
            var workflowApplication = new WorkflowApplication(activity);

            // Before you run it, the application is not readonly
            var readOnly = workflowApplication.IsReadOnly();
            Assert.IsFalse(readOnly);

            // Once you run the WorkflowApplication it becomes read only
            workflowApplication.RunEpisode(BookmarkName, Constants.Timeout);

            // This will cause an exception
            // System.InvalidOperationException was unhandled by user code
            // Message=WorkflowInstanceExtensionsManager cannot be modified once it has been associated with a WorkflowInstance.
            AssertHelper.Throws<InvalidOperationException>(
                () => workflowApplication.Extensions.Add(new TraceTrackingParticipant()));

            readOnly = workflowApplication.IsReadOnly();
            Assert.IsTrue(readOnly);
        }
        public void InHandlerThreadDetectsHandlerThread()
        {
            const string BookmarkName = "Test";

            var activity = new TestBookmark<int> { BookmarkName = BookmarkName };
            var inHandler = false;
            WorkflowApplication workflowApplication = null;
            workflowApplication = new WorkflowApplication(activity)
                {
                    // ReSharper disable AccessToModifiedClosure
                    Idle = args =>
                        {
                            if (workflowApplication != null)
                            {
                                inHandler =
                                    workflowApplication.
                                        IsHandlerThread();

                                // workflowApplication.Cancel();
                                // Doh! Can't to this in a handler
                                // System.InvalidOperationException was unhandled by user code
                                // Message=WorkflowApplication operations cannot be performed from within event handlers.
                            }
                        },

                    // ReSharper restore AccessToModifiedClosure
                };

            workflowApplication.RunEpisode(BookmarkName, Constants.Timeout);

            Assert.IsTrue(inHandler);
            Assert.IsFalse(workflowApplication.IsHandlerThread());
        }