public void VerifyBasicSendReceive() { var fooStub = new FooStub(); fooStub.ReturnValueOfBar = "Bar"; var dispatcherThread = new DispatcherThread(Dispatcher.Run); dispatcherThread.Start(); var ipcServer = new IpcServer <IFoo>(fooStub, dispatcherThread.Dispatcher, IPAddress.Loopback, 62000, new JsonDotNetSerializer()); ipcServer.Listen(); var ipcClient = new IpcClient <IFoo>(IPAddress.Loopback, 62000, new JsonDotNetSerializer()); object response; ipcClient.Proxy.Foo(); Assert.AreEqual(1, fooStub.NumberOfFooCalls); response = ipcClient.Proxy.Bar(); Assert.AreEqual(fooStub.ReturnValueOfBar, response); Assert.AreEqual(1, fooStub.NumberOfBarCalls); ipcClient.Dispose(); ipcServer.Dispose(); dispatcherThread.Shutdown(); }
public void TestFooStubReturnsTheAnswerToEverything() { IAsyncFoo sut = new FooStub(); var t = sut.CountBytesAsync(new HttpClient(), "http://www.slayer.net"); Assert.Equal(42, t.Result); }
public async Task HttpClientPolicy_CombinePolicies_BehavesAccordingToPolicy() { var retryCount = 3; var exceptionAllowedBeforeBreaking = retryCount + 2; var circuitBreakDurationInMs = 10000; var timeoutInSeconds = 25; var foo = new FooStub(); var policy = HttpClientPollyPolicy.Initialise() .AddWaitRetryPolicy(retryCount) .AddFallbackPolicy(() => foo.FakeCallback()) .AddCircuitBreakerPolicy(exceptionAllowedBeforeBreaking, TimeSpan.FromMilliseconds(circuitBreakDurationInMs)) .AddTimeoutPolicy(timeoutInSeconds) .Build(); Func <bool, int?, Task <HttpResponseMessage> > barAsync = (bool returnFailureMessage, int?fakeTimeoutInSeconds) => policy.ExecuteAsync(() => foo.BarAsync(returnFailureMessage, fakeTimeoutInSeconds)); var result = await barAsync(true, null); int barAsyncMethodInvocationCount = retryCount + 1; Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode); result = await barAsync(true, null); barAsyncMethodInvocationCount++; int callbackMethodInvocationCount = 3; Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode); await Task.Delay(circuitBreakDurationInMs); //Return unhandled error code to transition circuit from half-open to close result = await barAsync(false, null); barAsyncMethodInvocationCount++; Assert.Equal(HttpStatusCode.OK, result.StatusCode); result = await barAsync(true, null); barAsyncMethodInvocationCount += retryCount + 1; Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode); result = await barAsync(true, timeoutInSeconds); barAsyncMethodInvocationCount++; callbackMethodInvocationCount++; Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode); Assert.Equal(callbackMethodInvocationCount, foo.FallbackCount); Assert.Equal(barAsyncMethodInvocationCount, foo.BarAsyncCount); }
public void VerifyPerformance() { var fooStub = new FooStub(); fooStub.ReturnValueOfBar = "Bar"; var dispatcherThread = new DispatcherThread(Dispatcher.Run); dispatcherThread.Start(); var ipcServer = new IpcServer <IFoo>(fooStub, dispatcherThread.Dispatcher, IPAddress.Loopback, 62000, new JsonDotNetSerializer()); ipcServer.Listen(); var ipcClient = new IpcClient <IFoo>(IPAddress.Loopback, 62000, new JsonDotNetSerializer()); var numberOfCalls = 1000; var nanoSecs = Performance.MeasureMs(() => { for (var i = 0; i < numberOfCalls; i++) { ipcClient.Proxy.Foo(); } }); var fooAverageMs = nanoSecs / numberOfCalls; Console.WriteLine("Foo call took: {0}ms", fooAverageMs); Assert.AreEqual(numberOfCalls, fooStub.NumberOfFooCalls); Assert.LessOrEqual(TimeSpan.FromMilliseconds(fooAverageMs), TimeSpan.FromMilliseconds(10)); nanoSecs = Performance.MeasureMs(() => { for (var i = 0; i < numberOfCalls; i++) { ipcClient.Proxy.Bar(); } }); var barAverageMs = nanoSecs / numberOfCalls; Console.WriteLine("Bar call took: {0}ms", barAverageMs); Assert.AreEqual(numberOfCalls, fooStub.NumberOfBarCalls); Assert.LessOrEqual(TimeSpan.FromMilliseconds(barAverageMs), TimeSpan.FromMilliseconds(10)); ipcClient.Dispose(); ipcServer.Dispose(); dispatcherThread.Shutdown(); }
public void Setup() { _fooStub = new FooStub(); }