public void Client_Throttles_Automatically() { // Arrange var httpClient = A.Fake <ITeamHavenHttpClient>(); A.CallTo(() => httpClient.GetAsync("/api/server")).ReturnsNextFromSequence( Task.FromResult(CreateRetryAfterResponse(TimeSpan.FromSeconds(5))), Task.FromResult(CreateRetryAfterResponse(TimeSpan.FromSeconds(5))), Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{}", System.Text.Encoding.UTF8, "application/json") }) ); // Act var stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); var webClient = new TeamHavenClient(httpClient); var info = webClient.GetServerInfo().Result; stopwatch.Stop(); // Assert A.CallTo(() => httpClient.GetAsync("/api/server")).MustHaveHappened(Repeated.Exactly.Times(3)); Assert.AreEqual(10, (int)stopwatch.Elapsed.TotalSeconds); }
static void Main(string[] args) { // Create an HttpClient instance and initialise it with TeamHaven specific defaults var httpClient = TeamHavenClient.CreateHttpClient(ApplicationName, ApplicationEmail, Username, Password, Account, useHttpForEasyDebugging: true); // Create a proxy that fetches data using the HttpClient we just created var proxy = new TeamHavenClient(httpClient); // Example: Use the proxy to call the Server Information API proxy.GetServerInfo().ContinueWith(x => { if (!x.IsFaulted) { Console.WriteLine("Server identifies itself as " + x.Result.Name); Console.WriteLine("Server's preferred base address is " + (x.Result.HttpsUrl ?? x.Result.HttpUrl)); } else { Console.WriteLine("/api/server failed: ", x.Exception.InnerException.Message); } }); // Example: Display some information about the authenticated user proxy.GetUser().ContinueWith(x => { if (!x.IsFaulted) { Console.WriteLine("Authorized as " + x.Result.DisplayName); } else { Console.WriteLine("/api/user failed: {0}", x.Exception.InnerException.Message); } }); Console.ReadKey(); }
public void Client_Throttles_Automatically() { // Arrange var httpClient = A.Fake<ITeamHavenHttpClient>(); A.CallTo(() => httpClient.GetAsync("/api/server")).ReturnsNextFromSequence( Task.FromResult(CreateRetryAfterResponse(TimeSpan.FromSeconds(5))), Task.FromResult(CreateRetryAfterResponse(TimeSpan.FromSeconds(5))), Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{}", System.Text.Encoding.UTF8, "application/json") }) ); // Act var stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); var webClient = new TeamHavenClient(httpClient); var info = webClient.GetServerInfo().Result; stopwatch.Stop(); // Assert A.CallTo(() => httpClient.GetAsync("/api/server")).MustHaveHappened(Repeated.Exactly.Times(3)); Assert.AreEqual(10, (int)stopwatch.Elapsed.TotalSeconds); }