예제 #1
0
        public async Task Orchestration_With_Same_Id_Cant_Be_Started_While_Running()
        {
            var instanceId = nameof(Orchestration_With_Same_Id_Cant_Be_Started_While_Running);
            var testData   = new TestOrchestrationData()
            {
                NumberOfParallelTasks = 0,
                NumberOfSerialTasks   = 1,
                MaxDelay  = 5,
                MinDelay  = 5,
                DelayUnit = TimeSpan.FromSeconds(1),
            };

            Func <Task <Tuple <OrchestrationInstance, Exception> > > startFunc = async() =>
            {
                OrchestrationInstance instance  = null;
                Exception             exception = null;

                try
                {
                    instance = await this.taskHubClient.CreateOrchestrationInstanceAsync(typeof(TestOrchestration), instanceId, testData);
                }
                catch (Exception e)
                {
                    while (e is AggregateException)
                    {
                        e = e.InnerException;
                    }

                    exception = e;
                }

                return(Tuple.Create(instance, exception));
            };

            var allResults = await Task.WhenAll(startFunc(), startFunc(), startFunc(), startFunc());

            OrchestrationInstance createdInstance = null;

            for (int i = 0; i < allResults.Length; i++)
            {
                var result = allResults[i];
                if (result.Item1 != null)
                {
                    if (createdInstance != null)
                    {
                        Assert.Fail($"Multiple orchestrations were started with the instance id {instanceId} at the same time");
                    }
                    else
                    {
                        createdInstance = result.Item1;
                    }
                }
                else
                {
                    Assert.IsInstanceOfType(result.Item2, typeof(Exception),
                                            $"Exception Type Check Failed : Task {i} returned an unexpected exception {result.Item2}");
                }
            }

            Assert.IsNotNull(createdInstance, $"No task was able to create an orchestration with the given instance id {instanceId}");
            var orchestrationResult = await this.taskHubClient.WaitForOrchestrationAsync(createdInstance, TimeSpan.FromMinutes(2));

            Assert.AreEqual(OrchestrationStatus.Completed, orchestrationResult.OrchestrationStatus);
        }
예제 #2
0
        async Task RunTestOrchestrationsHelper(int numberOfInstances, TestOrchestrationData orchestrationInput, Func <int, TimeSpan> delayGeneratorFunction = null)
        {
            var taskHubClient = Utilities.CreateTaskHubClient();

            List <Tuple <OrchestrationInstance, OrchestrationState> > results = new List <Tuple <OrchestrationInstance, OrchestrationState> >();
            List <Task> waitTasks = new List <Task>();
            Stopwatch   stopWatch = Stopwatch.StartNew();

            for (int i = 0; i < numberOfInstances; i++)
            {
                var instance = await taskHubClient.CreateOrchestrationInstanceAsync(typeof(TestOrchestration), orchestrationInput);

                waitTasks.Add(Task.Run(async() =>
                {
                    var state = await taskHubClient.WaitForOrchestrationAsync(instance, TimeSpan.FromMinutes(2));
                    lock (results)
                    {
                        results.Add(Tuple.Create(instance, state));
                    }
                }));
                if (delayGeneratorFunction != null)
                {
                    await Task.Delay(delayGeneratorFunction(i));
                }
            }

            await Task.WhenAll(waitTasks);

            stopWatch.Stop();

            Func <TimeSpan, string> elapsedTimeFormatter = timeSpan => $"{timeSpan.Hours:00}:{timeSpan.Minutes:00}:{timeSpan.Seconds:00}.{timeSpan.Milliseconds / 10:00}";

            Console.WriteLine($"Total Meastured Time For All Orchestrations: {elapsedTimeFormatter(stopWatch.Elapsed)}");

            var      expectedResult = (orchestrationInput.NumberOfParallelTasks + orchestrationInput.NumberOfSerialTasks).ToString();
            TimeSpan minTime        = TimeSpan.MaxValue;
            TimeSpan maxTime        = TimeSpan.FromMilliseconds(0);

            int failedOrchestrations = 0;

            foreach (var kvp in results)
            {
                if (kvp.Item2 == null)
                {
                    failedOrchestrations++;
                    var state = await taskHubClient.GetOrchestrationStateAsync(kvp.Item1);

                    Console.WriteLine($"Unfinished orchestration {kvp.Item1}, state : {state?.OrchestrationStatus}");
                    continue;
                }

                Assert.AreEqual(expectedResult, kvp.Item2.Output, $"Unexpected output for Orchestration : {kvp.Item1.InstanceId}");
                TimeSpan orchestrationTime = kvp.Item2.CompletedTime - kvp.Item2.CreatedTime;
                if (minTime > orchestrationTime)
                {
                    minTime = orchestrationTime;
                }
                if (maxTime < orchestrationTime)
                {
                    maxTime = orchestrationTime;
                }
            }

            Console.WriteLine($"Minimum time taken for any orchestration instance : {elapsedTimeFormatter(minTime)}");
            Console.WriteLine($"Maximum time taken for any orchestration instance : {elapsedTimeFormatter(maxTime)}");

            Assert.AreEqual(0, failedOrchestrations, $"{failedOrchestrations} orchestrations failed out of {numberOfInstances}.");
        }