-
Notifications
You must be signed in to change notification settings - Fork 2
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
14 changed files
with
658 additions
and
91 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 |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright (c) David Karnok & Contributors. | ||
// Licensed under the Apache 2.0 License. | ||
// See LICENSE file in the project root for full license information. | ||
|
||
using Xunit; | ||
using async_enumerable_dotnet; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace async_enumerable_dotnet_test | ||
{ | ||
public class BufferBoundaryTest | ||
{ | ||
[Fact] | ||
public async void Size() | ||
{ | ||
await AsyncEnumerable.Range(1, 5) | ||
.Buffer(AsyncEnumerable.Timer(TimeSpan.FromMilliseconds(200)), 2) | ||
.AssertResult( | ||
new List<int>(new [] { 1, 2 }), | ||
new List<int>(new[] { 3, 4 }), | ||
new List<int>(new[] { 5 }) | ||
); | ||
} | ||
|
||
[Fact] | ||
public async void Size_Collection() | ||
{ | ||
await AsyncEnumerable.Range(1, 5) | ||
.Buffer(AsyncEnumerable.Timer(TimeSpan.FromMilliseconds(200)), () => new HashSet<int>(), 2) | ||
.AssertResult( | ||
new HashSet<int>(new [] { 1, 2 }), | ||
new HashSet<int>(new[] { 3, 4 }), | ||
new HashSet<int>(new[] { 5 }) | ||
); | ||
} | ||
|
||
[Fact] | ||
public async void Time() | ||
{ | ||
var t = 100L; | ||
if (Environment.GetEnvironmentVariable("CI") != null) | ||
{ | ||
t = 1000L; | ||
} | ||
|
||
await | ||
TestHelper.TimeSequence( | ||
t, t, | ||
3 * t, 3 * t, 3 * t, | ||
7 * t, 7 * t | ||
) | ||
.Buffer(AsyncEnumerable.Interval(TimeSpan.FromMilliseconds(2 * t))) | ||
.AssertResult( | ||
new List<long>(new[] { t, t }), | ||
new List<long>(new[] { 3 * t, 3 * t, 3 * t }), | ||
new List<long>(), | ||
new List<long>(new[] { 7 * t, 7 * t }) | ||
); | ||
|
||
} | ||
|
||
[Fact] | ||
public async void Error_Main() | ||
{ | ||
await AsyncEnumerable.Error<int>(new InvalidOperationException()) | ||
.Buffer(AsyncEnumerable.Timer(TimeSpan.FromMilliseconds(200)), 2) | ||
.AssertFailure(typeof(InvalidOperationException)); | ||
} | ||
|
||
[Fact] | ||
public async void Error_Other() | ||
{ | ||
await AsyncEnumerable.Timer(TimeSpan.FromMilliseconds(200)) | ||
.Buffer(AsyncEnumerable.Error<int>(new InvalidOperationException()), 2) | ||
.AssertFailure(typeof(InvalidOperationException)); | ||
} | ||
|
||
[Fact] | ||
public async void Empty_Main() | ||
{ | ||
await AsyncEnumerable.Empty<int>() | ||
.Buffer(AsyncEnumerable.Timer(TimeSpan.FromMilliseconds(200)), 2) | ||
.AssertResult(); | ||
} | ||
|
||
[Fact] | ||
public async void Empty_Other() | ||
{ | ||
await AsyncEnumerable.Timer(TimeSpan.FromMilliseconds(200)) | ||
.Buffer(AsyncEnumerable.Empty<int>(), 2) | ||
.AssertResult(); | ||
} | ||
|
||
[Fact] | ||
public async void Time_And_Size() | ||
{ | ||
var t = 100L; | ||
if (Environment.GetEnvironmentVariable("CI") != null) | ||
{ | ||
t = 1000L; | ||
} | ||
|
||
await | ||
TestHelper.TimeSequence( | ||
t, t, | ||
3 * t, 3 * t, 3 * t, | ||
7 * t, 7 * t, 7 * t, 7 * t | ||
) | ||
.Buffer(AsyncEnumerable.Interval(TimeSpan.FromMilliseconds(2 * t)), 3) | ||
.AssertResult( | ||
new List<long>(new[] { t, t }), | ||
new List<long>(new[] { 3 * t, 3 * t, 3 * t }), | ||
new List<long>(), | ||
new List<long>(), | ||
new List<long>(new[] { 7 * t, 7 * t, 7 * t }), | ||
new List<long>(new[] { 7 * t }) | ||
); | ||
|
||
} | ||
} | ||
} |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) David Karnok & Contributors. | ||
// Licensed under the Apache 2.0 License. | ||
// See LICENSE file in the project root for full license information. | ||
|
||
using Xunit; | ||
using async_enumerable_dotnet.impl; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
|
||
namespace async_enumerable_dotnet_test | ||
{ | ||
public class SlimResumeTest | ||
{ | ||
[Fact] | ||
public async void ReadyUpfront() | ||
{ | ||
var rsm = new SlimResume(); | ||
rsm.Signal(); | ||
|
||
await rsm; | ||
} | ||
|
||
[Fact] | ||
public async void Completed() | ||
{ | ||
var rsm = SlimResume.Completed; | ||
|
||
await rsm; | ||
} | ||
|
||
[Fact] | ||
public async void SignalLater() | ||
{ | ||
var rsm = new SlimResume(); | ||
|
||
var t = Task.Delay(100) | ||
.ContinueWith(t0 => rsm.Signal()); | ||
|
||
await rsm; | ||
|
||
await t; | ||
} | ||
|
||
|
||
[Fact] | ||
public async void Race() | ||
{ | ||
for (var i = 0; i < 10_000; i++) | ||
{ | ||
var rsm = new SlimResume(); | ||
|
||
var wip = 2; | ||
|
||
var t1 = Task.Factory.StartNew(() => | ||
{ | ||
if (Interlocked.Decrement(ref wip) != 0) | ||
{ | ||
while (Volatile.Read(ref wip) != 0) { } | ||
} | ||
rsm.Signal(); | ||
}, TaskCreationOptions.LongRunning); | ||
|
||
var t2 = Task.Factory.StartNew(async () => | ||
{ | ||
if (Interlocked.Decrement(ref wip) != 0) | ||
{ | ||
while (Volatile.Read(ref wip) != 0) { } | ||
} | ||
await rsm; | ||
}, TaskCreationOptions.LongRunning); | ||
|
||
await t1; | ||
|
||
await t2; | ||
} | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.