public async Task Only_logs_when_completed(Uri uri, HttpStatusCode statusCode, bool throwException, bool isCompleted, bool isFaulted, bool isCanceled) { // setup var resetEvent = new ManualResetEvent(false); Action<MeasurementDetails> logFunc = details => { // validate details.Request.Method.Method.Should().Be("GET"); details.Request.RequestUri.Should().Be(uri); details.SendTask.IsCompleted.Should().Be(isCompleted); details.SendTask.IsFaulted.Should().Be(isFaulted); details.SendTask.IsCanceled.Should().Be(isCanceled); if (!throwException) { details.SendTask.Result.StatusCode.Should().Be(statusCode); } resetEvent.Set(); }; var handler = new MeasurementHandler(null, new TestHandler(TimeSpan.FromMilliseconds(100), statusCode, throwException), logFunc); var client = new HttpClient(handler); // execute try { await client.GetAsync(uri); } catch (Exception) { if (!throwException) throw; } // wait until the validation has completed (but limit to max 1 second to avoid waiting forever) bool awaited = resetEvent.WaitOne(TimeSpan.FromSeconds(1)); awaited.Should().BeTrue(); }
public async Task Does_not_await_completion_of_log_function() { // setup var resetEvent = new ManualResetEvent(false); var hasLogged = false; Action<MeasurementDetails> logFunc = async details => { // validate await Task.Delay(100); hasLogged = true; resetEvent.Set(); }; var expectedResult = Guid.NewGuid().ToString(); var handler = new MeasurementHandler(null, new TestHandler(TimeSpan.FromMilliseconds(100), HttpStatusCode.OK, false, expectedResult), logFunc); var client = new HttpClient(handler); // execute var result = await client.GetStringAsync("http://unit.test"); // validate that the string was retrieved, but the logger hasn't completed yet result.ShouldBeEquivalentTo(expectedResult); hasLogged.Should().BeFalse(); // ensure logging eventually happened resetEvent.WaitOne(TimeSpan.FromSeconds(1)); hasLogged.Should().BeTrue(); }