public void TestByReferenceParameter() { IVoidMethodStringParameter actualListener = null; var subject = new Mock <IByReferenceParemeterMethodInterface>(); subject.Setup(x => x.AddListener(It.IsAny <IVoidMethodStringParameter>())) .Callback((IVoidMethodStringParameter l) => actualListener = l); var listener = new Mock <IVoidMethodStringParameter>(); IServant servant = _creator.CreateServant(_endPoint.Object, _channel.Object, 1, subject.Object); _endPoint.Setup(x => x.GetExistingOrCreateNewProxy <IVoidMethodStringParameter>(It.IsAny <ulong>())) .Returns((ulong objectId) => { objectId.Should().Be(12345678912345678912); return(listener.Object); }); var inStream = new MemoryStream(); var writer = new BinaryWriter(inStream); writer.Write(true); writer.Write((byte)ByReferenceHint.CreateProxy); writer.Write(12345678912345678912); inStream.Position = 0; var @in = new BinaryReader(inStream); var outStream = new MemoryStream(); servant.Invoke("AddListener", @in, new BinaryWriter(outStream)); actualListener.Should() .BeSameAs(listener.Object, "because the compiled code should've retrieved the existing proxy by its id"); outStream.Length.Should().Be(0, "because nothing needed to be written to the outstream"); // Servants hold a weak reference to their subjects, so in order for this test to run 100% of the time, // we need to keep the subject alive. GC.KeepAlive(subject.Object); }
public void TestReturnByReference1() { var proxy = TestGenerate <IByReferenceReturnMethodInterface>(); IVoidMethodStringParameter listener = new VoidMethodStringParameter(); const ulong id = 42; bool addListenerCalled = false; _channel.Setup( x => x.CallRemoteMethod(It.IsAny <ulong>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <MemoryStream>())) .Returns((ulong objectId, string interfaceName, string methodName, Stream stream) => { objectId.Should().Be(((IProxy)proxy).ObjectId); methodName.Should().Be("AddListener"); stream.Should().BeNull(); addListenerCalled = true; var output = new MemoryStream(); var writer = new BinaryWriter(output); writer.Write(true); writer.Write((byte)ByReferenceHint.CreateProxy); writer.Write(id); writer.Flush(); output.Position = 0; return(output); }); _endPoint.Setup(x => x.GetExistingOrCreateNewProxy <IVoidMethodStringParameter>(It.Is((ulong y) => y == id))) .Returns(listener); IVoidMethodStringParameter actualListener = proxy.AddListener(); addListenerCalled.Should().BeTrue(); actualListener.Should().NotBeNull(); actualListener.Should().BeSameAs(listener); }