Skip to content

Commit

Permalink
Add logging to test and retries to test http client
Browse files Browse the repository at this point in the history
  • Loading branch information
natemcmaster committed Apr 11, 2018
1 parent 7c2eb62 commit d6900ca
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions test/dotnet-serve.Tests/DotNetServe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ private DotNetServe(Process process, int port, ITestOutputHelper output)

public void Start()
{
if (_output != null)
{
_output.WriteLine($"Starting: {_process.StartInfo.FileName} {_process.StartInfo.Arguments}");
}
_process.Start();
_process.BeginOutputReadLine();
_process.BeginErrorReadLine();
Expand Down
37 changes: 37 additions & 0 deletions test/dotnet-serve.Tests/HttpClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;
using Xunit.Abstractions;

namespace McMaster.DotNet.Serve.Tests
{
static class HttpClientExtensions
{
public static async Task<string> GetStringWithRetriesAsync(this HttpClient client, string uri, int retries = 5)
{
while (retries > 0)
{
retries--;
try
{
return await client.GetStringAsync(uri);
}
catch
{
await Task.Delay(TimeSpan.FromMilliseconds(100));
}
}

throw new TimeoutException("Failed to connect to " + uri);
}
}
}

2 changes: 1 addition & 1 deletion test/dotnet-serve.Tests/RazorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task ItCanLoadSimpleRazorPage()
output: _output,
enableRazor: true))
{
var result = await serve.Client.GetStringAsync("/");
var result = await serve.Client.GetStringWithRetriesAsync("/", retries: 5);
Assert.Contains("<li>Item 2</li>", result);
}
}
Expand Down

0 comments on commit d6900ca

Please sign in to comment.