ToAsyncEnumerable.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Xunit;
  9. namespace Tests
  10. {
  11. public class ToAsyncEnumerable : AsyncEnumerableTests
  12. {
  13. [Fact]
  14. public void ToAsyncEnumerable_Null()
  15. {
  16. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.ToAsyncEnumerable(default(IEnumerable<int>)));
  17. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.ToAsyncEnumerable(default(IObservable<int>)));
  18. Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.ToAsyncEnumerable(default(Task<int>)));
  19. }
  20. [Fact]
  21. public async Task ToAsyncEnumerable1Async()
  22. {
  23. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  24. var e = xs.GetAsyncEnumerator();
  25. await HasNextAsync(e, 1);
  26. await HasNextAsync(e, 2);
  27. await HasNextAsync(e, 3);
  28. await HasNextAsync(e, 4);
  29. await NoNextAsync(e);
  30. }
  31. [Fact]
  32. public async Task ToAsyncEnumerable2Async()
  33. {
  34. var ex = new Exception("Bang");
  35. var xs = ToAsyncEnumerable_Sequence(ex).ToAsyncEnumerable();
  36. var e = xs.GetAsyncEnumerator();
  37. await HasNextAsync(e, 42);
  38. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  39. }
  40. private IEnumerable<int> ToAsyncEnumerable_Sequence(Exception e)
  41. {
  42. yield return 42;
  43. throw e;
  44. }
  45. [Fact]
  46. public async Task ToAsyncEnumerable3Async()
  47. {
  48. var subscribed = false;
  49. var xs = new MyObservable<int>(obs =>
  50. {
  51. subscribed = true;
  52. obs.OnNext(42);
  53. obs.OnCompleted();
  54. return new MyDisposable(() => { });
  55. }).ToAsyncEnumerable();
  56. Assert.False(subscribed);
  57. var e = xs.GetAsyncEnumerator();
  58. Assert.True(subscribed);
  59. await HasNextAsync(e, 42);
  60. await NoNextAsync(e);
  61. }
  62. [Fact]
  63. public async Task ToAsyncEnumerable4Async()
  64. {
  65. var ex = new Exception("Bang!");
  66. var subscribed = false;
  67. var xs = new MyObservable<int>(obs =>
  68. {
  69. subscribed = true;
  70. obs.OnError(ex);
  71. return new MyDisposable(() => { });
  72. }).ToAsyncEnumerable();
  73. Assert.False(subscribed);
  74. var e = xs.GetAsyncEnumerator();
  75. Assert.True(subscribed);
  76. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  77. }
  78. [Fact]
  79. public async Task ToAsyncEnumerable5Async()
  80. {
  81. var set = new HashSet<int>(new[] { 1, 2, 3, 4 });
  82. var xs = set.ToAsyncEnumerable();
  83. var e = xs.GetAsyncEnumerator();
  84. await HasNextAsync(e, 1);
  85. await HasNextAsync(e, 2);
  86. await HasNextAsync(e, 3);
  87. await HasNextAsync(e, 4);
  88. await NoNextAsync(e);
  89. }
  90. [Fact]
  91. public async Task ToAsyncEnumerable6()
  92. {
  93. var set = new HashSet<int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8 });
  94. var xs = set.ToAsyncEnumerable();
  95. var arr = await xs.ToArrayAsync();
  96. Assert.True(set.SetEquals(arr));
  97. }
  98. [Fact]
  99. public async Task ToAsyncEnumerable7()
  100. {
  101. var set = new HashSet<int>(new[] { 1, 2, 3, 4 });
  102. var xs = set.ToAsyncEnumerable();
  103. var arr = await xs.ToListAsync();
  104. Assert.True(set.SetEquals(arr));
  105. }
  106. [Fact]
  107. public async Task ToAsyncEnumerable8()
  108. {
  109. var set = new HashSet<int>(new[] { 1, 2, 3, 4 });
  110. var xs = set.ToAsyncEnumerable();
  111. var c = await xs.CountAsync();
  112. Assert.Equal(set.Count, c);
  113. }
  114. [Fact]
  115. public async Task ToAsyncEnumerable9()
  116. {
  117. var set = new HashSet<int>(new[] { 1, 2, 3, 4 });
  118. var xs = set.ToAsyncEnumerable();
  119. await SequenceIdentity(xs);
  120. }
  121. [Fact]
  122. public async Task ToAsyncEnumerable10()
  123. {
  124. var xs = new[] { 1, 2, 3, 4 }.ToAsyncEnumerable();
  125. await SequenceIdentity(xs);
  126. }
  127. [Fact]
  128. public void ToAsyncEnumerable11()
  129. {
  130. var set = new HashSet<int>(new[] { 1, 2, 3, 4 });
  131. var xs = set.ToAsyncEnumerable();
  132. var xc = xs as ICollection<int>;
  133. Assert.NotNull(xc);
  134. Assert.False(xc.IsReadOnly);
  135. xc.Add(5);
  136. Assert.True(xc.Contains(5));
  137. Assert.True(xc.Remove(5));
  138. var arr = new int[4];
  139. xc.CopyTo(arr, 0);
  140. Assert.True(arr.SequenceEqual(xc));
  141. xc.Clear();
  142. Assert.Equal(0, xc.Count);
  143. }
  144. [Fact]
  145. public void ToAsyncEnumerable12()
  146. {
  147. var set = new List<int> { 1, 2, 3, 4 };
  148. var xs = set.ToAsyncEnumerable();
  149. var xl = xs as IList<int>;
  150. Assert.NotNull(xl);
  151. Assert.False(xl.IsReadOnly);
  152. xl.Add(5);
  153. Assert.True(xl.Contains(5));
  154. Assert.True(xl.Remove(5));
  155. xl.Insert(2, 10);
  156. Assert.Equal(2, xl.IndexOf(10));
  157. xl.RemoveAt(2);
  158. xl[0] = 7;
  159. Assert.Equal(7, xl[0]);
  160. var arr = new int[4];
  161. xl.CopyTo(arr, 0);
  162. Assert.True(arr.SequenceEqual(xl));
  163. xl.Clear();
  164. Assert.Equal(0, xl.Count);
  165. }
  166. [Fact]
  167. public async Task ToAsyncEnumerable_With_Completed_TaskAsync()
  168. {
  169. var task = Task.Factory.StartNew(() => 36);
  170. var xs = task.ToAsyncEnumerable();
  171. var e = xs.GetAsyncEnumerator();
  172. Assert.True(await e.MoveNextAsync());
  173. Assert.Equal(36, e.Current);
  174. Assert.False(await e.MoveNextAsync());
  175. }
  176. [Fact]
  177. public async Task ToAsyncEnumerable_With_Faulted_TaskAsync()
  178. {
  179. var ex = new InvalidOperationException();
  180. var tcs = new TaskCompletionSource<int>();
  181. tcs.SetException(ex);
  182. var xs = tcs.Task.ToAsyncEnumerable();
  183. var e = xs.GetAsyncEnumerator();
  184. await AssertThrowsAsync(e.MoveNextAsync(), ex);
  185. }
  186. [Fact]
  187. public async Task ToAsyncEnumerable_With_Canceled_TaskAsync()
  188. {
  189. var tcs = new TaskCompletionSource<int>();
  190. tcs.SetCanceled();
  191. var xs = tcs.Task.ToAsyncEnumerable();
  192. var e = xs.GetAsyncEnumerator();
  193. await AssertThrowsAsync<TaskCanceledException>(e.MoveNextAsync().AsTask());
  194. }
  195. private sealed class MyObservable<T> : IObservable<T>
  196. {
  197. private readonly Func<IObserver<T>, IDisposable> _subscribe;
  198. public MyObservable(Func<IObserver<T>, IDisposable> subscribe)
  199. {
  200. _subscribe = subscribe;
  201. }
  202. public IDisposable Subscribe(IObserver<T> observer)
  203. {
  204. return _subscribe(observer);
  205. }
  206. }
  207. private sealed class MyDisposable : IDisposable
  208. {
  209. private readonly Action _dispose;
  210. public MyDisposable(Action dispose)
  211. {
  212. _dispose = dispose;
  213. }
  214. public void Dispose()
  215. {
  216. _dispose();
  217. }
  218. }
  219. }
  220. }