Ejemplo n.º 1
0
        public async Task TestWorkflowStoreInterfaceSyncState <TStore>(WorkflowEngine <TStore> engine)
            where TStore : IWorkflowStore
        {
            var storeWorkflow = new SequentialWorkflow(
                async instance =>
            {
                var store = WorkflowStore.TryGetStore(instance);

                var argCounter         = instance.GetArgument <int>("Counter");
                var argInternalCounter = instance.GetArgument <int>("InternalCounter");

                var parallelWorkflow = new ParallelWorkflow();

                parallelWorkflow.Add(
                    Workflow.Create(
                        async internalInstance =>
                {
                    // only one sync state should make it through for outer instance
                    if (!await store.SyncState("SyncIncrement", instance))
                    {
                        argCounter.Value += 1;
                    }

                    // both parallel workflows should be able to enter their internal instance sync state
                    if (!await store.SyncState("SyncIncrement", internalInstance))
                    {
                        argInternalCounter.Value += 1;
                    }
                }));

                parallelWorkflow.Add(
                    Workflow.Create(
                        async internalInstance =>
                {
                    // only one sync state should make it through for outer instance
                    if (!await store.SyncState("SyncIncrement", instance))
                    {
                        argCounter.Value += 1;
                    }

                    // both parallel workflows should be able to enter their internal instance sync state
                    if (!await store.SyncState("SyncIncrement", internalInstance))
                    {
                        argInternalCounter.Value += 1;
                    }
                }));

                await instance.EnterWorkflow(parallelWorkflow);
            });

            var workflowInstance = engine.CreateWorkflow(storeWorkflow);

            await workflowInstance.Start(new Argument <int>("Counter"), new Argument <int>("InternalCounter"));

            await workflowInstance.Wait();

            Assert.AreEqual(1, workflowInstance.GetArgument <int>("Counter").Value);
            Assert.AreEqual(2, workflowInstance.GetArgument <int>("InternalCounter").Value);
        }
Ejemplo n.º 2
0
        public async Task UnitTestParallelWorkflow()
        {
            using (var engine = new WorkflowEngine())
            {
                var parallelWorkflow = new ParallelWorkflow();

                // test parallel sequential workflow
                parallelWorkflow.Add(
                    new SequentialWorkflow(
                        async instance =>
                {
                    // test parent id set correct
                    instance.GetArgument <Guid>("ParentWorkflowId").Value = instance.ParentId;

                    // test enter workflow using outer instance
                    await
                    instance.EnterWorkflow(
                        Workflow.Create(
                            internalInstance => instance.GetArgument <int>("Counter").Lock(a => a.Value += 1)));
                    instance.GetArgument <int>("Counter").Lock(a => a.Value += 1);
                }));

                // test parallel create workflow
                parallelWorkflow.Add(
                    Workflow.Create(instance => instance.GetArgument <int>("Counter").Lock(a => a.Value += 1)));

                // test parallel delay workflow
                parallelWorkflow.Add(
                    Workflow.Create(
                        async instance => await instance.EnterWorkflow(
                            new DelayWorkflow(
                                Workflow.Create(
                                    internalInstance =>
                                    internalInstance.GetArgument <int>("Counter").Lock(a => a.Value += 1)),
                                UnitTestWorkflows.DefaultWaitTime))));

                var argCounter = new Argument <int>("Counter");

                var parallelInstance = engine.CreateWorkflow(parallelWorkflow);
                await parallelInstance.Start(
                    argCounter,
                    new Argument <Guid>("ParentWorkflowId"));

                await parallelInstance.Wait();

                Assert.AreEqual(argCounter.Value, parallelInstance.GetArgument <int>("Counter").Value);
                Assert.AreEqual(4, parallelInstance.GetArgument <int>("Counter").Value);
                Assert.AreEqual(parallelInstance.Id, parallelInstance.GetArgument <Guid>("ParentWorkflowId").Value);
            }
        }