private static void TestInstanceOperationFromResumeBookmarkCallback(int operationsId, bool isSync)
        {
            string         shouldNotExecuteMsg = "Should not see this message";
            Variable <int> value           = VariableHelper.Create <int>("value");
            TestWriteLine  writeLineNotRun = new TestWriteLine("NotExecuted", shouldNotExecuteMsg)
            {
            };
            TestSequence testSequence = new TestSequence()
            {
                Variables  = { value },
                Activities =
                {
                    new TestWriteLine()
                    {
                        Message = "Workflow Started"
                    },
                    new TestWaitReadLine <int>("Read", "Read")
                    {
                        BookmarkValue = value
                    },
                }
            };

            //Get Expected Trace without TestWriteLine()
            if (operationsId == 2)
            {
                testSequence.ExpectedOutcome = Outcome.Canceled;
            }

            ExpectedTrace expectedTrace = testSequence.GetExpectedTrace();

            if (operationsId == 4)
            {
                expectedTrace.Trace.Steps.RemoveAt(expectedTrace.Trace.Steps.Count - 1);
            }
            else if (operationsId == 3)
            {
                expectedTrace.Trace.Steps.RemoveAt(expectedTrace.Trace.Steps.Count - 1);
                expectedTrace.Trace.Steps.Add(new ActivityTrace(testSequence.DisplayName, ActivityInstanceState.Faulted));
            }


            //Now Add TestWriteLine to workflow
            testSequence.Activities.Add(writeLineNotRun);

            TestWorkflowRuntimeAsyncResult asyncResultResume    = null;
            TestWorkflowRuntimeAsyncResult asyncResultOperation = null;
            string message = "";

            //Execute Workflow
            JsonFileInstanceStore.FileInstanceStore jsonStore = new JsonFileInstanceStore.FileInstanceStore(".\\~");
            // using PersistableIdleAction.None here because the idle unload was racing with the resume bookmark after the wait for the BeforeWait trace.
            TestWorkflowRuntime workflowRuntime = TestRuntime.CreateTestWorkflowRuntime(testSequence, null, jsonStore, PersistableIdleAction.None);

            workflowRuntime.ExecuteWorkflow();
            workflowRuntime.WaitForActivityStatusChange("Read", TestActivityInstanceState.Executing);
            if (isSync)
            {
                //Log.Info("Resuming Bookmark");
                if (isSync)
                {
                    workflowRuntime.ResumeBookMark("Read", 9999);
                }
                else
                {
                    asyncResultResume = workflowRuntime.BeginResumeBookMark("Read", 9999, null, null);
                }

                workflowRuntime.WaitForTrace(new UserTrace(WaitReadLine <int> .BeforeWait));
                switch (operationsId)
                {
                case 2:
                    //Cancel Workflow during OnResumeBookmark is executing
                    //Log.Info("CancelWorkflow during OnResumeBookmark executing");
                    if (isSync)
                    {
                        workflowRuntime.CancelWorkflow();
                    }
                    else
                    {
                        asyncResultOperation = workflowRuntime.BeginCancelWorkflow(null, null);

                        workflowRuntime.EndResumeBookMark(asyncResultResume);
                        workflowRuntime.EndCancelWorkflow(asyncResultOperation);
                    }
                    //Trace.WriteLine should not execute
                    break;

                case 3:
                    //Terminate Workflow during OnResumeBookmark is executing
                    //Log.Info("TerminateWorkflow during OnResumeBookmark executing");
                    if (isSync)
                    {
                        workflowRuntime.TerminateWorkflow("Terminate Exception");
                    }
                    else
                    {
                        asyncResultOperation = workflowRuntime.BeginTerminateWorkflow("Terminate Exception", null, null);

                        workflowRuntime.EndResumeBookMark(asyncResultResume);
                        workflowRuntime.EndTerminateWorkflow(asyncResultOperation);
                    }
                    //Trace.WriteLine should not execute.
                    break;

                case 4:
                    //Unload Workflow during OnResumeBookmark is executing
                    //This should wait till ResumeMark finishes the work
                    //Log.Info("UnloadWorkflow during OnResumeBookmark executing");
                    if (isSync)
                    {
                        workflowRuntime.UnloadWorkflow();
                    }
                    else
                    {
                        asyncResultOperation = workflowRuntime.BeginUnloadWorkflow(null, null);

                        workflowRuntime.EndResumeBookMark(asyncResultResume);
                        workflowRuntime.EndUnloadWorkflow(asyncResultOperation);
                    }

                    //message = String.Format(ExceptionStrings.WorkflowInstanceUnloaded, workflowRuntime.CurrentWorkflowInstanceId);
                    ExceptionHelpers.CheckForException(typeof(WorkflowApplicationUnloadedException), message, new ExceptionHelpers.MethodDelegate(
                                                           delegate
                    {
                        workflowRuntime.ResumeWorkflow();
                    }));

                    break;
                }
            }

            if (isSync)
            {
                switch (operationsId)
                {
                case 2:
                {
                    workflowRuntime.WaitForCanceled(expectedTrace);
                    break;
                }

                case 3:
                {
                    workflowRuntime.WaitForTerminated(1, out Exception terminationException, expectedTrace);
                    break;
                }

                case 4:
                {
                    // We tried to do a ResumeWorkflow without loading it after an unload, so we expected
                    // to get a WorkflowApplicationUnloadedException. The workflow will never complete,
                    // so don't wait for it to complete.
                    break;
                }
                }
            }
            else
            {
                //Give some time for Workflow to execute
                Thread.CurrentThread.Join((int)TimeSpan.FromSeconds(1).TotalMilliseconds);
            }

            if (isSync)
            {
                expectedTrace.AddIgnoreTypes(typeof(WorkflowInstanceTrace));
                workflowRuntime.ActualTrace.Validate(expectedTrace);
            }
            else
            {
                //The traces are vary in the async situations, thus we can not do a full trace valdation
                //validate the writeline after read activity is not executed is sufficient
                if (workflowRuntime.ActualTrace.ToString().Contains(shouldNotExecuteMsg))
                {
                    throw new Exception("The NotExecuted WriteLine activity has been executed, the expectation is it does not");
                }
            }
        }
        public static void TestOperationsResumeBookmarkCallback(int operationId)
        {
            Variable <int> value        = VariableHelper.Create <int>("value");
            const string   WaitMessage  = "WaitActivity will wait for this trace";
            TestSequence   testSequence = new TestSequence()
            {
                Variables  = { value },
                Activities =
                {
                    new TestWriteLine()
                    {
                        Message = "Workflow Started"
                    },
                    new TestWaitForTrace()
                    {
                        DisplayName = "WaitActivity",
                        TraceToWait = WaitMessage
                    },
                    new TestWaitReadLine <int>("Read", "Read")
                    {
                        BookmarkValue = value
                    },
                    new TestReadLine <int>("Read1", "Read1")
                    {
                        BookmarkValue = value
                    },
                    new TestWriteLine()
                    {
                        Message = "Workflow Completed"
                    }
                }
            };

            JsonFileInstanceStore.FileInstanceStore jsonStore = new JsonFileInstanceStore.FileInstanceStore(".\\~");
            TestWorkflowRuntime workflowRuntime = TestRuntime.CreateTestWorkflowRuntime(testSequence, null, jsonStore, PersistableIdleAction.Unload);

            workflowRuntime.ExecuteWorkflow();
            workflowRuntime.WaitForActivityStatusChange("WaitActivity", TestActivityInstanceState.Executing);

            TestWorkflowRuntimeAsyncResult asyncResult = workflowRuntime.BeginResumeBookMark("Read", 10, new AsyncCallback(ResumeBookmarkCallback), operationId);

            //Continue the WaitActivity
            TestTraceManager.Instance.AddTrace(workflowRuntime.CurrentWorkflowInstanceId, new SynchronizeTrace(WaitMessage));
            SynchronizeTrace.Trace(workflowRuntime.CurrentWorkflowInstanceId, WaitMessage);
            workflowRuntime.EndResumeBookMark(asyncResult);
            workflowRuntime.WaitForTrace(new UserTrace("After ResumeBookmarkCallback"));

            if (operationId == 2)
            {
                //Do nothing
            }
            else if (operationId == 3)
            {
                workflowRuntime.UnloadWorkflow();
                workflowRuntime.LoadWorkflow();
                workflowRuntime.ExecuteWorkflow();
                workflowRuntime.ResumeBookMark("Read1", 99);
            }
            else if (operationId == 4)
            {
                workflowRuntime.LoadWorkflow();
                workflowRuntime.ExecuteWorkflow();
                workflowRuntime.ResumeBookMark("Read1", 99);
            }

            ExpectedTrace expectedTrace = testSequence.GetExpectedTrace();

            expectedTrace.AddIgnoreTypes(typeof(UserTrace));
            workflowRuntime.WaitForCompletion(expectedTrace);
        }
        public void PersistParallelForEach()
        {
            DelegateInArgument <string> _currentVariable = new DelegateInArgument <string>()
            {
                Name = "_currentVariable"
            };

            TestParallelForEach <string> parallelForEach = new TestParallelForEach <string>("Parallel for each")
            {
                HintValues       = new string[] { "Hi", "There" },
                ValuesExpression = (context => new string[] { "Hi", "There" }),
                CurrentVariable  = _currentVariable,
                Body             = new TestSequence("Sequence")
                {
                    Activities =
                    {
                        new TestWriteLine("Writeline")
                        {
                            Message = "Hi"
                        },

                        new TestSequence("inner seq")
                        {
                            Activities =
                            {
                                new TestIf(HintThenOrElse.Else, HintThenOrElse.Then)
                                {
                                    DisplayName         = "Test if",
                                    ConditionExpression = (env) => (bool)(_currentVariable.Get(env).Equals("Hi")),
                                    ThenActivity        = new TestBlockingActivity("Block Hi"),
                                    ElseActivity        = new TestBlockingActivity("Block There")
                                }
                            }
                        },

                        new TestWriteLine("Writeline act")
                        {
                            Message = "After blocking activty"
                        },
                    }
                }
            };

            JsonFileInstanceStore.FileInstanceStore jsonStore = new JsonFileInstanceStore.FileInstanceStore(".\\~");

            using (TestWorkflowRuntime testWorkflowRuntime = TestRuntime.CreateTestWorkflowRuntime(parallelForEach, null, jsonStore, PersistableIdleAction.None))
            {
                testWorkflowRuntime.ExecuteWorkflow();
                testWorkflowRuntime.WaitForActivityStatusChange("Block Hi", TestActivityInstanceState.Executing);
                testWorkflowRuntime.PersistWorkflow();
                System.Threading.Thread.Sleep(2000);
                testWorkflowRuntime.ResumeBookMark("Block Hi", null);


                testWorkflowRuntime.WaitForActivityStatusChange("Block There", TestActivityInstanceState.Executing);
                testWorkflowRuntime.PersistWorkflow();
                System.Threading.Thread.Sleep(2000);
                testWorkflowRuntime.ResumeBookMark("Block There", null);

                testWorkflowRuntime.WaitForCompletion();
            }
        }