Skip to content

Commit d932339

Browse files
author
Oren (electricessence)
committed
Added .ForceBatch() test.
1 parent a8dff0e commit d932339

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Open.ChannelExtensions.Tests/BatchTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,50 @@ await c.Reader
106106
});
107107

108108
}
109+
110+
111+
[Fact]
112+
public static async Task ForceBatchTest()
113+
{
114+
var c = Channel.CreateUnbounded<int>(new UnboundedChannelOptions { SingleReader = false, SingleWriter = false });
115+
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
116+
Task.Run(async () =>
117+
{
118+
await Task.Delay(1000);
119+
c.Writer.TryWrite(1);
120+
c.Writer.TryWrite(2);
121+
c.Writer.TryWrite(3);
122+
c.Writer.TryWrite(4);
123+
c.Writer.TryWrite(5);
124+
});
125+
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
126+
127+
using var tokenSource = new CancellationTokenSource();
128+
var token = tokenSource.Token;
129+
var reader = c.Reader.Batch(3);
130+
Assert.Equal(2, await reader.ReadAllAsync(async (batch, i) =>
131+
{
132+
switch (i)
133+
{
134+
case 0:
135+
Assert.Equal(1, batch[0]);
136+
Assert.Equal(2, batch[1]);
137+
Assert.Equal(3, batch[2]);
138+
await Task.Delay(500);
139+
reader.ForceBatch();
140+
break;
141+
case 1:
142+
Assert.Equal(2, batch.Count);
143+
Assert.Equal(4, batch[0]);
144+
Assert.Equal(5, batch[1]);
145+
c.Writer.Complete();
146+
break;
147+
default:
148+
throw new Exception("Shouldn't arrive here.");
149+
}
150+
await Task.Delay(500);
151+
}));
152+
153+
}
109154
}
110155
}

0 commit comments

Comments
 (0)