Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/AsyncEnumerator/AsyncEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AsyncEnumerator<T> : TaskLikeBase, IAsyncEnumeratorProducer<T>, IAs
private ExceptionDispatchInfo _exception;

private bool _isStarted;
private TaskCompletionSource<bool> _nextSource;
private TaskCompletionSource<bool> _nextSource =new TaskCompletionSource<bool>();
private TaskCompletionSource<bool> _yieldSource;

public static TaskProvider<IAsyncEnumeratorProducer<T>> Capture() => TaskProvider<IAsyncEnumeratorProducer<T>>.Instance;
Expand All @@ -32,7 +32,7 @@ public Task<bool> MoveNextAsync()
if (!_isStarted)
{
_isStarted = true;
return Task.FromResult(true);
return _nextSource.Task;
}

_nextSource = new TaskCompletionSource<bool>();
Expand All @@ -45,7 +45,7 @@ public Task<bool> MoveNextAsync()
internal override void SetException(ExceptionDispatchInfo exception)
{
_exception = exception;
_nextSource?.TrySetException(exception.SourceException);
_nextSource.TrySetException(exception.SourceException);
}

T IAsyncEnumeratorProducer<T>.Break()
Expand All @@ -65,10 +65,8 @@ Task IAsyncEnumeratorProducer<T>.Pause()
Task IAsyncEnumeratorProducer<T>.Return(T value)
{
Current = value;

_yieldSource = new TaskCompletionSource<bool>();

_nextSource?.TrySetResult(true);
_nextSource.TrySetResult(true);

return _yieldSource.Task;
}
Expand Down
16 changes: 16 additions & 0 deletions src/AsyncEnumeratorTests/AsyncEnumeratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public async Task EnumerationAdvancesCorrectlyAndCompletes2()
Assert.IsTrue(seq.IsCompleted, "Enumeration did not complete after return.");
}

[TestMethod]
public async Task EmptyEnumeratorTest()
{
var seq = GetEmptyEnumerator();

Assert.IsFalse(await seq.MoveNextAsync(), $"Call to {nameof(seq.MoveNextAsync)} did not return false after enumeration completed.");

Assert.IsTrue(seq.IsCompleted, "Enumeration did not complete after return.");
}

private static async AsyncEnumerator<int> ExceptionTest1()
{
Expand Down Expand Up @@ -89,5 +98,12 @@ private static async AsyncEnumerator<int> Test2()
return yield.Break();
}

private static async AsyncEnumerator<int> GetEmptyEnumerator()
{
var yield = await AsyncEnumerator<int>.Capture();

return yield.Break();
}

}
}
17 changes: 17 additions & 0 deletions src/AsyncEnumeratorTests/AsyncSequenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public async Task EnumerationAdvancesCorrectlyAndCompletes2()
Assert.IsTrue(seq.IsCompleted, "Enumeration did not complete after return.");
}

[TestMethod]
public async Task EmptyEnumeratorTest()
{
var seq = GetEmptyEnumerator();

Assert.IsFalse(await seq.MoveNextAsync(), $"Call to {nameof(seq.MoveNextAsync)} did not return false after enumeration completed.");

Assert.IsTrue(seq.IsCompleted, "Enumeration did not complete after return.");
}

private static async AsyncEnumerator<int> ExceptionTest1()
{
var yield = await AsyncEnumerator<int>.Capture();
Expand Down Expand Up @@ -91,5 +101,12 @@ private static async AsyncSequence<int> Test2()

return yield.Break();
}

private static async AsyncSequence<int> GetEmptyEnumerator()
{
var yield = await AsyncSequence<int>.Capture();

return yield.Break();
}
}
}