public void HandleBlockingRequestArgsNoReturn() { const int num = 42; var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.BlockingProcedureNoReturn(It.IsAny <int> ())) .Callback((int n) => BlockingProcedureNoReturnFn(n)); TestService.Service = mock.Object; var request = Req("TestService", "BlockingProcedureNoReturn", Arg(0, ProtocolBuffers.WriteValue(num, typeof(int)))); BlockingProcedureNoReturnFnCount = 0; Response response = null; Continuation <Response> continuation = new RequestContinuation(null, request); while (response == null) { try { response = continuation.Run(); } catch (YieldException e) { continuation = (Continuation <Response>)e.Continuation; } } response.Time = 0; Assert.AreEqual("", response.Error); // Verify the KRPCProcedure is called once, but the handler function is called multiple times mock.Verify(x => x.BlockingProcedureNoReturn(It.IsAny <int> ()), Times.Once()); Assert.AreEqual(num + 1, BlockingProcedureNoReturnFnCount); }
public void HandleBlockingRequestArgsReturns() { const int num = 10; const int expectedResult = 55; var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.BlockingProcedureReturns(It.IsAny <int> (), It.IsAny <int> ())) .Returns((int n, int sum) => BlockingProcedureReturnsFn(n, sum)); TestService.Service = mock.Object; var request = Req("TestService", "BlockingProcedureReturns", Arg(0, num)); BlockingProcedureReturnsFnCount = 0; Response response = null; Continuation <Response> continuation = new RequestContinuation(null, request); while (response == null) { try { response = continuation.Run(); } catch (YieldException e) { continuation = (Continuation <Response>)e.Continuation; } } response.Time = 0; Assert.AreEqual(string.Empty, response.Error); Assert.AreEqual(expectedResult, (int)response.ReturnValue); // Verify the KRPCProcedure is called once, but the handler function is called multiple times mock.Verify(x => x.BlockingProcedureReturns(It.IsAny <int> (), It.IsAny <int> ()), Times.Once()); Assert.AreEqual(num + 1, BlockingProcedureReturnsFnCount); }
void ExecuteContinuation(RequestContinuation continuation) { var client = continuation.Client; // Run the continuation, and either return a result, an error, // or throw a YieldException if the continuation has not completed Response response; try { CallContext.Set(client); response = continuation.Run(); } catch (YieldException) { throw; } catch (RPCException e) { response = new Response(); response.HasError = true; response.Error = e.Message; if (Logger.ShouldLog(Logger.Severity.Debug)) { Logger.WriteLine(response.Error, Logger.Severity.Debug); } } catch (Exception e) { response = new Response(); response.HasError = true; response.Error = e.Message + Environment.NewLine + e.StackTrace; if (Logger.ShouldLog(Logger.Severity.Debug)) { Logger.WriteLine(response.Error, Logger.Severity.Debug); } } finally { CallContext.Clear(); } // Send response to the client response.Time = GetUniversalTime(); client.Stream.Write(response); if (Logger.ShouldLog(Logger.Severity.Debug)) { if (response.HasError) { Logger.WriteLine("Sent error response to client " + client.Address + " (" + response.Error + ")", Logger.Severity.Debug); } else { Logger.WriteLine("Sent response to client " + client.Address, Logger.Severity.Debug); } } }
static void ExecuteContinuation(RequestContinuation continuation) { var client = continuation.Client; // Run the continuation, and either return a result, an error, // or throw a YieldException if the continuation has not completed Response response; try { CallContext.Set(client); response = continuation.Run(); } catch (YieldException) { throw; } catch (RPCException e) { response = new Response { Error = HandleException(e) }; } catch (System.Exception e) { response = new Response { Error = HandleException(e) }; } finally { CallContext.Clear(); } // Send response to the client try { client.Stream.Write(response); if (Logger.ShouldLog(Logger.Severity.Debug)) { if (response.HasError) { Logger.WriteLine("Sent error response to client " + client.Address + " (" + response.Error + ")", Logger.Severity.Debug); } else { Logger.WriteLine("Sent response to client " + client.Address, Logger.Severity.Debug); } } } catch (ServerException exn) { Logger.WriteLine("Failed to send response to client " + client.Address + Environment.NewLine + exn, Logger.Severity.Error); } }
void ExecuteContinuation (RequestContinuation continuation) { var client = continuation.Client; // Run the continuation, and either return a result, an error, // or throw a YieldException if the continuation has not completed Response response; try { CallContext.Set (client); response = continuation.Run (); } catch (YieldException) { throw; } catch (RPCException e) { response = new Response (); response.HasError = true; response.Error = e.Message; if (Logger.ShouldLog (Logger.Severity.Debug)) Logger.WriteLine (response.Error, Logger.Severity.Debug); } catch (Exception e) { response = new Response (); response.HasError = true; response.Error = e.Message + Environment.NewLine + e.StackTrace; if (Logger.ShouldLog (Logger.Severity.Debug)) Logger.WriteLine (response.Error, Logger.Severity.Debug); } finally { CallContext.Clear (); } // Send response to the client response.Time = GetUniversalTime (); client.Stream.Write (response); if (Logger.ShouldLog (Logger.Severity.Debug)) { if (response.HasError) Logger.WriteLine ("Sent error response to client " + client.Address + " (" + response.Error + ")", Logger.Severity.Debug); else Logger.WriteLine ("Sent response to client " + client.Address, Logger.Severity.Debug); } }
public void HandleBlockingRequestArgsReturns () { const int num = 10; const int expectedResult = 55; var mock = new Mock<ITestService> (MockBehavior.Strict); mock.Setup (x => x.BlockingProcedureReturns (It.IsAny<int> (), It.IsAny<int> ())) .Returns ((int n, int sum) => BlockingProcedureReturnsFn (n, sum)); TestService.Service = mock.Object; var request = Req ("TestService", "BlockingProcedureReturns", Arg (0, num)); BlockingProcedureReturnsFnCount = 0; Response response = null; Continuation<Response> continuation = new RequestContinuation (null, request); while (response == null) { try { response = continuation.Run (); } catch (YieldException e) { continuation = (Continuation<Response>)e.Continuation; } } response.Time = 0; Assert.AreEqual (String.Empty, response.Error); Assert.AreEqual (expectedResult, (int)response.ReturnValue); // Verify the KRPCProcedure is called once, but the handler function is called multiple times mock.Verify (x => x.BlockingProcedureReturns (It.IsAny<int> (), It.IsAny<int> ()), Times.Once ()); Assert.AreEqual (num + 1, BlockingProcedureReturnsFnCount); }
/// <summary> /// Execute the continuation and send a response to the client, /// or throw a YieldException if the continuation is not complete. /// </summary> void ExecuteContinuation(RequestContinuation continuation) { var client = continuation.Client; // Run the continuation, and either return a result, an error, // or throw a YieldException if the continuation has not completed Response.Builder response; try { Context.Set (this, client); response = continuation.Run (); } catch (YieldException) { throw; } catch (Exception e) { response = Response.CreateBuilder (); response.Error = e.Message; if (Logger.ShouldLog (Logger.Severity.Debug)) Logger.WriteLine (e.Message, Logger.Severity.Debug); } finally { Context.Clear (); } // Send response to the client response.SetTime (GetUniversalTime ()); var builtResponse = response.Build (); ((RPCClient)client).Stream.Write (builtResponse); if (Logger.ShouldLog (Logger.Severity.Debug)) { if (response.HasError) Logger.WriteLine ("Sent error response to client " + client.Address + " (" + response.Error + ")", Logger.Severity.Debug); else Logger.WriteLine ("Sent response to client " + client.Address, Logger.Severity.Debug); } }