-
-
Notifications
You must be signed in to change notification settings - Fork 846
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
197 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,137 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Cysharp.Threading.Tasks; | ||
using Cysharp.Threading.Tasks.Linq; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace NetCoreTests.Linq | ||
{ | ||
public class Merge | ||
public class MergeTest | ||
{ | ||
[Fact] | ||
public async Task Hoge() | ||
public async Task TwoSource() | ||
{ | ||
var a = UniTaskAsyncEnumerable.Range(0, 5).Select(x => (x + 1) * 100); | ||
var b = UniTaskAsyncEnumerable.Range(0, 3).Select(x => (x + 1) * 200); | ||
var semaphore = new SemaphoreSlim(1, 1); | ||
|
||
var a = UniTaskAsyncEnumerable.Create<string>(async (writer, _) => | ||
{ | ||
await UniTask.SwitchToThreadPool(); | ||
await semaphore.WaitAsync(); | ||
await writer.YieldAsync("A1"); | ||
semaphore.Release(); | ||
await semaphore.WaitAsync(); | ||
await writer.YieldAsync("A2"); | ||
semaphore.Release(); | ||
}); | ||
|
||
var b = UniTaskAsyncEnumerable.Create<string>(async (writer, _) => | ||
{ | ||
await UniTask.SwitchToThreadPool(); | ||
await semaphore.WaitAsync(); | ||
await writer.YieldAsync("B1"); | ||
await writer.YieldAsync("B2"); | ||
semaphore.Release(); | ||
await semaphore.WaitAsync(); | ||
await writer.YieldAsync("B3"); | ||
semaphore.Release(); | ||
}); | ||
|
||
var result = await a.Merge(b).ToArrayAsync(); | ||
result.Should().Equal("A1", "B1", "B2", "A2", "B3"); | ||
} | ||
|
||
[Fact] | ||
public async Task ThreeSource() | ||
{ | ||
var semaphore = new SemaphoreSlim(0, 1); | ||
|
||
var a = UniTaskAsyncEnumerable.Create<string>(async (writer, _) => | ||
{ | ||
await UniTask.SwitchToThreadPool(); | ||
await semaphore.WaitAsync(); | ||
await writer.YieldAsync("A1"); | ||
semaphore.Release(); | ||
await semaphore.WaitAsync(); | ||
await writer.YieldAsync("A2"); | ||
semaphore.Release(); | ||
}); | ||
|
||
var b = UniTaskAsyncEnumerable.Create<string>(async (writer, _) => | ||
{ | ||
await UniTask.SwitchToThreadPool(); | ||
await semaphore.WaitAsync(); | ||
await writer.YieldAsync("B1"); | ||
await writer.YieldAsync("B2"); | ||
semaphore.Release(); | ||
await semaphore.WaitAsync(); | ||
await writer.YieldAsync("B3"); | ||
semaphore.Release(); | ||
}); | ||
|
||
var c = UniTaskAsyncEnumerable.Create<string>(async (writer, _) => | ||
{ | ||
await UniTask.SwitchToThreadPool(); | ||
await writer.YieldAsync("C1"); | ||
semaphore.Release(); | ||
}); | ||
|
||
var result = await a.Merge(b, c).ToArrayAsync(); | ||
result.Should().Equal("C1", "A1", "B1", "B2", "A2", "B3"); | ||
} | ||
|
||
[Fact] | ||
public async Task Throw() | ||
{ | ||
var a = UniTaskAsyncEnumerable.Create<string>(async (writer, _) => | ||
{ | ||
await writer.YieldAsync("A1"); | ||
}); | ||
|
||
var b = UniTaskAsyncEnumerable.Create<string>(async (writer, _) => | ||
{ | ||
throw new UniTaskTestException(); | ||
}); | ||
|
||
var enumerator = a.Merge(b).GetAsyncEnumerator(); | ||
(await enumerator.MoveNextAsync()).Should().Be(true); | ||
enumerator.Current.Should().Be(100); | ||
enumerator.Current.Should().Be("A1"); | ||
|
||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await enumerator.MoveNextAsync()); | ||
} | ||
|
||
[Fact] | ||
public async Task Cancel() | ||
{ | ||
var cts = new CancellationTokenSource(); | ||
|
||
var a = UniTaskAsyncEnumerable.Create<string>(async (writer, _) => | ||
{ | ||
await writer.YieldAsync("A1"); | ||
}); | ||
|
||
var b = UniTaskAsyncEnumerable.Create<string>(async (writer, _) => | ||
{ | ||
await writer.YieldAsync("B1"); | ||
}); | ||
|
||
var enumerator = a.Merge(b).GetAsyncEnumerator(cts.Token); | ||
(await enumerator.MoveNextAsync()).Should().Be(true); | ||
enumerator.Current.Should().Be("A1"); | ||
|
||
cts.Cancel(); | ||
await Assert.ThrowsAsync<OperationCanceledException>(async () => await enumerator.MoveNextAsync()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters