Ejemplo n.º 1
0
        public async Task RestartOrchestrationWithExternalEvents()
        {
            using TestOrchestrationHost host = TestHelpers.GetTestOrchestrationHost(enableExtendedSessions: false);
            await host.StartAsync();

            // This is the local method we'll use to continuously recreate the same instance.
            // It doesn't matter that the orchestration name is different as long as the instance ID is the same.
            Task <TestInstance <string> > CreateInstance(int i) => host.StartInlineOrchestration(
                input: "Hello, world!",
                instanceId: "Singleton",
                orchestrationName: $"EchoOrchestration{i}",
                implementation: (ctx, input) => Task.FromResult(input));

            // Start the first iteration and wait for it to complete.
            TestInstance <string> instance = await CreateInstance(0);

            await instance.WaitForCompletion();

            int restartIterations  = 10;
            int externalEventCount = 10;

            // We'll keep track of the execution IDs to make 100% sure we get new instances every iteration.
            var executionIds = new HashSet <string> {
                instance.ExecutionId
            };

            // Simultaneously send a bunch of external events as well as a "recreate" message. Repeat this
            // several times. Need to ensure that the orchestration can always be restarted reliably. It
            // doesn't matter whether the orchestration handles the external events or not.
            for (int i = 1; i <= restartIterations; i++)
            {
                List <Task> concurrentTasks = Enumerable.Range(0, externalEventCount).Select(j => instance.RaiseEventAsync("DummyEvent", j)).ToList();
                Task <TestInstance <string> > recreateInstanceTask = CreateInstance(i);
                concurrentTasks.Add(recreateInstanceTask);
                await Task.WhenAll(concurrentTasks);

                // Wait for the newly created instance to complete.
                instance = await recreateInstanceTask;
                await instance.WaitForCompletion();

                // Ensure that this is a new execution ID.
                Assert.IsTrue(executionIds.Add(instance.ExecutionId));
            }
        }