Răsfoiți Sursa

Moving non-standard Concat overloads to AsyncEnumerableEx.

Bart De Smet 8 ani în urmă
părinte
comite
8c0f8ae39d

+ 95 - 0
Ix.NET/Source/System.Interactive.Async.Tests/System/Linq/Operators/Concat.cs

@@ -0,0 +1,95 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the Apache 2.0 License.
+// See the LICENSE file in the project root for more information. 
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Xunit;
+
+namespace Tests
+{
+    public class Concat : AsyncEnumerableExTests
+    {
+        [Fact]
+        public void Concat_Null()
+        {
+            AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Concat<int>(default(IAsyncEnumerable<int>[])));
+            AssertThrows<ArgumentNullException>(() => AsyncEnumerableEx.Concat<int>(default(IEnumerable<IAsyncEnumerable<int>>)));
+        }
+
+        [Fact]
+        public void Concat4()
+        {
+            var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
+            var ys = new[] { 4, 5 }.ToAsyncEnumerable();
+            var zs = new[] { 6, 7, 8 }.ToAsyncEnumerable();
+
+            var res = AsyncEnumerableEx.Concat(xs, ys, zs);
+
+            var e = res.GetAsyncEnumerator();
+            HasNext(e, 1);
+            HasNext(e, 2);
+            HasNext(e, 3);
+            HasNext(e, 4);
+            HasNext(e, 5);
+            HasNext(e, 6);
+            HasNext(e, 7);
+            HasNext(e, 8);
+            NoNext(e);
+        }
+
+        [Fact]
+        public void Concat5()
+        {
+            var ex = new Exception("Bang");
+            var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
+            var ys = new[] { 4, 5 }.ToAsyncEnumerable();
+            var zs = Throw<int>(ex);
+
+            var res = AsyncEnumerableEx.Concat(xs, ys, zs);
+
+            var e = res.GetAsyncEnumerator();
+            HasNext(e, 1);
+            HasNext(e, 2);
+            HasNext(e, 3);
+            HasNext(e, 4);
+            HasNext(e, 5);
+            AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), SingleInnerExceptionMatches(ex));
+        }
+
+        [Fact]
+        public void Concat6()
+        {
+            var res = AsyncEnumerableEx.Concat(ConcatXss());
+
+            var e = res.GetAsyncEnumerator();
+            HasNext(e, 1);
+            HasNext(e, 2);
+            HasNext(e, 3);
+            HasNext(e, 4);
+            HasNext(e, 5);
+            AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!");
+        }
+
+        [Fact]
+        public async Task Concat9()
+        {
+            var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
+            var ys = new[] { 4, 5 }.ToAsyncEnumerable();
+            var zs = new[] { 6, 7, 8 }.ToAsyncEnumerable();
+
+            var res = AsyncEnumerableEx.Concat(xs, ys, zs);
+
+            await SequenceIdentity(res);
+        }
+
+        private static IEnumerable<IAsyncEnumerable<int>> ConcatXss()
+        {
+            yield return new[] { 1, 2, 3 }.ToAsyncEnumerable();
+            yield return new[] { 4, 5 }.ToAsyncEnumerable();
+            throw new Exception("Bang!");
+        }
+    }
+}

+ 124 - 0
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Concat.cs

@@ -0,0 +1,124 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the Apache 2.0 License.
+// See the LICENSE file in the project root for more information. 
+
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Threading.Tasks;
+
+namespace System.Linq
+{
+    public static partial class AsyncEnumerableEx
+    {
+        public static IAsyncEnumerable<TSource> Concat<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
+        {
+            if (sources == null)
+                throw new ArgumentNullException(nameof(sources));
+
+            return ConcatCore(sources);
+        }
+
+        public static IAsyncEnumerable<TSource> Concat<TSource>(params IAsyncEnumerable<TSource>[] sources)
+        {
+            if (sources == null)
+                throw new ArgumentNullException(nameof(sources));
+
+            return ConcatCore(sources);
+        }
+
+        private static IAsyncEnumerable<TSource> ConcatCore<TSource>(IEnumerable<IAsyncEnumerable<TSource>> sources)
+        {
+            return new ConcatEnumerableAsyncIterator<TSource>(sources);
+        }
+
+        private sealed class ConcatEnumerableAsyncIterator<TSource> : AsyncIterator<TSource>
+        {
+            private readonly IEnumerable<IAsyncEnumerable<TSource>> source;
+
+            public ConcatEnumerableAsyncIterator(IEnumerable<IAsyncEnumerable<TSource>> source)
+            {
+                Debug.Assert(source != null);
+
+                this.source = source;
+            }
+
+            public override AsyncIterator<TSource> Clone()
+            {
+                return new ConcatEnumerableAsyncIterator<TSource>(source);
+            }
+
+            public override async Task DisposeAsync()
+            {
+                if (outerEnumerator != null)
+                {
+                    outerEnumerator.Dispose();
+                    outerEnumerator = null;
+                }
+
+                if (currentEnumerator != null)
+                {
+                    await currentEnumerator.DisposeAsync().ConfigureAwait(false);
+                    currentEnumerator = null;
+                }
+
+                await base.DisposeAsync().ConfigureAwait(false);
+            }
+
+            // State machine vars
+            private IEnumerator<IAsyncEnumerable<TSource>> outerEnumerator;
+            private IAsyncEnumerator<TSource> currentEnumerator;
+            private int mode;
+
+            private const int State_OuterNext = 1;
+            private const int State_While = 4;
+
+            protected override async Task<bool> MoveNextCore()
+            {
+
+                switch (state)
+                {
+                    case AsyncIteratorState.Allocated:
+                        outerEnumerator = source.GetEnumerator();
+                        mode = State_OuterNext;
+                        state = AsyncIteratorState.Iterating;
+                        goto case AsyncIteratorState.Iterating;
+
+                    case AsyncIteratorState.Iterating:
+                        switch (mode)
+                        {
+                            case State_OuterNext:
+                                if (outerEnumerator.MoveNext())
+                                {
+                                    // make sure we dispose the previous one if we're about to replace it
+                                    if (currentEnumerator != null)
+                                    {
+                                        await currentEnumerator.DisposeAsync().ConfigureAwait(false);
+                                    }
+
+                                    currentEnumerator = outerEnumerator.Current.GetAsyncEnumerator();
+
+                                    mode = State_While;
+                                    goto case State_While;
+                                }
+
+                                break;
+                            case State_While:
+                                if (await currentEnumerator.MoveNextAsync().ConfigureAwait(false))
+                                {
+                                    current = currentEnumerator.Current;
+                                    return true;
+                                }
+
+                                // No more on the inner enumerator, move to the next outer
+                                goto case State_OuterNext;
+                        }
+
+                        await DisposeAsync().ConfigureAwait(false);
+                        break;
+                }
+
+                return false;
+            }
+        }
+    }
+}

+ 0 - 75
Ix.NET/Source/System.Linq.Async.Tests/System/Linq/Operators/Concat.cs

@@ -17,8 +17,6 @@ namespace Tests
         {
         {
             AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Concat<int>(default(IAsyncEnumerable<int>), Return42));
             AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Concat<int>(default(IAsyncEnumerable<int>), Return42));
             AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Concat<int>(Return42, default(IAsyncEnumerable<int>)));
             AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Concat<int>(Return42, default(IAsyncEnumerable<int>)));
-            AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Concat<int>(default(IAsyncEnumerable<int>[])));
-            AssertThrows<ArgumentNullException>(() => AsyncEnumerable.Concat<int>(default(IEnumerable<IAsyncEnumerable<int>>)));
         }
         }
 
 
         [Fact]
         [Fact]
@@ -59,60 +57,6 @@ namespace Tests
             AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), SingleInnerExceptionMatches(ex));
             AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), SingleInnerExceptionMatches(ex));
         }
         }
 
 
-        [Fact]
-        public void Concat4()
-        {
-            var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
-            var ys = new[] { 4, 5 }.ToAsyncEnumerable();
-            var zs = new[] { 6, 7, 8 }.ToAsyncEnumerable();
-
-            var res = AsyncEnumerable.Concat(xs, ys, zs);
-
-            var e = res.GetAsyncEnumerator();
-            HasNext(e, 1);
-            HasNext(e, 2);
-            HasNext(e, 3);
-            HasNext(e, 4);
-            HasNext(e, 5);
-            HasNext(e, 6);
-            HasNext(e, 7);
-            HasNext(e, 8);
-            NoNext(e);
-        }
-
-        [Fact]
-        public void Concat5()
-        {
-            var ex = new Exception("Bang");
-            var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
-            var ys = new[] { 4, 5 }.ToAsyncEnumerable();
-            var zs = Throw<int>(ex);
-
-            var res = AsyncEnumerable.Concat(xs, ys, zs);
-
-            var e = res.GetAsyncEnumerator();
-            HasNext(e, 1);
-            HasNext(e, 2);
-            HasNext(e, 3);
-            HasNext(e, 4);
-            HasNext(e, 5);
-            AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), SingleInnerExceptionMatches(ex));
-        }
-
-        [Fact]
-        public void Concat6()
-        {
-            var res = AsyncEnumerable.Concat(ConcatXss());
-
-            var e = res.GetAsyncEnumerator();
-            HasNext(e, 1);
-            HasNext(e, 2);
-            HasNext(e, 3);
-            HasNext(e, 4);
-            HasNext(e, 5);
-            AssertThrows(() => e.MoveNextAsync().Wait(WaitTimeoutMs), (Exception ex_) => ((AggregateException)ex_).Flatten().InnerExceptions.Single().Message == "Bang!");
-        }
-
         [Fact]
         [Fact]
         public void Concat7()
         public void Concat7()
         {
         {
@@ -151,18 +95,6 @@ namespace Tests
             await SequenceIdentity(res);
             await SequenceIdentity(res);
         }
         }
 
 
-        [Fact]
-        public async Task Concat9()
-        {
-            var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable();
-            var ys = new[] { 4, 5 }.ToAsyncEnumerable();
-            var zs = new[] { 6, 7, 8 }.ToAsyncEnumerable();
-
-            var res = AsyncEnumerable.Concat(xs, ys, zs);
-
-            await SequenceIdentity(res);
-        }
-
         [Fact]
         [Fact]
         public async Task Concat10()
         public async Task Concat10()
         {
         {
@@ -200,12 +132,5 @@ namespace Tests
 
 
             Assert.Equal(8, await c.Count());
             Assert.Equal(8, await c.Count());
         }
         }
-
-        static IEnumerable<IAsyncEnumerable<int>> ConcatXss()
-        {
-            yield return new[] { 1, 2, 3 }.ToAsyncEnumerable();
-            yield return new[] { 4, 5 }.ToAsyncEnumerable();
-            throw new Exception("Bang!");
-        }
     }
     }
 }
 }

+ 0 - 111
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Concat.cs

@@ -24,117 +24,6 @@ namespace System.Linq
                        new Concat2AsyncIterator<TSource>(first, second);
                        new Concat2AsyncIterator<TSource>(first, second);
         }
         }
 
 
-        public static IAsyncEnumerable<TSource> Concat<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
-        {
-            if (sources == null)
-                throw new ArgumentNullException(nameof(sources));
-
-            return sources.Concat_();
-        }
-
-        public static IAsyncEnumerable<TSource> Concat<TSource>(params IAsyncEnumerable<TSource>[] sources)
-        {
-            if (sources == null)
-                throw new ArgumentNullException(nameof(sources));
-
-            return sources.Concat_();
-        }
-
-        private static IAsyncEnumerable<TSource> Concat_<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
-        {
-            return new ConcatEnumerableAsyncIterator<TSource>(sources);
-        }
-
-        private sealed class ConcatEnumerableAsyncIterator<TSource> : AsyncIterator<TSource>
-        {
-            private readonly IEnumerable<IAsyncEnumerable<TSource>> source;
-
-            public ConcatEnumerableAsyncIterator(IEnumerable<IAsyncEnumerable<TSource>> source)
-            {
-                Debug.Assert(source != null);
-
-                this.source = source;
-            }
-
-            public override AsyncIterator<TSource> Clone()
-            {
-                return new ConcatEnumerableAsyncIterator<TSource>(source);
-            }
-
-            public override async Task DisposeAsync()
-            {
-                if (outerEnumerator != null)
-                {
-                    outerEnumerator.Dispose();
-                    outerEnumerator = null;
-                }
-
-                if (currentEnumerator != null)
-                {
-                    await currentEnumerator.DisposeAsync().ConfigureAwait(false);
-                    currentEnumerator = null;
-                }
-
-                await base.DisposeAsync().ConfigureAwait(false);
-            }
-
-            // State machine vars
-            private IEnumerator<IAsyncEnumerable<TSource>> outerEnumerator;
-            private IAsyncEnumerator<TSource> currentEnumerator;
-            private int mode;
-
-            private const int State_OuterNext = 1;
-            private const int State_While = 4;
-
-            protected override async Task<bool> MoveNextCore()
-            {
-
-                switch (state)
-                {
-                    case AsyncIteratorState.Allocated:
-                        outerEnumerator = source.GetEnumerator();
-                        mode = State_OuterNext;
-                        state = AsyncIteratorState.Iterating;
-                        goto case AsyncIteratorState.Iterating;
-
-                    case AsyncIteratorState.Iterating:
-                        switch (mode)
-                        {
-                            case State_OuterNext:
-                                if (outerEnumerator.MoveNext())
-                                {
-                                    // make sure we dispose the previous one if we're about to replace it
-                                    if (currentEnumerator != null)
-                                    {
-                                        await currentEnumerator.DisposeAsync().ConfigureAwait(false);
-                                    }
-
-                                    currentEnumerator = outerEnumerator.Current.GetAsyncEnumerator();
-
-                                    mode = State_While;
-                                    goto case State_While;
-                                }
-
-                                break;
-                            case State_While:
-                                if (await currentEnumerator.MoveNextAsync().ConfigureAwait(false))
-                                {
-                                    current = currentEnumerator.Current;
-                                    return true;
-                                }
-
-                                // No more on the inner enumerator, move to the next outer
-                                goto case State_OuterNext;
-                        }
-
-                        await DisposeAsync().ConfigureAwait(false);
-                        break;
-                }
-
-                return false;
-            }
-        }
-
         private sealed class Concat2AsyncIterator<TSource> : ConcatAsyncIterator<TSource>
         private sealed class Concat2AsyncIterator<TSource> : ConcatAsyncIterator<TSource>
         {
         {
             private readonly IAsyncEnumerable<TSource> first;
             private readonly IAsyncEnumerable<TSource> first;