public async Task SSEWaitsForResponseToStart() { using (StartVerifiableLog()) { var httpHandler = new TestHttpMessageHandler(); var connectResponseTcs = new TaskCompletionSource(); httpHandler.OnGet("/?id=00000000-0000-0000-0000-000000000000", async(_, __) => { await connectResponseTcs.Task; return(ResponseUtils.CreateResponse(HttpStatusCode.Accepted)); }); var sse = new ServerSentEventsTransport(new HttpClient(httpHandler), loggerFactory: LoggerFactory); await WithConnectionAsync( CreateConnection(httpHandler, loggerFactory : LoggerFactory, transport : sse), async (connection) => { var startTask = connection.StartAsync(); Assert.False(connectResponseTcs.Task.IsCompleted); Assert.False(startTask.IsCompleted); connectResponseTcs.TrySetResult(); await startTask.DefaultTimeout(); }); } }
public async Task SSECanBeCanceled() { bool ExpectedErrors(WriteContext writeContext) { return(writeContext.LoggerName == typeof(HttpConnection).FullName && writeContext.EventId.Name == "ErrorStartingTransport"); } using (StartVerifiableLog(expectedErrorsFilter: ExpectedErrors)) { var httpHandler = new TestHttpMessageHandler(); httpHandler.OnGet("/?id=00000000-0000-0000-0000-000000000000", (_, __) => { // Simulating a cancellationToken canceling this request. throw new OperationCanceledException("Cancel SSE Start."); }); var sse = new ServerSentEventsTransport(new HttpClient(httpHandler), loggerFactory: LoggerFactory); await WithConnectionAsync( CreateConnection(httpHandler, loggerFactory : LoggerFactory, transport : sse, transportType : HttpTransportType.ServerSentEvents), async (connection) => { var ex = await Assert.ThrowsAsync <AggregateException>(async() => await connection.StartAsync()).DefaultTimeout(); }); } }
public async Task SSEWontStartIfSuccessfulConnectionIsNotEstablished() { bool ExpectedErrors(WriteContext writeContext) { return(writeContext.LoggerName == typeof(HttpConnection).FullName && writeContext.EventId.Name == "ErrorStartingTransport"); } using (StartVerifiableLog(expectedErrorsFilter: ExpectedErrors)) { var httpHandler = new TestHttpMessageHandler(); httpHandler.OnGet("/?id=00000000-0000-0000-0000-000000000000", (_, __) => { return(Task.FromResult(ResponseUtils.CreateResponse(HttpStatusCode.InternalServerError))); }); var sse = new ServerSentEventsTransport(new HttpClient(httpHandler), loggerFactory: LoggerFactory); await WithConnectionAsync( CreateConnection(httpHandler, loggerFactory : LoggerFactory, transport : sse), async (connection) => { await Assert.ThrowsAsync <AggregateException>( () => connection.StartAsync().DefaultTimeout()); }); } }