|
5 | 5 | using System.Threading.Tasks; |
6 | 6 | using Xunit; |
7 | 7 |
|
8 | | -namespace Open.ChannelExtensions.Tests |
| 8 | +namespace Open.ChannelExtensions.Tests; |
| 9 | + |
| 10 | +public static class BatchTests |
9 | 11 | { |
10 | | - public static class BatchTests |
| 12 | + [Fact] |
| 13 | + public static async Task SimpleBatch2Test() |
11 | 14 | { |
12 | | - [Fact] |
13 | | - public static async Task SimpleBatch2Test() |
| 15 | + var c = Channel.CreateUnbounded<int>(new UnboundedChannelOptions { SingleReader = false, SingleWriter = false }); |
| 16 | + _ = Task.Run(async () => |
14 | 17 | { |
15 | | - var c = Channel.CreateUnbounded<int>(new UnboundedChannelOptions { SingleReader = false, SingleWriter = false }); |
16 | | - _ = Task.Run(async () => |
17 | | - { |
18 | | - await Task.Delay(1000); |
19 | | - c.Writer.TryWrite(1); |
20 | | - c.Writer.TryWrite(2); |
21 | | - c.Writer.TryWrite(3); |
22 | | - c.Writer.TryWrite(4); |
23 | | - c.Writer.TryWrite(5); |
24 | | - c.Writer.TryWrite(6); |
25 | | - c.Writer.Complete(); |
26 | | - }); |
| 18 | + await Task.Delay(1000); |
| 19 | + c.Writer.TryWrite(1); |
| 20 | + c.Writer.TryWrite(2); |
| 21 | + c.Writer.TryWrite(3); |
| 22 | + c.Writer.TryWrite(4); |
| 23 | + c.Writer.TryWrite(5); |
| 24 | + c.Writer.TryWrite(6); |
| 25 | + c.Writer.Complete(); |
| 26 | + }); |
27 | 27 |
|
28 | | - await c.Reader |
29 | | - .Batch(2) |
30 | | - .ReadAllAsync(async (batch, i) => |
31 | | - { |
32 | | - switch (i) |
33 | | - { |
34 | | - case 0: |
35 | | - Assert.Equal(1, batch[0]); |
36 | | - Assert.Equal(2, batch[1]); |
37 | | - break; |
38 | | - case 1: |
39 | | - Assert.Equal(3, batch[0]); |
40 | | - Assert.Equal(4, batch[1]); |
41 | | - break; |
42 | | - case 2: |
43 | | - Assert.Equal(5, batch[0]); |
44 | | - Assert.Equal(6, batch[1]); |
45 | | - break; |
46 | | - default: |
47 | | - throw new Exception("Shouldn't arrive here."); |
48 | | - } |
49 | | - await Task.Delay(500); |
50 | | - }); |
51 | | - |
52 | | - } |
53 | | - |
54 | | - [Fact] |
55 | | - public static async Task Batch2TestWithDelay() |
56 | | - { |
57 | | - var c = Channel.CreateUnbounded<int>(new UnboundedChannelOptions { SingleReader = false, SingleWriter = false }); |
58 | | - _ = Task.Run(async () => |
| 28 | + await c.Reader |
| 29 | + .Batch(2) |
| 30 | + .ReadAllAsync(async (batch, i) => |
59 | 31 | { |
60 | | - await Task.Delay(1000); |
61 | | - c.Writer.TryWrite(1); |
62 | | - c.Writer.TryWrite(2); |
63 | | - c.Writer.TryWrite(3); |
64 | | - c.Writer.TryWrite(4); |
65 | | - c.Writer.TryWrite(5); |
66 | | - c.Writer.TryWrite(6); |
67 | | - }); |
68 | | - |
69 | | - using var tokenSource = new CancellationTokenSource(); |
70 | | - var token = tokenSource.Token; |
71 | | - await c.Reader |
72 | | - .Batch(2) |
73 | | - .ReadAllAsync(async (batch, i) => |
| 32 | + switch (i) |
74 | 33 | { |
75 | | - switch (i) |
76 | | - { |
77 | | - case 0: |
78 | | - Assert.Equal(1, batch[0]); |
79 | | - Assert.Equal(2, batch[1]); |
80 | | - break; |
81 | | - case 1: |
82 | | - Assert.Equal(3, batch[0]); |
83 | | - Assert.Equal(4, batch[1]); |
84 | | - _ = Task.Run(async () => |
85 | | - { |
86 | | - await Task.Delay(60000, token); |
87 | | - if (!token.IsCancellationRequested) c.Writer.TryComplete(new Exception("Should have completed successfuly.")); |
88 | | - }); |
89 | | - break; |
90 | | - case 2: |
91 | | - Assert.Equal(5, batch[0]); |
92 | | - Assert.Equal(6, batch[1]); |
93 | | - tokenSource.Cancel(); |
94 | | - c.Writer.Complete(); |
95 | | - break; |
96 | | - default: |
97 | | - throw new Exception("Shouldn't arrive here."); |
98 | | - } |
99 | | - await Task.Delay(500); |
100 | | - }); |
101 | | - |
102 | | - } |
| 34 | + case 0: |
| 35 | + Assert.Equal(1, batch[0]); |
| 36 | + Assert.Equal(2, batch[1]); |
| 37 | + break; |
| 38 | + case 1: |
| 39 | + Assert.Equal(3, batch[0]); |
| 40 | + Assert.Equal(4, batch[1]); |
| 41 | + break; |
| 42 | + case 2: |
| 43 | + Assert.Equal(5, batch[0]); |
| 44 | + Assert.Equal(6, batch[1]); |
| 45 | + break; |
| 46 | + default: |
| 47 | + throw new Exception("Shouldn't arrive here."); |
| 48 | + } |
| 49 | + await Task.Delay(500); |
| 50 | + }); |
103 | 51 |
|
| 52 | + } |
104 | 53 |
|
105 | | - [Fact] |
106 | | - public static async Task ForceBatchTest() |
| 54 | + [Fact] |
| 55 | + public static async Task Batch2TestWithDelay() |
| 56 | + { |
| 57 | + var c = Channel.CreateUnbounded<int>(new UnboundedChannelOptions { SingleReader = false, SingleWriter = false }); |
| 58 | + _ = Task.Run(async () => |
107 | 59 | { |
108 | | - var c = Channel.CreateUnbounded<int>(new UnboundedChannelOptions { SingleReader = false, SingleWriter = false }); |
109 | | - _ = Task.Run(async () => |
| 60 | + await Task.Delay(1000); |
| 61 | + c.Writer.TryWrite(1); |
| 62 | + c.Writer.TryWrite(2); |
| 63 | + c.Writer.TryWrite(3); |
| 64 | + c.Writer.TryWrite(4); |
| 65 | + c.Writer.TryWrite(5); |
| 66 | + c.Writer.TryWrite(6); |
| 67 | + }); |
| 68 | + |
| 69 | + using var tokenSource = new CancellationTokenSource(); |
| 70 | + var token = tokenSource.Token; |
| 71 | + await c.Reader |
| 72 | + .Batch(2) |
| 73 | + .ReadAllAsync(async (batch, i) => |
110 | 74 | { |
111 | | - await Task.Delay(1000); |
112 | | - c.Writer.TryWrite(1); |
113 | | - c.Writer.TryWrite(2); |
114 | | - c.Writer.TryWrite(3); |
115 | | - c.Writer.TryWrite(4); |
116 | | - c.Writer.TryWrite(5); |
| 75 | + switch (i) |
| 76 | + { |
| 77 | + case 0: |
| 78 | + Assert.Equal(1, batch[0]); |
| 79 | + Assert.Equal(2, batch[1]); |
| 80 | + break; |
| 81 | + case 1: |
| 82 | + Assert.Equal(3, batch[0]); |
| 83 | + Assert.Equal(4, batch[1]); |
| 84 | + _ = Task.Run(async () => |
| 85 | + { |
| 86 | + await Task.Delay(60000, token); |
| 87 | + if (!token.IsCancellationRequested) c.Writer.TryComplete(new Exception("Should have completed successfuly.")); |
| 88 | + }); |
| 89 | + break; |
| 90 | + case 2: |
| 91 | + Assert.Equal(5, batch[0]); |
| 92 | + Assert.Equal(6, batch[1]); |
| 93 | + tokenSource.Cancel(); |
| 94 | + c.Writer.Complete(); |
| 95 | + break; |
| 96 | + default: |
| 97 | + throw new Exception("Shouldn't arrive here."); |
| 98 | + } |
| 99 | + await Task.Delay(500); |
117 | 100 | }); |
118 | 101 |
|
119 | | - using var tokenSource = new CancellationTokenSource(10000); |
120 | | - var reader = c.Reader.Batch(3); |
121 | | - Assert.Equal(2, await reader.ReadAllAsync(tokenSource.Token, async (batch, i) => |
122 | | - { |
123 | | - switch (i) |
124 | | - { |
125 | | - case 0: |
126 | | - Assert.Equal(1, batch[0]); |
127 | | - Assert.Equal(2, batch[1]); |
128 | | - Assert.Equal(3, batch[2]); |
129 | | - await Task.Delay(500); |
130 | | - reader.ForceBatch(); |
131 | | - break; |
132 | | - case 1: |
133 | | - Assert.Equal(2, batch.Count); |
134 | | - Assert.Equal(4, batch[0]); |
135 | | - Assert.Equal(5, batch[1]); |
136 | | - c.Writer.Complete(); |
137 | | - break; |
138 | | - default: |
139 | | - throw new Exception("Shouldn't arrive here."); |
140 | | - } |
141 | | - await Task.Delay(500); |
142 | | - })); |
| 102 | + } |
143 | 103 |
|
144 | | - } |
145 | 104 |
|
146 | | - [Fact] |
147 | | - public static async Task ForceBatchTest2() |
| 105 | + [Fact] |
| 106 | + public static async Task ForceBatchTest() |
| 107 | + { |
| 108 | + var c = Channel.CreateUnbounded<int>(new UnboundedChannelOptions { SingleReader = false, SingleWriter = false }); |
| 109 | + _ = Task.Run(async () => |
148 | 110 | { |
149 | | - var c = Channel.CreateUnbounded<int>(new UnboundedChannelOptions { SingleReader = false, SingleWriter = false }); |
150 | | - var reader = c.Reader.Batch(3); |
151 | | - _ = Task.Run(async () => |
152 | | - { |
153 | | - await Task.Delay(1000); |
154 | | - c.Writer.TryWrite(1); |
155 | | - c.Writer.TryWrite(2); |
156 | | - c.Writer.TryWrite(3); |
157 | | - c.Writer.TryWrite(4); |
158 | | - c.Writer.TryWrite(5); |
159 | | - Debug.WriteLine("Writing Complete."); |
160 | | - |
161 | | - await Task.Delay(1000); |
162 | | - Assert.True(reader.ForceBatch()); |
163 | | - Debug.WriteLine("Batch Forced."); |
164 | | - }); |
| 111 | + await Task.Delay(1000); |
| 112 | + c.Writer.TryWrite(1); |
| 113 | + c.Writer.TryWrite(2); |
| 114 | + c.Writer.TryWrite(3); |
| 115 | + c.Writer.TryWrite(4); |
| 116 | + c.Writer.TryWrite(5); |
| 117 | + }); |
165 | 118 |
|
166 | | - using var tokenSource = new CancellationTokenSource(6000); |
167 | | - Assert.Equal(2, await reader.ReadAllAsync(tokenSource.Token, async (batch, i) => |
| 119 | + using var tokenSource = new CancellationTokenSource(10000); |
| 120 | + var reader = c.Reader.Batch(3); |
| 121 | + Assert.Equal(2, await reader.ReadAllAsync(tokenSource.Token, async (batch, i) => |
168 | 122 | { |
169 | 123 | switch (i) |
170 | 124 | { |
171 | 125 | case 0: |
172 | 126 | Assert.Equal(1, batch[0]); |
173 | 127 | Assert.Equal(2, batch[1]); |
174 | 128 | Assert.Equal(3, batch[2]); |
175 | | - Debug.WriteLine("First batch received."); |
| 129 | + await Task.Delay(500); |
| 130 | + reader.ForceBatch(); |
176 | 131 | break; |
177 | 132 | case 1: |
178 | 133 | Assert.Equal(2, batch.Count); |
179 | 134 | Assert.Equal(4, batch[0]); |
180 | 135 | Assert.Equal(5, batch[1]); |
181 | | - Debug.WriteLine("Second batch received."); |
182 | 136 | c.Writer.Complete(); |
183 | 137 | break; |
184 | 138 | default: |
185 | 139 | throw new Exception("Shouldn't arrive here."); |
186 | 140 | } |
187 | 141 | await Task.Delay(500); |
188 | 142 | })); |
189 | | - } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + [Fact] |
| 147 | + public static async Task ForceBatchTest2() |
| 148 | + { |
| 149 | + var c = Channel.CreateUnbounded<int>(new UnboundedChannelOptions { SingleReader = false, SingleWriter = false }); |
| 150 | + var reader = c.Reader.Batch(3); |
| 151 | + _ = Task.Run(async () => |
| 152 | + { |
| 153 | + await Task.Delay(1000); |
| 154 | + c.Writer.TryWrite(1); |
| 155 | + c.Writer.TryWrite(2); |
| 156 | + c.Writer.TryWrite(3); |
| 157 | + c.Writer.TryWrite(4); |
| 158 | + c.Writer.TryWrite(5); |
| 159 | + Debug.WriteLine("Writing Complete."); |
| 160 | + |
| 161 | + await Task.Delay(1000); |
| 162 | + Assert.True(reader.ForceBatch()); |
| 163 | + Debug.WriteLine("Batch Forced."); |
| 164 | + }); |
| 165 | + |
| 166 | + using var tokenSource = new CancellationTokenSource(6000); |
| 167 | + Assert.Equal(2, await reader.ReadAllAsync(tokenSource.Token, async (batch, i) => |
| 168 | + { |
| 169 | + switch (i) |
| 170 | + { |
| 171 | + case 0: |
| 172 | + Assert.Equal(1, batch[0]); |
| 173 | + Assert.Equal(2, batch[1]); |
| 174 | + Assert.Equal(3, batch[2]); |
| 175 | + Debug.WriteLine("First batch received."); |
| 176 | + break; |
| 177 | + case 1: |
| 178 | + Assert.Equal(2, batch.Count); |
| 179 | + Assert.Equal(4, batch[0]); |
| 180 | + Assert.Equal(5, batch[1]); |
| 181 | + Debug.WriteLine("Second batch received."); |
| 182 | + c.Writer.Complete(); |
| 183 | + break; |
| 184 | + default: |
| 185 | + throw new Exception("Shouldn't arrive here."); |
| 186 | + } |
| 187 | + await Task.Delay(500); |
| 188 | + })); |
190 | 189 | } |
191 | 190 | } |
0 commit comments