Exemple #1
0
        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, ProtocolBuffers.WriteValue(num, typeof(int))));

            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("", response.Error);
            Assert.AreEqual(expectedResult, ProtocolBuffers.ReadValue(response.ReturnValue, typeof(int)));
            // 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);
        }
Exemple #2
0
 /// <summary>
 /// Decode a serialized value
 /// </summary>
 object Decode(ProcedureSignature procedure, int i, Type type, ByteString value)
 {
     if (TypeUtils.IsAClassType(type))
     {
         return(ObjectStore.Instance.GetInstance((ulong)ProtocolBuffers.ReadValue(value, typeof(ulong))));
     }
     else if (TypeUtils.IsACollectionType(type))
     {
         return(DecodeCollection(procedure, i, type, value));
     }
     else if (ProtocolBuffers.IsAMessageType(type))
     {
         return(ProtocolBuffers.ParseFrom(type, value));
     }
     else if (TypeUtils.IsAnEnumType(type))
     {
         // TODO: Assumes it's underlying type is int
         var enumValue = ProtocolBuffers.ReadValue(value, typeof(int));
         if (!Enum.IsDefined(type, enumValue))
         {
             throw new RPCException(procedure, "Failed to convert value " + enumValue + " to enumeration type " + type);
         }
         return(Enum.ToObject(type, enumValue));
     }
     else
     {
         return(ProtocolBuffers.ReadValue(value, type));
     }
 }
Exemple #3
0
        public void HandleRequestForClassStaticMethod()
        {
            ByteString argBytes = ProtocolBuffers.WriteValue("bob", typeof(string));
            var        request  = Req("TestService", "TestClass_StaticMethod", Arg(0, argBytes));
            var        response = Run(request);

            response.Time = 42;
            Assert.AreEqual("jebbob", ProtocolBuffers.ReadValue(response.ReturnValue, typeof(string)));
        }
Exemple #4
0
        public void HandleRequestForPropertyGetter()
        {
            var mock = new Mock <ITestService> (MockBehavior.Strict);

            mock.Setup(x => x.PropertyWithGet).Returns("foo");
            TestService.Service = mock.Object;
            Response response = Run(Req("TestService", "get_PropertyWithGet"));

            Assert.AreEqual("foo", ProtocolBuffers.ReadValue(response.ReturnValue, typeof(string)));
            mock.Verify(x => x.PropertyWithGet, Times.Once());
        }
Exemple #5
0
        public void HandleRequestForObjectMethod()
        {
            var        instance  = new TestService.TestClass("jeb");
            var        guid      = ObjectStore.Instance.AddInstance(instance);
            ByteString guidBytes = ProtocolBuffers.WriteValue(guid, typeof(ulong));
            ByteString argBytes  = ProtocolBuffers.WriteValue(3.14159f, typeof(float));
            var        request   = Req("TestService", "TestClass_FloatToString", Arg(0, guidBytes), Arg(1, argBytes));
            var        response  = Run(request);

            response.Time = 42;
            Assert.AreEqual("jeb3.14159", ProtocolBuffers.ReadValue(response.ReturnValue, typeof(string)));
        }
Exemple #6
0
        public void HandleRequestWithClassTypeReturnFromDifferentService()
        {
            var request = Req("TestService2", "ClassTypeFromOtherServiceAsReturn",
                              Arg(0, ProtocolBuffers.WriteValue("jeb", typeof(string))));
            var response = Run(request);

            response.Time = 0;
            Assert.AreEqual("", response.Error);
            var guid = (ulong)ProtocolBuffers.ReadValue(response.ReturnValue, typeof(ulong));
            var obj  = (TestService.TestClass)ObjectStore.Instance.GetInstance(guid);

            Assert.AreEqual("jeb", obj.value);
        }
Exemple #7
0
 public void ReadValue()
 {
     Assert.AreEqual(3.14159, ProtocolBuffers.ReadValue(FromHex("6E861BF0F9210940"), typeof(double)));
     Assert.AreEqual(3.14159f, ProtocolBuffers.ReadValue(FromHex("D00F4940"), typeof(float)));
     Assert.AreEqual(-42, ProtocolBuffers.ReadValue(FromHex("D6FFFFFFFFFFFFFFFF01"), typeof(int)));
     Assert.AreEqual(-42834463454L, ProtocolBuffers.ReadValue(FromHex("A2EAF7B6E0FEFFFFFF01"), typeof(long)));
     Assert.AreEqual(42, ProtocolBuffers.ReadValue(FromHex("2A"), typeof(uint)));
     Assert.AreEqual(42834463454UL, ProtocolBuffers.ReadValue(FromHex("DE9588C99F01"), typeof(ulong)));
     Assert.AreEqual(true, ProtocolBuffers.ReadValue(FromHex("01"), typeof(bool)));
     Assert.AreEqual(false, ProtocolBuffers.ReadValue(FromHex("00"), typeof(bool)));
     Assert.AreEqual("jebediah", ProtocolBuffers.ReadValue(FromHex("086A65626564696168"), typeof(string)));
     Assert.AreEqual(new byte[] { 0xBA, 0xDA, 0x55 }, ProtocolBuffers.ReadValue(FromHex("03BADA55"), typeof(byte[])));
     Assert.Throws <ArgumentException> (() => ProtocolBuffers.ReadValue(FromHex(""), typeof(bool)));
 }
Exemple #8
0
        public void HandleRequestWithClassTypeParameterFromDifferentService()
        {
            var instance = new TestService.TestClass("jeb");

            instance.IntProperty = 42;
            var        guid      = ObjectStore.Instance.AddInstance(instance);
            ByteString guidBytes = ProtocolBuffers.WriteValue(guid, typeof(ulong));
            var        request   = Req("TestService2", "ClassTypeFromOtherServiceAsParameter", Arg(0, guidBytes));
            var        response  = Run(request);

            response.Time = 0;
            Assert.AreEqual("", response.Error);
            Assert.AreEqual(42, ProtocolBuffers.ReadValue(response.ReturnValue, typeof(long)));
        }
Exemple #9
0
        public void HandleRequestForClassPropertyGetter()
        {
            var instance = new TestService.TestClass("jeb");

            instance.IntProperty = 42;
            var        guid      = ObjectStore.Instance.AddInstance(instance);
            ByteString guidBytes = ProtocolBuffers.WriteValue(guid, typeof(ulong));
            var        request   = Req("TestService", "TestClass_get_IntProperty", Arg(0, guidBytes));
            var        response  = Run(request);

            response.Time = 0;
            Assert.AreEqual("", response.Error);
            Assert.AreEqual(42, ProtocolBuffers.ReadValue(response.ReturnValue, typeof(int)));
        }
Exemple #10
0
        public void HandleRequestForObjectMethodWithObjectParameter()
        {
            var        instance    = new TestService.TestClass("bill");
            var        argInstance = new TestService.TestClass("bob");
            var        guid        = ObjectStore.Instance.AddInstance(instance);
            var        argGuid     = ObjectStore.Instance.AddInstance(argInstance);
            ByteString guidBytes   = ProtocolBuffers.WriteValue(guid, typeof(ulong));
            ByteString argBytes    = ProtocolBuffers.WriteValue(argGuid, typeof(ulong));
            var        request     = Req("TestService", "TestClass_ObjectToString", Arg(0, guidBytes), Arg(1, argBytes));
            var        response    = Run(request);

            response.Time = 42;
            Assert.AreEqual("billbob", ProtocolBuffers.ReadValue(response.ReturnValue, typeof(string)));
        }
Exemple #11
0
        public void HandleRequestWithNullObjectParameterAndReturn()
        {
            ByteString argBytes = ProtocolBuffers.WriteValue(0ul, typeof(ulong));
            var        mock     = new Mock <ITestService> (MockBehavior.Strict);

            mock.Setup(x => x.EchoTestObject(It.IsAny <TestService.TestClass> ()))
            .Callback((TestService.TestClass x) => Assert.AreEqual(null, x))
            .Returns((TestService.TestClass x) => x);
            TestService.Service = mock.Object;
            Response response = Run(Req("TestService", "EchoTestObject", Arg(0, argBytes)));

            Assert.AreEqual("", response.Error);
            response.Time = 42;
            Assert.IsNotNull(response.ReturnValue);
            Assert.AreEqual(0, ProtocolBuffers.ReadValue(response.ReturnValue, typeof(ulong)));
        }
Exemple #12
0
        public void HandleRequestWithObjectReturn()
        {
            var instance = new TestService.TestClass("foo");
            var guid     = ObjectStore.Instance.AddInstance(instance);
            var argBytes = ProtocolBuffers.WriteValue("foo", typeof(string));
            var mock     = new Mock <ITestService> (MockBehavior.Strict);

            mock.Setup(x => x.CreateTestObject("foo")).Returns(instance);
            TestService.Service = mock.Object;
            Response response = Run(Req("TestService", "CreateTestObject", Arg(0, argBytes)));

            Assert.AreEqual("", response.Error);
            response.Time = 42;
            Assert.IsNotNull(response.ReturnValue);
            Assert.AreEqual(guid, ProtocolBuffers.ReadValue(response.ReturnValue, typeof(ulong)));
        }
Exemple #13
0
 public void ReadWriteValues()
 {
     Assert.AreEqual(3.14159, ProtocolBuffers.ReadValue(ProtocolBuffers.WriteValue(3.14159, typeof(double)), typeof(double)));
     Assert.AreEqual(3.14159f, ProtocolBuffers.ReadValue(ProtocolBuffers.WriteValue(3.14159f, typeof(float)), typeof(float)));
     Assert.AreEqual(-42, ProtocolBuffers.ReadValue(ProtocolBuffers.WriteValue(-42, typeof(int)), typeof(int)));
     Assert.AreEqual(-42834463454, ProtocolBuffers.ReadValue(ProtocolBuffers.WriteValue(-42834463454L, typeof(long)), typeof(long)));
     Assert.AreEqual(42U, ProtocolBuffers.ReadValue(ProtocolBuffers.WriteValue(42U, typeof(uint)), typeof(uint)));
     Assert.AreEqual(42834463454UL, ProtocolBuffers.ReadValue(ProtocolBuffers.WriteValue(42834463454UL, typeof(ulong)), typeof(ulong)));
     Assert.AreEqual(true, ProtocolBuffers.ReadValue(ProtocolBuffers.WriteValue(true, typeof(bool)), typeof(bool)));
     Assert.AreEqual(false, ProtocolBuffers.ReadValue(ProtocolBuffers.WriteValue(false, typeof(bool)), typeof(bool)));
     Assert.AreEqual("jebediah", ProtocolBuffers.ReadValue(ProtocolBuffers.WriteValue("jebediah", typeof(string)), typeof(string)));
     Assert.AreEqual(new byte[] { 0xBA, 0xDA, 0x55 }, ProtocolBuffers.ReadValue(ProtocolBuffers.WriteValue(new byte[] {
         0xBA,
         0xDA,
         0x55
     }, typeof(byte[])), typeof(byte[])));
 }