Exemplo n.º 1
0
        public void TestIntTaskReturnValue()
        {
            var        subject   = new Mock <IReturnsIntTask>();
            Task <int> innerTask = null;

            subject.Setup(x => x.DoStuff()).Returns(() =>
            {
                innerTask = new Task <int>(() => 42);
                innerTask.Start();
                return(innerTask);
            });
            IServant servant = _creator.CreateServant(_endPoint.Object, _channel.Object, 1, subject.Object);
            var      output  = new MemoryStream();

            servant.Invoke("DoStuff", null, new BinaryWriter(output));
            innerTask.Should().NotBeNull();
            innerTask.Status.Should().Be(TaskStatus.RanToCompletion);
            output.Position = 0;
            var reader = new BinaryReader(output);

            reader.ReadInt32().Should().Be(42);

            // 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);
        }
Exemplo n.º 2
0
        public void TestFixtureSetUp()
        {
            var seed = (int)DateTime.Now.Ticks;

            _random = new Random(seed);

            var endPoint = new Mock <IRemotingEndPoint>();

            _endPoint = endPoint.Object;

            var channel = new Mock <IEndPointChannel>();

            _channel = channel.Object;

            _proxyCreator   = new RemotingProxyCreator();
            _servantCreator = new ServantCreator();

            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 arguments) =>
            {
                if (objectId != _servant.ObjectId)
                {
                    throw new NoSuchServantException(objectId);
                }

                BinaryReader reader = arguments != null ? new BinaryReader(arguments) : null;
                var ret             = new MemoryStream();
                var writer          = new BinaryWriter(ret);

                _servant.Invoke(methodName, reader, writer);
                ret.Position = 0;
                return(ret);
            });
        }
Exemplo n.º 3
0
        public void TestIntMethodTypeParameters()
        {
            var subject = new Mock <ISubjectHost>();

            bool disposed = false;

            subject.Setup(x => x.Dispose())
            .Callback(() => disposed = true);

            ulong?actualId   = null;
            Type  @interface = null;
            Type  @impl      = null;

            subject.Setup(x => x.CreateSubject1(It.IsAny <ulong>(), It.IsAny <Type>(), It.IsAny <Type>()))
            .Callback((ulong id, Type a, Type b) =>
            {
                actualId   = id;
                @interface = a;
                @impl      = b;
            });

            IServant servant = TestGenerate(subject.Object);

            var arguments = new MemoryStream();
            var writer    = new BinaryWriter(arguments);

            writer.Write((ulong)42);
            writer.Write(true);
            writer.Write(typeof(IGetStringProperty).AssemblyQualifiedName);
            writer.Write(true);
            writer.Write(typeof(GetStringPropertyImplementation).AssemblyQualifiedName);
            arguments.Position = 0;

            var output = new MemoryStream();

            servant.Invoke("CreateSubject1", new BinaryReader(arguments), new BinaryWriter(output));
            actualId.Should().Be(42ul);
            @interface.Should().Be <IGetStringProperty>();
            @impl.Should().Be <GetStringPropertyImplementation>();

            servant.Invoke("Dispose", null, new BinaryWriter(new MemoryStream()));
            disposed.Should().BeTrue();

            // 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);
        }
Exemplo n.º 4
0
        public void TestWatchdog()
        {
            var subject = new Mock <IReturnComplexType>();
            var app     = new InstalledApplication(new ApplicationDescriptor {
                Name = "SharpRemote/0.1"
            });

            app.Files.Add(new InstalledFile
            {
                Id         = 1,
                Filename   = "SharpRemote.dll",
                FileLength = 212345,
                Folder     = Environment.SpecialFolder.CommonProgramFiles
            });
            app.Files.Add(new InstalledFile
            {
                Id         = 2,
                Filename   = "SharpRemote.Host.exe",
                FileLength = 1234,
                Folder     = Environment.SpecialFolder.CommonProgramFiles
            });
            subject.Setup(x => x.CommitInstallation(It.IsAny <long>()))
            .Returns((long id) => app);
            IServant servant = TestGenerate(subject.Object);

            var arguments = new MemoryStream();
            var writer    = new BinaryWriter(arguments);

            writer.Write((long)42);
            arguments.Position = 0;

            var output = new MemoryStream();

            servant.Invoke("CommitInstallation", new BinaryReader(arguments), new BinaryWriter(output));
            output.Position = 0;
            output.Length.Should().BeInRange(342, 344);

            // 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);
        }
Exemplo n.º 5
0
        public void TestGetProperty()
        {
            var      subject = new Mock <IGetDoubleProperty>();
            IServant servant = TestGenerate(subject.Object);

            subject.Setup(x => x.Value).Returns(Math.PI);

            var outStream = new MemoryStream();
            var @out      = new BinaryWriter(outStream);

            servant.Invoke("get_Value", null, @out);

            outStream.Position = 0;
            var reader = new BinaryReader(outStream);

            reader.ReadDouble().Should().BeApproximately(Math.PI, 0);

            // 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);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        public void TestVoidMethodTypeParameter()
        {
            var  subject    = new Mock <IVoidMethodTypeParameter>();
            Type actualType = null;

            subject.Setup(x => x.Do(It.IsAny <Type>())).Callback((Type t) => { actualType = t; });

            IServant servant = TestGenerate(subject.Object);

            var arguments = new MemoryStream();
            var writer    = new BinaryWriter(arguments);

            writer.Write(true);
            writer.Write(typeof(string).AssemblyQualifiedName);
            arguments.Position = 0;

            servant.Invoke("Do", new BinaryReader(arguments), new BinaryWriter(new MemoryStream()));
            actualType.Should().Be <string>();

            // 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);
        }