private async Task Timeout_Test(int timeoutMs, int waitBeforeAssertTimeoutMs) { ulong id = 25; var responding = new Queue <RpcMessage>(); int responseTimeMs = 500; var connection = new RespondingMockRpcConnection(responding, responseTimeMs); // Start channel var channel = await RpcChannel.Create(new RpcPeerInfo(null, "localhost"), connection, new MockRpcMethodExecutor(), backlog : null); _ = channel.Start(); var callTask = channel.Run(new RpcCall { Method = new RpcMethod { ID = id, Name = "MyMethod" }, TimeoutMs = timeoutMs }); // When we give more than 200 ms time to fail, check already after // 100 ms, then the task should still be open if (waitBeforeAssertTimeoutMs > 200) { await Task.Delay(100); waitBeforeAssertTimeoutMs -= 100; Assert.IsFalse(callTask.IsCompleted); } // Wait a short moment, the method call must have failed await Task.Delay(waitBeforeAssertTimeoutMs); Assert.IsTrue(callTask.IsCompleted); Assert.AreEqual(RpcFailureType.Timeout, callTask.Result.Failure?.Type); channel.Stop(); }
public async Task Run_And_ReceiveResult_Test() { // Result to receive ulong id = 25; var responding = new Queue <RpcMessage>(); responding.Enqueue(RpcMessage.Encode(new RpcResult { MethodID = id, ReturnValue = new byte[] { 42 } })); int responseTimeMs = 500; var connection = new RespondingMockRpcConnection(responding, responseTimeMs); // Start channel var channel = await RpcChannel.Create(new RpcPeerInfo(null, "localhost"), connection, new MockRpcMethodExecutor(), backlog : null); _ = channel.Start(); var callTask = channel.Run(new RpcCall { Method = new RpcMethod { ID = id, Name = "MyMethod" } }); // Wait shorter than the response time, nothing should be received yet await Task.Delay(responseTimeMs / 2); Assert.IsFalse(callTask.IsCompleted); // Give remaining time to execute await Task.Delay(responseTimeMs / 2 + 200); // Check received message Assert.IsTrue(callTask.IsCompleted); Assert.AreEqual(new RpcResult { MethodID = id, ReturnValue = new byte[] { 42 } }, callTask.Result); channel.Stop(); }