Пример #1
0
        public void QueryAsync_WhenSubscribedForWrongReturnType_ShouldThrowInvalidCastException()
        {
            var aggregator = InitAggregator();

            aggregator.SubscribeForAsyncResult <TestMessage>(async handler =>
            {
                await Task.Delay(10).ConfigureAwait(false);
                return("Test");
            });

            AggregateExceptionHelper.AssertInnerException <InvalidCastException>(aggregator.QueryAsync <TestMessage, List <string> >(new TestMessage()));
        }
Пример #2
0
        public void QueryAsync_WhenExceptionInHandler_ShouldCatchIt()
        {
            var aggregator = InitAggregator();

            aggregator.SubscribeForAsyncResult <TestMessage>(async handler =>
            {
                await Task.Delay(10).ConfigureAwait(false);
                throw new TestException();
            });

            AggregateExceptionHelper.AssertInnerException <TestException>(aggregator.QueryAsync <TestMessage, string>(new TestMessage()));
        }
Пример #3
0
        public void QueryWithResults_WhenCanceled_ShouldThrowOperationCanceledException()
        {
            var aggregator = InitAggregator();
            var expected   = new List <string> {
                "Test", "Test2"
            };

            aggregator.SubscribeForAsyncResult <TestMessage>(async message =>
            {
                message.CancellationToken.ThrowIfCancellationRequested();
                await Task.Delay(100).ConfigureAwait(false);
                return(expected[1]);
            });

            var ts = new CancellationTokenSource();

            ts.Cancel();

            var subscription = aggregator.QueryWithResults <TestMessage, string>(new TestMessage(), cancellationToken: ts.Token);

            AggregateExceptionHelper.AssertInnerException <TaskCanceledException>(subscription);
        }
Пример #4
0
        public static async Task Run<TValue>(Func<TValue, int, Task> actionOnValueN, IEnumerable<TValue> values, int maxConcurrency)
        {
            var currentIndex = 1;

            var exceptions = new List<Exception>();

            using (var maxConcurrencySemaphor = new SemaphoreSlim(maxConcurrency))
            {
                var tasks = new List<Task>();

                foreach (var value in values)
                {
                    maxConcurrencySemaphor.Wait();

                    var task = Task.Run(async () =>
                    {
                        try
                        {
                            await actionOnValueN(value, currentIndex++);
                        }
                        catch(Exception ex)
                        {
                            exceptions.Add(ex);
                        }
                        finally
                        {
                            maxConcurrencySemaphor.Release();
                        }
                    });

                    tasks.Add(task);
                }

                await Task.WhenAll(tasks);
            }

            AggregateExceptionHelper.ThrowIfAny(exceptions);
        }
Пример #5
0
        public void QueryWithResults_WhenLongRun_ShouldTimeOut()
        {
            var aggregator = InitAggregator();
            var expected   = new List <string> {
                "Test", "Test2"
            };

            aggregator.SubscribeForAsyncResult <TestMessage>(async handler =>
            {
                await Task.Delay(100).ConfigureAwait(false);
                return(expected[1]);
            });

            aggregator.SubscribeForAsyncResult <TestMessage>(async handler =>
            {
                await Task.Delay(100).ConfigureAwait(false);
                return(expected[0]);
            });

            var subscription = aggregator.QueryWithResults <TestMessage, string>(new TestMessage(), behaviors: new BehaviorChain().WithTimeout(TimeSpan.FromMilliseconds(10)));

            AggregateExceptionHelper.AssertInnerException <TimeoutException>(subscription);
        }