public void HandleEchoDictionary() { var dictionary = new Dictionary(); dictionary.Entries.Add(new DictionaryEntry { Key = ProtocolBuffers.WriteValue(0, typeof(int)), Value = ProtocolBuffers.WriteValue("jeb", typeof(string)) }); dictionary.Entries.Add(new DictionaryEntry { Key = ProtocolBuffers.WriteValue(1, typeof(int)), Value = ProtocolBuffers.WriteValue("bob", typeof(string)) }); dictionary.Entries.Add(new DictionaryEntry { Key = ProtocolBuffers.WriteValue(2, typeof(int)), Value = ProtocolBuffers.WriteValue("bill", typeof(string)) }); var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.EchoDictionary(It.IsAny <IDictionary <int, string> > ())) .Returns((IDictionary <int, string> x) => x); TestService.Service = mock.Object; var response = Run(Req("TestService", "EchoDictionary", Arg(0, ProtocolBuffers.WriteMessage(dictionary)))); response.Time = 0; Assert.AreEqual("", response.Error); Assert.AreEqual(ProtocolBuffers.WriteMessage(dictionary), response.ReturnValue); mock.Verify(x => x.EchoDictionary(It.IsAny <IDictionary <int, string> > ()), Times.Once()); }
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); }
public ParameterSignature(string fullProcedureName, ProcedureParameter parameter) { Name = parameter.Name; Type = parameter.Type; // Check the parameter type is valid if (!TypeUtils.IsAValidType(Type)) { throw new ServiceException(Type + " is not a valid Procedure parameter type, in " + fullProcedureName); } // Encode the default value as a ByteString if (parameter.HasDefaultValue) { var value = parameter.DefaultValue; if (TypeUtils.IsAClassType(Type)) { DefaultArgument = ProtocolBuffers.WriteValue(ObjectStore.Instance.AddInstance(value), typeof(ulong)); } else if (TypeUtils.IsAnEnumType(Type)) { // TODO: Assumes it's underlying type is int DefaultArgument = ProtocolBuffers.WriteValue((int)value, typeof(int)); } else if (ProtocolBuffers.IsAMessageType(Type)) { DefaultArgument = ProtocolBuffers.WriteMessage(value as IMessage); } else { DefaultArgument = ProtocolBuffers.WriteValue(value, Type); } } }
public void HandleRequestThreeOptionalArgs() { const float arg0 = 3.14159f; const int arg2 = 42; var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.ProcedureThreeOptionalArgsNoReturn( It.IsAny <float> (), It.IsAny <string> (), It.IsAny <int> ())) .Callback((float x, string y, int z) => { Assert.AreEqual(arg0, x); Assert.AreEqual("jeb", y); Assert.AreEqual(arg2, z); }); TestService.Service = mock.Object; var request = Req("TestService", "ProcedureThreeOptionalArgsNoReturn", Arg(2, ProtocolBuffers.WriteValue(arg2, arg2.GetType())), Arg(0, ProtocolBuffers.WriteValue(arg0, arg0.GetType()))); Run(request); mock.Verify(x => x.ProcedureThreeOptionalArgsNoReturn( It.IsAny <float> (), It.IsAny <string> (), It.IsAny <int> ()), Times.Once()); }
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))); }
public void HandleRequestSingleInvalidEnumArgNoReturn() { const int arg = 9999; var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.ProcedureEnumArg(It.IsAny <TestService.TestEnum> ())); TestService.Service = mock.Object; var request = Req("TestService", "ProcedureTestEnumArg", Arg(0, ProtocolBuffers.WriteValue((int)arg, typeof(int)))); Assert.Throws <RPCException> (() => Run(request)); }
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))); }
public void HandleRequestForPropertySetter() { var mock = new Mock <ITestService> (MockBehavior.Strict); mock.SetupSet(x => x.PropertyWithSet = "foo"); TestService.Service = mock.Object; var request = Req("TestService", "set_PropertyWithSet", Arg(0, ProtocolBuffers.WriteValue("foo", typeof(string)))); Response response = Run(request); Assert.AreEqual("", response.Error); }
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); }
public void HandleRequestWithObjectParameter() { var instance = new TestService.TestClass("foo"); var arg = ObjectStore.Instance.AddInstance(instance); ByteString argBytes = ProtocolBuffers.WriteValue(arg, typeof(ulong)); var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.DeleteTestObject(It.IsAny <TestService.TestClass> ())) .Callback((TestService.TestClass x) => Assert.AreSame(instance, x)); TestService.Service = mock.Object; Run(Req("TestService", "DeleteTestObject", Arg(0, argBytes))); mock.Verify(x => x.DeleteTestObject(It.IsAny <TestService.TestClass> ()), Times.Once()); }
public void HandleRequestNoArgEnumReturn() { var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.ProcedureEnumReturn()).Returns(TestService.TestEnum.z); TestService.Service = mock.Object; var response = Run(Req("TestService", "ProcedureEnumReturn")); response.Time = 0; Assert.AreEqual("", response.Error); Assert.AreEqual(ProtocolBuffers.WriteValue((int)TestService.TestEnum.z, typeof(int)), response.ReturnValue); mock.Verify(x => x.ProcedureEnumReturn(), Times.Once()); }
public void WriteValue() { Assert.AreEqual("6E861BF0F9210940", ToHex(ProtocolBuffers.WriteValue(3.14159, typeof(double)))); Assert.AreEqual("D00F4940", ToHex(ProtocolBuffers.WriteValue(3.14159f, typeof(float)))); Assert.AreEqual("D6FFFFFFFFFFFFFFFF01", ToHex(ProtocolBuffers.WriteValue(-42, typeof(int)))); Assert.AreEqual("A2EAF7B6E0FEFFFFFF01", ToHex(ProtocolBuffers.WriteValue(-42834463454L, typeof(long)))); Assert.AreEqual("2A", ToHex(ProtocolBuffers.WriteValue(42U, typeof(uint)))); Assert.AreEqual("DE9588C99F01", ToHex(ProtocolBuffers.WriteValue(42834463454UL, typeof(ulong)))); Assert.AreEqual("01", ToHex(ProtocolBuffers.WriteValue(true, typeof(bool)))); Assert.AreEqual("00", ToHex(ProtocolBuffers.WriteValue(false, typeof(bool)))); Assert.AreEqual("086A65626564696168", ToHex(ProtocolBuffers.WriteValue("jebediah", typeof(string)))); Assert.AreEqual("03BADA55", ToHex(ProtocolBuffers.WriteValue(new byte[] { 0xBA, 0xDA, 0x55 }, typeof(byte[])))); Assert.Throws <ArgumentException> (() => ProtocolBuffers.WriteValue("foo", typeof(ProtocolBuffersTest))); }
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))); }
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))); }
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))); }
public void HandleRequestSingleEnumArgNoReturn() { var arg = TestService.TestEnum.y; var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.ProcedureEnumArg(It.IsAny <TestService.TestEnum> ())) .Callback((TestService.TestEnum x) => Assert.AreEqual(TestService.TestEnum.y, x)); TestService.Service = mock.Object; var request = Req("TestService", "ProcedureEnumArg", Arg(0, ProtocolBuffers.WriteValue((int)arg, typeof(int)))); Run(request); mock.Verify(x => x.ProcedureEnumArg(It.IsAny <TestService.TestEnum> ()), Times.Once()); }
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))); }
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))); }
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[]))); }
public void HandleEchoTuple() { var tuple = new KRPC.Schema.KRPC.Tuple(); tuple.Items.Add(ProtocolBuffers.WriteValue(42, typeof(int))); tuple.Items.Add(ProtocolBuffers.WriteValue(false, typeof(bool))); var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.EchoTuple(It.IsAny <KRPC.Utils.Tuple <int, bool> > ())) .Returns((KRPC.Utils.Tuple <int, bool> x) => x); TestService.Service = mock.Object; var response = Run(Req("TestService", "EchoTuple", Arg(0, ProtocolBuffers.WriteMessage(tuple)))); response.Time = 0; Assert.AreEqual("", response.Error); Assert.AreEqual(ProtocolBuffers.WriteMessage(tuple), response.ReturnValue); mock.Verify(x => x.EchoTuple(It.IsAny <KRPC.Utils.Tuple <int, bool> > ()), Times.Once()); }
public void HandleEchoSet() { var set = new Set(); set.Items.Add(ProtocolBuffers.WriteValue(345, typeof(int))); set.Items.Add(ProtocolBuffers.WriteValue(723, typeof(int))); set.Items.Add(ProtocolBuffers.WriteValue(112, typeof(int))); var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.EchoSet(It.IsAny <HashSet <int> > ())) .Returns((HashSet <int> x) => x); TestService.Service = mock.Object; var response = Run(Req("TestService", "EchoSet", Arg(0, ProtocolBuffers.WriteMessage(set)))); response.Time = 0; Assert.AreEqual("", response.Error); Assert.AreEqual(ProtocolBuffers.WriteMessage(set), response.ReturnValue); mock.Verify(x => x.EchoSet(It.IsAny <HashSet <int> > ()), Times.Once()); }
public void HandleEchoList() { var list = new KRPC.Schema.KRPC.List(); list.Items.Add(ProtocolBuffers.WriteValue("jeb", typeof(string))); list.Items.Add(ProtocolBuffers.WriteValue("bob", typeof(string))); list.Items.Add(ProtocolBuffers.WriteValue("bill", typeof(string))); var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.EchoList(It.IsAny <IList <string> > ())) .Returns((IList <string> x) => x); TestService.Service = mock.Object; var response = Run(Req("TestService", "EchoList", Arg(0, ProtocolBuffers.WriteMessage(list)))); response.Time = 0; Assert.AreEqual("", response.Error); Assert.AreEqual(ProtocolBuffers.WriteMessage(list), response.ReturnValue); mock.Verify(x => x.EchoList(It.IsAny <IList <string> > ()), Times.Once()); }
public void HandleEchoNestedCollection() { var list0 = new KRPC.Schema.KRPC.List(); list0.Items.Add(ProtocolBuffers.WriteValue("jeb", typeof(string))); list0.Items.Add(ProtocolBuffers.WriteValue("bob", typeof(string))); var list1 = new KRPC.Schema.KRPC.List(); var list2 = new KRPC.Schema.KRPC.List(); list2.Items.Add(ProtocolBuffers.WriteValue("bill", typeof(string))); list2.Items.Add(ProtocolBuffers.WriteValue("edzor", typeof(string))); var collection = new Dictionary(); collection.Entries.Add(new DictionaryEntry { Key = ProtocolBuffers.WriteValue(0, typeof(int)), Value = ProtocolBuffers.WriteMessage(list0) }); collection.Entries.Add(new DictionaryEntry { Key = ProtocolBuffers.WriteValue(1, typeof(int)), Value = ProtocolBuffers.WriteMessage(list1) }); collection.Entries.Add(new DictionaryEntry { Key = ProtocolBuffers.WriteValue(2, typeof(int)), Value = ProtocolBuffers.WriteMessage(list2) }); var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.EchoNestedCollection(It.IsAny <IDictionary <int, IList <string> > > ())) .Returns((IDictionary <int, IList <string> > x) => x); TestService.Service = mock.Object; var response = Run(Req("TestService", "EchoNestedCollection", Arg(0, ProtocolBuffers.WriteMessage(collection)))); response.Time = 0; Assert.AreEqual("", response.Error); Assert.AreEqual(ProtocolBuffers.WriteMessage(collection), response.ReturnValue); mock.Verify(x => x.EchoNestedCollection(It.IsAny <IDictionary <int, IList <string> > > ()), Times.Once()); }
public void HandleEchoListOfObjects() { var instance0 = new TestService.TestClass("foo"); var instance1 = new TestService.TestClass("bar"); var guid0 = ObjectStore.Instance.AddInstance(instance0); var guid1 = ObjectStore.Instance.AddInstance(instance1); var list = new KRPC.Schema.KRPC.List(); list.Items.Add(ProtocolBuffers.WriteValue(guid0, typeof(ulong))); list.Items.Add(ProtocolBuffers.WriteValue(guid1, typeof(ulong))); var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.EchoListOfObjects(It.IsAny <IList <TestService.TestClass> > ())) .Returns((IList <TestService.TestClass> x) => x); TestService.Service = mock.Object; var response = Run(Req("TestService", "EchoListOfObjects", Arg(0, ProtocolBuffers.WriteMessage(list)))); response.Time = 0; Assert.AreEqual("", response.Error); Assert.AreEqual(ProtocolBuffers.WriteMessage(list), response.ReturnValue); mock.Verify(x => x.EchoListOfObjects(It.IsAny <IList <TestService.TestClass> > ()), Times.Once()); }
/// <summary> /// Encode a value /// </summary> ByteString Encode(Type type, object value) { if (TypeUtils.IsAClassType(type)) { return(ProtocolBuffers.WriteValue(ObjectStore.Instance.AddInstance(value), typeof(ulong))); } else if (TypeUtils.IsACollectionType(type)) { return(EncodeCollection(type, value)); } else if (ProtocolBuffers.IsAMessageType(type)) { return(ProtocolBuffers.WriteMessage(value as IMessage)); } else if (TypeUtils.IsAnEnumType(type)) { // TODO: Assumes it's underlying type is int return(ProtocolBuffers.WriteValue((int)value, typeof(int))); } else { return(ProtocolBuffers.WriteValue(value, type)); } }