-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add iasyncenumerable support for netstandard2.1 * add missing withoutresult implementation * remove compile condition * add tests and sample endpoint
- Loading branch information
Showing
6 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
sample/Sample.FunctionalTests/AuthorEndpoints/StreamEndpoint.cs
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,40 @@ | ||
using Ardalis.HttpClientTestExtensions; | ||
using Sample.FunctionalTests.Models; | ||
using SampleEndpointApp; | ||
using SampleEndpointApp.DataAccess; | ||
using SampleEndpointApp.DomainModel; | ||
using Xunit; | ||
|
||
namespace Sample.FunctionalTests.AuthorEndpoints; | ||
|
||
public class StreamEndpoint : IClassFixture<CustomWebApplicationFactory<Startup>> | ||
{ | ||
private readonly HttpClient _client; | ||
|
||
public StreamEndpoint(CustomWebApplicationFactory<Startup> factory) | ||
{ | ||
_client = factory.CreateClient(); | ||
} | ||
|
||
[Fact] | ||
public async Task ReturnsTwoGivenTwoAuthors() | ||
{ | ||
var result = await _client.GetAndDeserialize<IEnumerable<Author>>(Routes.Authors.Stream()); | ||
|
||
Assert.NotNull(result); | ||
Assert.Equal(SeedData.Authors().Count, result.Count()); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenLongRunningListRequest_WhenTokenSourceCallsForCancellation_RequestIsTerminated() | ||
{ | ||
// Arrange, generate a token source that times out instantly | ||
var tokenSource = new CancellationTokenSource(TimeSpan.Zero); | ||
|
||
// Act | ||
var request = _client.GetAsync(Routes.Authors.Stream(), tokenSource.Token); | ||
|
||
// Assert | ||
var response = await Assert.ThrowsAsync<OperationCanceledException>(async () => await request); | ||
} | ||
} |
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,37 @@ | ||
using System.Runtime.CompilerServices; | ||
using Ardalis.ApiEndpoints; | ||
using AutoMapper; | ||
using Microsoft.AspNetCore.Mvc; | ||
using SampleEndpointApp.DomainModel; | ||
|
||
namespace SampleEndpointApp.Endpoints.Authors; | ||
|
||
public class Stream : EndpointBaseAsync | ||
.WithoutRequest | ||
.WithAsyncEnumerableResult<AuthorListResult> | ||
{ | ||
private readonly IAsyncRepository<Author> repository; | ||
private readonly IMapper mapper; | ||
|
||
public Stream( | ||
IAsyncRepository<Author> repository, | ||
IMapper mapper) | ||
{ | ||
this.repository = repository; | ||
this.mapper = mapper; | ||
} | ||
|
||
/// <summary> | ||
/// Stream all authors with a one second delay between entries | ||
/// </summary> | ||
[HttpGet("api/[namespace]/stream")] | ||
public override async IAsyncEnumerable<AuthorListResult> HandleAsync([EnumeratorCancellation] CancellationToken cancellationToken) | ||
{ | ||
var result = await repository.ListAllAsync(cancellationToken); | ||
foreach (var author in result) | ||
{ | ||
yield return mapper.Map<AuthorListResult>(author); | ||
await Task.Delay(1000, cancellationToken); | ||
} | ||
} | ||
} |
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