public int ProcessOperation(int value1, int value2, Operation operation) { switch (operation) { case Operation.Sum: return(_sumService.Sum(value1, value2)); case Operation.Subtract: return(_subtractionService.Subtract(value1, value2)); } throw new InvalidOperationException(); }
public void TestMockingSumService() { var value1 = 10; var value2 = 10; var operation = Operation.Sum; // Here the expected value is different from the sum between value1 and value2 just to show // Sum() will not be called actually. The mocked object will return a preprogramed value. var expectedValue = -1; _calculatorService = new CalculatorService(_mockedSumService, _subtractionService); // For any values received as parameter, Sum() will return the expectedValue. _mockedSumService.Sum(Arg.Any <int>(), Arg.Any <int>()) .ReturnsForAnyArgs(expectedValue); var result = _calculatorService.ProcessOperation(value1, value2, operation); result.ShouldBe(expectedValue, $"Wrong answer! Result should be {expectedValue}, {result} was found."); // Asserts Sum() was called once. _mockedSumService.Received(1).Sum(Arg.Any <int>(), Arg.Any <int>()); }
public void Benchmark() { var serializer = new JsonCommonSerializer(); // new ProtobufCommonSerializer();// int port = -1; Poller client = null, server = null; var serverThread = RunServer <ISumService>(serializer, p => port = p, p => server = p, new SumService()); while (port == -1) { Thread.Yield(); } ISumService proxy = null; var clientThread = RunClient <ISumService>(serializer, port, p => client = p, p => proxy = p); while (proxy == null) { Thread.Yield(); } const int randCnt = 100; var rand = new Random(42); var randoms = new int[randCnt]; for (int i = 0; i < randCnt; i++) { randoms[i] = rand.Next(10000000, 20000000); } var sw = new Stopwatch(); long timeFromClient = 0, timeToClient = 0; const int cnt = 1000; for (int j = 0; j < cnt; j++) { sw.Start(); var sum = proxy.Sum(randoms).Result; sw.Stop(); Assert.Equal(randoms.Sum(), sum); for (int i = 0; i < randCnt; i++) { randoms[i] = rand.Next(10000000, 20000000); } var times = proxy.TimeDiff(Stopwatch.GetTimestamp()).Result; timeFromClient += times.Item1; timeToClient += Stopwatch.GetTimestamp() - times.Item2; } _testOutputHelper.WriteLine("Completed {0} sum passes in {1}ms", cnt, sw.ElapsedMilliseconds); _testOutputHelper.WriteLine("Client to server latency: {0}us", timeFromClient / cnt / 10); _testOutputHelper.WriteLine("Server to client latency: {0}us", timeToClient / cnt / 10); sw.Reset(); var tree = new SumServiceTree(); SumServiceTree.FillTree(tree, rand, 2); _testOutputHelper.WriteLine("Starting large message transfer."); sw.Start(); var result = proxy.Increment(tree).Result; sw.Stop(); Assert.True(tree.IsExactMatch(result, 1)); _testOutputHelper.WriteLine("Completed large transfer in {0}ms", sw.Elapsed.TotalMilliseconds); client.CancelAndJoin(); server.CancelAndJoin(); clientThread.Wait(); serverThread.Wait(); }