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

A strange behavior when Enumerable.Select is chained with AsyncEnumerable.Take #27

Open
CXuesong opened this issue Oct 11, 2017 · 0 comments
Assignees

Comments

@CXuesong
Copy link
Owner

CXuesong commented Oct 11, 2017

Test case

public class Playground : UnitTestsBase
{

    /// <inheritdoc />
    public Playground(ITestOutputHelper output) : base(output)
    {
    }

    [Fact]
    public async Task Test1()
    {
        var invoked = false;
        var ienu = new DelegateAsyncEnumerable<IEnumerable<string>>(async ct =>
        {
            if (invoked) return null;
            await Task.Yield();
            IEnumerable<string> x = new[] {"abc", "def", "ghi", "jkl"};
            invoked = true;
            return Tuple.Create(x.Select(a => a + "a"), true);
        });
        var y = ienu.SelectMany(t => t.ToAsyncEnumerable());
        var z = await y.Take(4).ToArray();
        ShallowTrace(z);
        Assert.DoesNotContain(null, z);
    }

}

The assertion fails. If I either change Take(4) to Take(3), change Select invocation to x.Select((a, i) => a + "a"), or remove await Task.Yield(); line, the test passes.

It seems that there is a strange interaction between the underlying SelectArrayIterator<TSource, TResult> and thread-context switches. I got a rough idea when looking at Iterator<TSource>.GetEnumerator, but would take another time to dig it deeper.

Related code in library:

// ISSUE directly return SelectArrayIterator<TSource, TResult> via Enumerable.Select
// can cause strange behavior when chained with other async LINQ methods. See
// https://github.com/CXuesong/WikiClientLibrary/issues/27
var topics = Topic.FromJsonTopicList(Site, jtopiclist).ToList();

@CXuesong CXuesong self-assigned this Oct 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant