public async void Unbounded_2_Consumers()
        {
            var push = new ReplayAsyncEnumerable <int>();

            var en1 = push.GetAsyncEnumerator();
            var en2 = push.GetAsyncEnumerator();

            var task1 = Task.Run(async() =>
            {
                await en1.AssertResult(1, 2, 3, 4, 5);
            });
            var task2 = Task.Run(async() =>
            {
                await en2.AssertResult(1, 2, 3, 4, 5);
            });

            for (var i = 1; i <= 5; i++)
            {
                await push.Next(i);
            }
            await push.Complete();

            await task1;
            await task2;
        }
        public async Task PushToLimitedListWithFunc()
        {
            var asyncEnum = new ReplayAsyncEnumerable <int>();
            var list      = new List <int>();
            var max       = 5;
            var task      = asyncEnum.PushToList(list, () => max).LastAsync();

            Assert.Empty(list);

            for (int i = 0; i < 7; i++)
            {
                await asyncEnum.Next(i);
            }

            await Task.Delay(500);

            Assert.Equal(5, list.Count);

            max = 2;

            for (int i = 0; i < 4; i++)
            {
                await asyncEnum.Next(i);
            }

            await asyncEnum.Complete();

            await task;

            Assert.Equal(2, list.Count);
        }
        public async void TimedSized_2_Consumers_Error()
        {
            var push = new ReplayAsyncEnumerable <int>(10, TimeSpan.FromHours(1));

            var en1 = push.GetAsyncEnumerator();
            var en2 = push.GetAsyncEnumerator();

            var task1 = Task.Run(async() =>
            {
                await en1.AssertFailure(typeof(InvalidOperationException), 1, 2, 3, 4, 5);
            });
            var task2 = Task.Run(async() =>
            {
                await en2.AssertFailure(typeof(InvalidOperationException), 1, 2, 3, 4, 5);
            });

            for (var i = 1; i <= 5; i++)
            {
                await push.Next(i);
            }
            await push.Error(new InvalidOperationException());

            await task1;
            await task2;
        }
Beispiel #4
0
        public async void Unbounded_No_Consumers()
        {
            var push = new ReplayAsyncEnumerable <int>();

            for (var i = 1; i <= 5; i++)
            {
                await push.Next(i);
            }
            await push.Complete();

            await push.AssertResult(1, 2, 3, 4, 5);

            await push.AssertResult(1, 2, 3, 4, 5);
        }
Beispiel #5
0
        public async void Unbounded_No_Consumers_Error()
        {
            var push = new ReplayAsyncEnumerable <int>();

            for (var i = 1; i <= 5; i++)
            {
                await push.Next(i);
            }
            await push.Error(new InvalidOperationException());

            await push.AssertFailure(typeof(InvalidOperationException), 1, 2, 3, 4, 5);

            await push.AssertFailure(typeof(InvalidOperationException), 1, 2, 3, 4, 5);
        }
        public async void TimedSized_No_Consumers_Error()
        {
            var push = new ReplayAsyncEnumerable <int>(10, TimeSpan.FromHours(1));

            for (var i = 1; i <= 5; i++)
            {
                await push.Next(i);
            }
            await push.Error(new InvalidOperationException());

            await push.AssertFailure(typeof(InvalidOperationException), 1, 2, 3, 4, 5);

            await push.AssertFailure(typeof(InvalidOperationException), 1, 2, 3, 4, 5);
        }
        public async void TimedSized_No_Consumers()
        {
            var push = new ReplayAsyncEnumerable <int>(10, TimeSpan.FromHours(1));

            for (var i = 1; i <= 5; i++)
            {
                await push.Next(i);
            }
            await push.Complete();

            await push.AssertResult(1, 2, 3, 4, 5);

            await push.AssertResult(1, 2, 3, 4, 5);
        }
        public async void HasConsumers()
        {
            var push = new ReplayAsyncEnumerable <int>();

            Assert.False(push.HasConsumers);

            var en = push.GetAsyncEnumerator();

            Assert.True(push.HasConsumers);

            await en.DisposeAsync();

            Assert.False(push.HasConsumers);
        }
        /// <summary>
        /// Source stream will be cached for multiple enumerations of later consumers
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="maxSize">max Size of Queue, otherwise unbounded</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static IAsyncEnumerable <T> ToReplayQueue <T>(this IAsyncEnumerable <T> source, int maxSize,
                                                             CancellationToken cancellationToken = default)
        {
            return(ToReplayQueueImpl().HandleInDispatcher(cancellationToken));

            IAsyncEnumerable <T> ToReplayQueueImpl()
            {
                var queue = new ReplayAsyncEnumerable <T>(maxSize);

                FillQueue(source, queue, cancellationToken);

                return(queue);
            }
        }
        public async void Unbounded_Take()
        {
            var push = new ReplayAsyncEnumerable <int>();

            var t1 = push.Take(1).AssertResult(1);

            var t2 = push.Take(2).AssertResult(1, 2);

            await push.Next(1);

            await push.Next(2);

            await t1;

            await t2;
        }
        public async void SizedTimed_Long()
        {
            const long n    = 1_000_000;
            var        push = new ReplayAsyncEnumerable <long>(10, TimeSpan.FromHours(1));
            var        t    = push
                              .Reduce((a, b) => a + b)
                              .AssertResult(n * (n + 1) / 2);

            for (var i = 1; i <= n; i++)
            {
                await push.Next(i);
            }

            await push.Complete();

            await t;
        }
        public IAsyncEnumerator <TResult> GetAsyncEnumerator()
        {
            var subject = new ReplayAsyncEnumerable <TSource>();
            IAsyncEnumerable <TResult> result;

            try
            {
                result = _func(subject);
            }
            catch (Exception ex)
            {
                return(new Error <TResult> .ErrorEnumerator(ex));
            }
            var en = new MulticastEnumerator <TSource, TResult>(_source.GetAsyncEnumerator(), subject, result.GetAsyncEnumerator());

            return(en);
        }
        public IAsyncEnumerator <TResult> GetAsyncEnumerator(CancellationToken cancellationToken)
        {
            var subject = new ReplayAsyncEnumerable <TSource>();
            IAsyncEnumerable <TResult> result;

            try
            {
                result = _func(subject);
            }
            catch (Exception ex)
            {
                return(new Error <TResult> .ErrorEnumerator(ex));
            }
            var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            var en  = new MulticastEnumerator <TSource, TResult>(_source.GetAsyncEnumerator(cts.Token), subject, result.GetAsyncEnumerator(cancellationToken), cts);

            return(en);
        }
        public async void Unbounded_One_Consumer_Error()
        {
            var push = new ReplayAsyncEnumerable <int>();

            var en1 = push.GetAsyncEnumerator();

            var task1 = Task.Run(async() =>
            {
                await en1.AssertFailure(typeof(InvalidOperationException), 1, 2, 3, 4, 5);
            });

            for (var i = 1; i <= 5; i++)
            {
                await push.Next(i);
            }
            await push.Error(new InvalidOperationException());

            await task1;
        }
        public async void TimedSized_One_Consumer()
        {
            var push = new ReplayAsyncEnumerable <int>(10, TimeSpan.FromHours(1));

            var en1 = push.GetAsyncEnumerator();

            var task1 = Task.Run(async() =>
            {
                await en1.AssertResult(1, 2, 3, 4, 5);
            });

            for (var i = 1; i <= 5; i++)
            {
                await push.Next(i);
            }
            await push.Complete();

            await task1;
        }
        public async void TimedSized_Bounded_1_Normal()
        {
            var push = new ReplayAsyncEnumerable <int>(1, TimeSpan.FromHours(1));

            await push.Next(1);

            await push.Next(2);

            var t1 = push.AssertResult(2, 3, 4, 5);

            await push.Next(3);

            await push.Next(4);

            await push.Next(5);

            await push.Complete();

            await t1;

            await push.AssertResult(5);
        }
        public async void TimedSized_Long_Halfway()
        {
            const long n    = 1_000_000;
            var        push = new ReplayAsyncEnumerable <long>((int)n, TimeSpan.FromHours(1));
            var        t    = default(ValueTask);

            for (var i = 1; i <= n; i++)
            {
                await push.Next(i);

                if (i * 2 == n)
                {
                    t = push
                        .Reduce((a, b) => a + b)
                        .AssertResult(n * (n + 1) / 2);
                }
            }

            await push.Complete();

            await t;
        }
        public async void Sized_Bounded_2_Normal()
        {
            var push = new ReplayAsyncEnumerable <int>(2);

            await push.Next(1);

            await push.Next(2);

            await push.Next(3);

            var t1 = push.AssertResult(2, 3, 4, 5);

            await push.Next(4);

            await push.Next(5);

            await push.Complete();

            await t1;

            await push.AssertResult(4, 5);
        }
        public async void Timed_Bounded_1_Normal()
        {
            var timeNow = new [] { 0L };

            var push = new ReplayAsyncEnumerable <int>(10, TimeSpan.FromMilliseconds(100), () => timeNow[0]);

            await push.Next(1);

            timeNow[0] += 100;

            await push.Next(2);

            var t1 = push.AssertResult(2, 3, 4, 5);

            timeNow[0] += 100;

            await push.Next(3);

            timeNow[0] += 100;

            await push.Next(4);

            timeNow[0] += 100;

            await push.Next(5);

            await push.Complete();

            await t1;

            await push.AssertResult(5);

            timeNow[0] += 100;

            await push.AssertResult();
        }
Beispiel #20
0
        public async void Unbounded_One_Consumer()
        {
            var push = new ReplayAsyncEnumerable <int>();

            var en1 = push.GetAsyncEnumerator(default);