public async Task It_can_skip_buffering_if_the_index_is_small_enough() { // Simulate 100 records var index = Enumerable.Range(1, 1000).ToHashSet(); var records = index.Select(id => new StubRecord(id)).ToList(); var sut = SplitQuery.Create <int, StubRecord>( (range, ct) => { var found = records.Where(record => range.Contains(record.Id)).ToHashSet(); return(Task.FromResult((IReadOnlyCollection <StubRecord>)found)); } ); var actual = await sut.QueryAsync(index).ToListAsync(); Assert.Equal(index.Count, actual.Count); Assert.All(index, id => Assert.Contains(actual, record => record.Id == id)); Assert.All( actual, record => { Assert.Contains(record.Id, index); } ); }
public async Task It_respects_cancellation_requested(int resultTotal, int bufferSize) { // Check if cancellation works in both scenarios where the buffer size is smaller or larger than the index // because the implementation is slightly optimized for the case where buffering is not needed CancellationTokenSource cancellationTokenSource = new(); var index = Enumerable.Range(1, resultTotal).ToHashSet(); var records = index.Select(id => new StubRecord(id)).ToList(); var sut = SplitQuery.Create <int, StubRecord>( (range, ct) => { var found = records.Where(record => range.Contains(record.Id)).ToHashSet(); return(Task.FromResult((IReadOnlyCollection <StubRecord>)found)); }, bufferSize ); var received = 0; var producer = sut.QueryAsync(index, cancellationToken: cancellationTokenSource.Token); var reason = await Assert.ThrowsAsync <OperationCanceledException>( async() => { await foreach (var _ in producer.WithCancellation(cancellationTokenSource.Token)) { if (++received == 107) { cancellationTokenSource.Cancel(); } } } ); Assert.True(cancellationTokenSource.Token.Equals(reason.CancellationToken)); Assert.Equal(107, received); }
public IAsyncEnumerable <Item> GetItemsByIds( IReadOnlyCollection <int> itemIds, Language?language = default, MissingMemberBehavior missingMemberBehavior = default, IProgress <ICollectionContext>?progress = default, CancellationToken cancellationToken = default ) { var producer = SplitQuery.Create <int, Item>( async(range, ct) => { var request = new ItemsByIdsRequest(range) { Language = language, MissingMemberBehavior = missingMemberBehavior }; var response = await request.SendAsync(http, ct).ConfigureAwait(false); return(response.Values); }, progress ); return(producer.QueryAsync(itemIds, cancellationToken: cancellationToken)); }