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
        private T CreateServantAndProxy <T>(T subject)
        {
            var objectId = (ulong)_random.Next();

            _servant = _servantCreator.CreateServant(_endPoint, _channel, objectId, subject);
            return(_proxyCreator.CreateProxy <T>(_endPoint, _channel, objectId));
        }
Exemplo n.º 3
0
        public void TestEventInt32()
        {
            var      subject = new Mock <IEventInt32>();
            IServant servant = TestGenerate(subject.Object);

            bool callRemoteMethodInvoked = false;

            _channel.Setup(
                x => x.CallRemoteMethod(It.IsAny <ulong>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <MemoryStream>()))
            .Callback((ulong id, string interfaceName, string methodName, MemoryStream stream) =>
            {
                id.Should().Be(servant.ObjectId);
                methodName.Should().Be("Foobar");
                stream.Should().NotBeNull();
                var reader = new BinaryReader(stream);
                reader.ReadInt32().Should().Be(42);
                callRemoteMethodInvoked = true;
            });
            subject.Raise(x => x.Foobar += null, 42);
            callRemoteMethodInvoked.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 TestGetTaskScheduler6()
        {
            var      subject = new Mock <IInvokeAttributeMethods>();
            IServant servant = TestGenerate(subject.Object);

            new Action(() => servant.GetTaskScheduler("DoesntExist"))
            .Should().Throw <ArgumentException>();
        }
Exemplo n.º 5
0
 public bool TryGetServant(ulong servantId, out IServant servant, out int numServants)
 {
     lock (_syncRoot)
     {
         numServants = _servantsById.Count;
         return(_servantsById.TryGetValue(servantId, out servant));
     }
 }
Exemplo n.º 6
0
        public void TestGetTaskScheduler2()
        {
            var      subject = new Mock <IInvokeAttributeMethods>();
            IServant servant = TestGenerate(subject.Object);

            servant.GetTaskScheduler("DoNotSerialize")
            .Should().BeNull();
        }
Exemplo n.º 7
0
        public void TestGetTaskScheduler4()
        {
            var      subject = new Mock <IInvokeAttributeMethods>();
            IServant servant = TestGenerate(subject.Object);

            var scheduler = servant.GetTaskScheduler("SerializePerObject1");

            scheduler.Should().NotBeNull();
            scheduler.Should().BeOfType <SerialTaskScheduler>();

            scheduler.Should().BeSameAs(servant.GetTaskScheduler("SerializePerObject2"));
        }
Exemplo n.º 8
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.º 9
0
        public void TestGetTaskScheduler5()
        {
            var      subject  = new Mock <IInvokeAttributeMethods>();
            IServant servant1 = TestGenerate(subject.Object);
            IServant servant2 = TestGenerate(subject.Object);

            var scheduler = servant1.GetTaskScheduler("SerializePerMethod1");

            scheduler.Should().NotBeNull();
            scheduler.Should().BeOfType <SerialTaskScheduler>();

            scheduler.Should()
            .NotBeSameAs(servant1.GetTaskScheduler("SerializePerMethod2"),
                         "because Dispatch.SerializePerMethod shall behave exactly like Java's synchronized statement");
            scheduler.Should()
            .NotBeSameAs(servant2.GetTaskScheduler("SerializePerMethod1"),
                         "because Dispatch.SerializePerMethod shall behave exactly like Java's synchronized statement");
        }
Exemplo n.º 10
0
        public void TestRaiseEmptyEvent()
        {
            var       subject   = new Mock <IEventInt32>();
            const int servantId = 4;
            IServant  servant   = _server.CreateServant(servantId, subject.Object);
            var       proxy     = (IProxy)_client.CreateProxy <IEventInt32>(servantId);

            new Action(() => subject.Raise(x => x.Foobar += null, 42))
            .ExecutionTime().ShouldNotExceed(TimeSpan.FromSeconds(1));

            servant.ObjectId.Should().Be(servantId);
            proxy.ObjectId.Should().Be(servantId);

            // 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);
        }
Exemplo n.º 11
0
        public void TestCreateServant2()
        {
            using (var endPoint = CreateClient())
            {
                var      subject = new Mock <IEventInt32>();
                IServant servant = null;

                const string reason =
                    "because a servant can always be created - its usage may however not work depending on the endpoint's connection status";
                new Action(() => servant = endPoint.CreateServant(0, subject.Object))
                .Should().NotThrow(reason);
                servant.Should().NotBeNull(reason);

                new Action(() => subject.Raise(x => x.Foobar += null, 42))
                .Should().Throw <NotConnectedException>(
                    "because the endpoint is not connected to any other endpoint and thus there cannot be a proxy on which the event can ever be executed");
            }
        }
Exemplo n.º 12
0
        private IServant TestGenerate <T>(T subject)
        {
            Type type = null;

            new Action(() => type = _creator.GenerateServant <T>())
            .Should().NotThrow();

            type.Should().NotBeNull();

            _objectId = (ulong)_random.Next();
            IServant servant = _creator.CreateServant(_endPoint.Object, _channel.Object, _objectId, subject);

            servant.Should().NotBeNull();
            servant.ObjectId.Should().Be(_objectId);
            servant.Subject.Should().BeSameAs(subject);

            return(servant);
        }
Exemplo n.º 13
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.º 14
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.º 15
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.º 16
0
        public void TestAddByReference1()
        {
            const ulong servantId = 30;
            var         processor = new Processor();

            processor.Listeners.Should().BeEmpty();

            _server.CreateServant(servantId, (IProcessor)processor);
            var proxy = _client.CreateProxy <IProcessor>(servantId);

            var listener1 = new Listener();

            proxy.AddListener(listener1);
            IServant listenerServant = Servants(_client).First(x => x.InterfaceType == typeof(IListener));
            IProxy   listenerProxy   = _server.Proxies.First(x => x.InterfaceType == typeof(IListener));

            listenerServant.ObjectId.Should()
            .Be(listenerProxy.ObjectId,
                "Because both proxy and servant must have been created for that specific listener instance because of the remote method call");


            processor.Listeners.Count.Should().Be(1, "Because AddListener() should have added a listener to the processor");
            processor.Listeners[0].Should()
            .NotBe(listener1, "Because not the real listener has been added, but a proxy representing it");
            processor.Listeners[0].Should()
            .BeSameAs(listenerProxy,
                      "Because not the real listener has been added, but a proxy representing it");


            proxy.Report("Foobar");
            listener1.Messages.Should()
            .Equal(new[] { "Foobar" },
                   "Because Processor.Report() method should have invoked the Report() method on the proxy which in turn would've called the real listener implementation");

            // 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(processor);
        }
Exemplo n.º 17
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);
        }
Exemplo n.º 18
0
 public void Add(string servantName, IServant servant)
 {
     this.servantsByName.Add(servantName, servant);
     servant.Die += OnServantDead;
 }
Exemplo n.º 19
0
 public void TestEmpty()
 {
     var      subject = new Mock <IEmpty>();
     IServant servant = TestGenerate(subject.Object);
 }