public void SuccessfulTaskCallsCallResultWithResult(WampRpcCall call, object result)
        {
            MockRawFormatter formatter = new MockRawFormatter();

            MockRpcCatalog catalog = new MockRpcCatalog();

            call.CallId = Guid.NewGuid().ToString();

            MockRpcMethod mockMethod = GetMockMethod(call);

            mockMethod.Result = result;

            catalog.MapMethod(mockMethod);

            WampRpcServer<MockRaw> server =
                new WampRpcServer<MockRaw>(formatter, catalog);

            MockClient client = new MockClient();

            server.Call(client, call.CallId, call.ProcUri,
                        SerializeArguments(call, formatter));

            Assert.That(client.GetCallErrorByCallId(call.CallId),
                        Is.Null);

            Assert.That(client.GetResultByCallId(call.CallId),
                        Is.EqualTo(result));
        }
        public WampRpcCall Serialize(MethodInfo method, object[] arguments)
        {
            WampRpcCall result = new WampRpcCall()
                                             {
                                                 Arguments =  arguments,
                                                 ProcUri =  mProcUriMapper.Map(method),
                                             };

            result.ReturnType = ExtractReturnType(method.ReturnType);

            return result;
        }
        public void HandleAsync_ClientCallError_SetsTasksException(WampRpcCall rpcCall, CallErrorDetails callErrorDetails)
        {
            MockWampRpcCallManager<MockRaw> callManager =
                new MockWampRpcCallManager<MockRaw>();

            IWampRpcClientHandler handler =
                GetHandler(client => callManager.GetServer(client));

            Task<object> task = handler.HandleAsync(rpcCall);

            MockWampRpcCallDetails<MockRaw> details =
                callManager.GetCallDetails(rpcCall.CallId);

            Assert.IsFalse(task.IsCompleted);

            object errorDetails = callErrorDetails.ErrorDetails;

            if (errorDetails == null)
            {
                details.Client.CallError(rpcCall.CallId,
                                         callErrorDetails.ErrorUri,
                                         callErrorDetails.ErrorDesc);
            }
            else
            {
                details.Client.CallError(rpcCall.CallId,
                                         callErrorDetails.ErrorUri,
                                         callErrorDetails.ErrorDesc,
                                         new MockRaw(errorDetails));
            }

            AggregateException aggregatedException = task.Exception;
            Assert.IsNotNull(aggregatedException);

            Exception innerException = aggregatedException.InnerException;

            Assert.That(innerException, Is.TypeOf(typeof(WampRpcCallException)));

            WampRpcCallException casted = innerException as WampRpcCallException;
            Assert.That(casted.Message, Is.EqualTo(callErrorDetails.ErrorDesc));
            Assert.That(casted.CallId, Is.EqualTo(rpcCall.CallId));
            Assert.That(casted.ErrorUri, Is.EqualTo(callErrorDetails.ErrorUri));
            Assert.That(casted.ErrorDetails,
                        Is.EqualTo(errorDetails)
                          .Using(StructuralComparisons.StructuralEqualityComparer));
        }
        public void HandleAsync_Calls_ServerProxyCall(WampRpcCall rpcCall)
        {
            MockWampRpcCallManager<MockRaw> callManager =
                new MockWampRpcCallManager<MockRaw>();

            IWampRpcClientHandler handler =
                GetHandler(client => callManager.GetServer(client));

            Assert.That(callManager.AllCalls, Is.Empty);

            Task<object> task = handler.HandleAsync(rpcCall);

            MockWampRpcCallDetails<MockRaw> details =
                callManager.GetCallDetails(rpcCall.CallId);

            Assert.IsNotNull(details);

            Assert.That(details.CallId, Is.EqualTo(rpcCall.CallId));
            Assert.That(details.ProcUri, Is.EqualTo(rpcCall.ProcUri));
            CollectionAssert.AreEqual(rpcCall.Arguments, details.Arguments);
        }
        public void ErrorTaskCallsCallErrorWithError(WampRpcCall call, CallErrorDetails details)
        {
            MockRawFormatter formatter = new MockRawFormatter();

            MockRpcCatalog catalog = new MockRpcCatalog();

            call.CallId = Guid.NewGuid().ToString();

            MockRpcMethod mockMethod = GetMockMethod(call);

            mockMethod.Error =
                new WampRpcCallException(details.ErrorUri,
                                         details.ErrorDesc,
                                         details.ErrorDetails);

            catalog.MapMethod(mockMethod);

            WampRpcServer<MockRaw> server =
                new WampRpcServer<MockRaw>(formatter, catalog);

            MockClient client = new MockClient();

            server.Call(client, call.CallId, call.ProcUri,
                        SerializeArguments(call, formatter));

            Assert.That(client.GetResultByCallId(call.CallId),
                        Is.Null);

            CallErrorDetails error = client.GetCallErrorByCallId(call.CallId);

            Assert.That(error.ErrorDesc,
                        Is.EqualTo(details.ErrorDesc));

            Assert.That(error.ErrorDetails,
                        Is.EqualTo(details.ErrorDetails));

            Assert.That(error.ErrorUri,
                        Is.EqualTo(details.ErrorUri));
        }
        public void HandleAsync_ClientCallResult_SetsTasksResult(WampRpcCall rpcCall, object result)
        {
            MockWampRpcCallManager<MockRaw> callManager =
                new MockWampRpcCallManager<MockRaw>();

            IWampRpcClientHandler handler =
                GetHandler(client => callManager.GetServer(client));

            if (result != null)
            {
                rpcCall.ReturnType = result.GetType();
            }

            Task<object> task = handler.HandleAsync(rpcCall);

            MockWampRpcCallDetails<MockRaw> details =
                callManager.GetCallDetails(rpcCall.CallId);

            Assert.IsFalse(task.IsCompleted);

            details.Client.CallResult(rpcCall.CallId, new MockRaw(result));

            Assert.That(task.Result, Is.EqualTo(result));
        }
        public void HandleAsync_ClientCall_TaskIsAsync()
        {
            MockWampRpcCallManager<MockRaw> callManager =
                new MockWampRpcCallManager<MockRaw>();

            IWampRpcClientHandler handler =
                GetHandler(client => callManager.GetServer(client));

            // call a function that takes a long time, call another function
            // the result of the latter is received first, in other words,
            // RPC is really asynchronous
            var slowCall = new WampRpcCall()
                               {
                                   Arguments = new object[] {new int[] {1, 2, 3}},
                                   ProcUri = "calc:asum",
                                   ReturnType = typeof (int)
                               };

            var fastCall = new WampRpcCall()
                               {
                                   Arguments = new object[] {new int[] {4, 5, 6}},
                                   ProcUri = "calc:sum",
                                   ReturnType = typeof (int)
                               };

            Task<object> slowTask = handler.HandleAsync(slowCall);

            Task<object> fastTask = handler.HandleAsync(fastCall);

            MockWampRpcCallDetails<MockRaw> slowCallDetails =
                callManager.GetCallDetails(slowCall.CallId);

            MockWampRpcCallDetails<MockRaw> fastCallDetails =
                callManager.GetCallDetails(fastCall.CallId);

            Assert.IsFalse(slowTask.IsCompleted);
            Assert.IsFalse(fastTask.IsCompleted);

            fastCallDetails.Client.CallResult(fastCall.CallId, new MockRaw(15));

            Assert.That(fastTask.Result, Is.EqualTo(15));
            Assert.IsFalse(slowTask.IsCompleted);

            slowCallDetails.Client.CallResult(slowCall.CallId, new MockRaw(6));
            Assert.That(slowTask.Result, Is.EqualTo(6));
        }
 private static MockRpcMethod GetMockMethod(WampRpcCall call)
 {
     return new MockRpcMethod()
         {
             ProcUri = call.ProcUri,
             Parameters = call.Arguments.Select
                 (x => GetType(x)).ToArray()
         };
 }
 private static MockRaw[] SerializeArguments(WampRpcCall call, MockRawFormatter formatter)
 {
     return call.Arguments.Select(x => formatter.Serialize(x)).ToArray();
 }