public void TestReturnByReference2()
        {
            const ulong servantId = 27;

            var subject = new Mock <IReturnsObjectMethod>();
            var foo     = new ByReferenceClass();

            subject.Setup(x => x.GetListener()).Returns(foo);

            _server.CreateServant(servantId, subject.Object);
            var proxy = _client.CreateProxy <IReturnsObjectMethod>(servantId);

            var actualFoo1 = proxy.GetListener() as IByReferenceType;

            actualFoo1.Should().NotBeNull();
            actualFoo1.Value.Should().Be(foo.Value);

            proxy.GetListener()
            .Should()
            .BeSameAs(actualFoo1,
                      "because [ByReference] types must be marshalled with referential equality in mind - GetFoo() always returns the same instance and thus the proxy should as well");

            // This line exists to FORCE the GC to NOT collect the subject, which
            // in turn would unregister the servant from the server, thus making the test
            // fail sporadically.
            GC.KeepAlive(subject);
        }
        public void TestAddObjectByReference1()
        {
            var reference = new ByReferenceClass(9001);
            var value     = new FieldObjectStruct
            {
                Value = reference
            };

            var    subject     = new Mock <IVoidMethodObjectParameter>();
            object actualValue = null;

            subject.Setup(x => x.AddListener(It.IsAny <object>()))
            .Callback((object x) => actualValue = x);

            const ulong servantId = 25;

            _server.CreateServant(servantId, subject.Object);
            var proxy = _client.CreateProxy <IVoidMethodObjectParameter>(servantId);

            proxy.AddListener(value);
            actualValue.Should().NotBeNull();
            actualValue.Should().BeOfType <FieldObjectStruct>();
            (((FieldObjectStruct)actualValue).Value is IByReferenceType).Should().BeTrue();
            object actualReference = ((FieldObjectStruct)actualValue).Value;

            proxy.AddListener(value);
            ((FieldObjectStruct)actualValue).Value.Should()
            .BeSameAs(actualReference,
                      "Because [ByReference] types should adhere to referential equality after deserialization");

            // This line exists to FORCE the GC to NOT collect the subject, which
            // in turn would unregister the servant from the server, thus making the test
            // fail sporadically.
            GC.KeepAlive(subject);
        }
示例#3
0
        public void TestCollect1()
        {
            var dictionary = new WeakKeyDictionary <object, int>();
            WeakReference <object> weakKey = null;

            new Action(() =>
            {
                var key = new ByReferenceClass();
                weakKey = new WeakReference <object>(key);

                dictionary.Add(key, 42);
                dictionary.Count.Should().Be(1);
            })();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            weakKey.Should().NotBeNull();

            dictionary.Collect();
            dictionary.Count.Should().Be(0);
            object unused;

            weakKey.TryGetTarget(out unused).Should().BeFalse("Because the dictionary shouldn't keep the key alive");

            EnsureIntegrity(dictionary);
        }
示例#4
0
        public void TestByReferenceDeserialize()
        {
            var endPoint = new Mock <IRemotingEndPoint>();

            const ulong objectId = 42;
            var         value    = new ByReferenceClass();

            endPoint.Setup(x => x.GetExistingOrCreateNewProxy <IByReferenceType>(It.IsAny <ulong>()))
            .Returns((ulong id) =>
            {
                id.Should().Be(objectId);
                return(value);
            });

            using (var stream = new MemoryStream())
                using (var writer = new BinaryWriter(stream))
                    using (var reader = new BinaryReader(stream))
                    {
                        writer.Write(typeof(IByReferenceType).AssemblyQualifiedName);
                        writer.Write((byte)ByReferenceHint.CreateProxy);
                        writer.Write(objectId);

                        writer.Flush();
                        stream.Position = 0;

                        var actualValue = _serializer.ReadObject(reader, endPoint.Object);
                        actualValue.Should().BeSameAs(value);

                        stream.Position.Should().Be(stream.Length, "because we should've consumed the entire stream");
                    }
        }
示例#5
0
        public void TestByReferenceSerialize1()
        {
            _serializer.RegisterType <IByReferenceType>();

            var        value    = new ByReferenceClass();
            const long objectId = 42;
            var        endPoint = new Mock <IRemotingEndPoint>();

            endPoint.Setup(x => x.GetExistingOrCreateNewServant(It.IsAny <IByReferenceType>()))
            .Returns((IByReferenceType x) =>
            {
                x.Should().BeSameAs(value);
                var servant = new Mock <IServant>();
                servant.Setup(y => y.ObjectId).Returns(objectId);
                return(servant.Object);
            });

            using (var stream = new MemoryStream())
                using (var writer = new BinaryWriter(stream))
                    using (var reader = new BinaryReader(stream))
                    {
                        _serializer.WriteObject(writer, value, endPoint.Object);
                        writer.Flush();
                        stream.Position = 0;

                        reader.ReadString().Should().Be(typeof(IByReferenceType).AssemblyQualifiedName);
                        reader.ReadByte().Should().Be((byte)ByReferenceHint.CreateProxy);
                        reader.ReadInt64().Should().Be(objectId);

                        stream.Position.Should().Be(stream.Length, "because we should've consumed the entire stream");
                    }
        }
        public void TestReturnListOfByReferences()
        {
            const ulong servantId = 28;

            var subject = new Mock <IReturnsObjectArray>();
            var foo1    = new ByReferenceClass(42);
            var foo2    = new ByReferenceClass(9001);

            subject.Setup(x => x.Objects).Returns(new object[]
            {
                foo1,
                foo2,
                foo1,
                42,
                "Hello World!"
            });

            _server.CreateServant(servantId, subject.Object);
            var proxy = _client.CreateProxy <IReturnsObjectArray>(servantId);

            object[] objects = proxy.Objects;
            objects.Should().NotBeNull();
            objects.Length.Should().Be(5);
            objects[0].Should().NotBeNull();
            (objects[0] is IByReferenceType).Should().BeTrue();
            ((IByReferenceType)objects[0]).Value.Should().Be(foo1.Value);

            objects[1].Should().NotBeNull();
            (objects[1] is IByReferenceType).Should().BeTrue();
            ((IByReferenceType)objects[1]).Value.Should().Be(foo2.Value);

            objects[2].Should().BeSameAs(objects[0]);
            objects[3].Should().Be(42);
            objects[4].Should().Be("Hello World!");

            // This line exists to FORCE the GC to NOT collect the subject, which
            // in turn would unregister the servant from the server, thus making the test
            // fail sporadically.
            GC.KeepAlive(subject);
        }