예제 #1
0
        public void Can_reconnect()
        {
            var reconnected = false;
            var client = new DdpClient(_socket, _helper);
            client.DdpReconnect += () => reconnected = true;

            var firstMessage = new PortableMessageReceivedEventArgs(JsonConvert.SerializeObject(new
            {
                session = AutoFixture.Create<string>(),
                msg = "connected"
            }));

            var secondMessage = new PortableMessageReceivedEventArgs(JsonConvert.SerializeObject(new
            {
                session = AutoFixture.Create<string>(),
                msg = "connected"
            }));

            // Act
            _socket.MessageReceived += Raise.Event<EventHandler<PortableMessageReceivedEventArgs>>(new object(), firstMessage);
            _socket.Closed += Raise.Event<EventHandler>();
            _socket.MessageReceived += Raise.Event<EventHandler<PortableMessageReceivedEventArgs>>(new object(), secondMessage);

            // Assert
            _socket.Received().Open();
            reconnected.Should().BeTrue();
        }
예제 #2
0
        public void DdpClient_ConnectAsync_CalledMultipleTimes()
        {
            var connection = new InMemoryConnection();

            var client = new DdpClient(connection);

            connection.Reply(
                JsonConvert.SerializeObject(new Connected() {Session = "TestSession"}));

            client.ConnectAsync().Wait();

            bool exceptionCaught = false;

            try
            {
                client.ConnectAsync().Wait();
            }
            catch (AggregateException e)
            {
                Assert.IsTrue(e.InnerException is InvalidOperationException);
                exceptionCaught = true;
            }

            Assert.IsTrue(exceptionCaught);
        }
예제 #3
0
        public void DdpClient_Call_GetResult()
        {
            var connection = new InMemoryConnection();

            var client = new DdpClient(connection);

            this.Connect(client, connection);

            this.ClearSentMessages(connection);

            var task = client.Call<SimpleField>("TestMethod");

            var methodString = connection.GetSentMessage();

            var method = JsonConvert.DeserializeObject<Method>(methodString);

            connection.Reply(
                JsonConvert.SerializeObject(new Result()
                {
                    Id = method.Id,
                    ResultObject = JToken.FromObject(new SimpleField() {integerField = 11, boolField = false, stringField = "ReturnObject"})
                }));
            connection.Reply(JsonConvert.SerializeObject(new Updated() { Methods = new[] { method.Id } }));

            task.Wait();

            Assert.AreEqual("TestMethod", method.MethodName);
            Assert.AreEqual(0, method.Parameters.Length);

            var result = task.Result;

            Assert.AreEqual(11, result.integerField);
            Assert.AreEqual(false, result.boolField);
            Assert.AreEqual("ReturnObject", result.stringField);
        }
예제 #4
0
        public async Task Can_ping()
        {
            var client = new DdpClient(Constants.RocketServer, false, _helper);
            await client.ConnectAsync(TimeoutToken);

            // Act
            await client.PingAsync(TimeoutToken);

            // Assert
        }
예제 #5
0
        public async Task Can_ping()
        {
            var client = new DdpClient(Constants.RocketServer, false, _helper);
            await client.ConnectAsync(TimeoutToken);

            // Act
            await client.PingAsync(TimeoutToken);

            // Assert
        }
예제 #6
0
        public void On_error_log()
        {
            var logger = Substitute.For<ILogger>();
            // ReSharper disable once UnusedVariable
            var client = new DdpClient(_socket, logger);

            var message = AutoFixture.Create<string>();
            var exception = new Exception(message);

            // Act
            _socket.Error += Raise.Event<EventHandler<PortableErrorEventArgs>>(new object(), new PortableErrorEventArgs(exception));

            // Assert
            logger.Received().Info($"ERROR: {message}");
        }
예제 #7
0
        public void Connecting_should_set_session()
        {
            var client = new DdpClient(_socket, _helper);

            var sessionId = AutoFixture.Create<string>();
            var message = new
            {
                session = sessionId,
                msg = "connected"
            };
            var jsonMessage = new PortableMessageReceivedEventArgs(JsonConvert.SerializeObject(message));

            // Act
            _socket.MessageReceived += Raise.Event<EventHandler<PortableMessageReceivedEventArgs>>(new object(), jsonMessage);

            // Assert
            client.SessionId.Should().Be(sessionId);
        }
예제 #8
0
        public void Can_unsubscribe()
        {
            var client = new DdpClient(_socket, _helper);

            var subId = AutoFixture.Create<string>();
            var message = new
            {
                id = subId,
                msg = "nosub"
            };
            var jsonMessage = new PortableMessageReceivedEventArgs(JsonConvert.SerializeObject(message));

            // Act
            var task = client.UnsubscribeAsync(subId, TimeoutToken);
            _socket.MessageReceived += Raise.Event<EventHandler<PortableMessageReceivedEventArgs>>(new object(), jsonMessage);

            Action action = async () => await task;

            // Assert
            action.ShouldNotThrow<OperationCanceledException>();
        }
예제 #9
0
        static void Main(string[] args)
        {
            using (DdpClient client = new DdpClient("localhost:3000"))
            {
                client.Message       += OnMessage;
                client.MeteorError   += OnError;
                client.MethodResult  += OnMessageResult;
                client.MethodUpdated += OnMessageUpdated;
                client.ClientError   += OnClientError;


                client.Connect();
                String callId = Guid.NewGuid().ToString("N");
                client.Subscribe(Guid.NewGuid().ToString("N"), "MeteorPublishTest");
                client.Publish(callId, "MeteorMethod", new
                {
                    Type   = "test",
                    @event = "Hello, Meteor!!!"
                });
                Console.WriteLine("Called method with callId: " + callId);
                Console.ReadLine();
            }
        }
예제 #10
0
        static void Main(string[] args)
        {
            using(DdpClient client = new DdpClient("localhost:3000"))
            {
                client.Message += OnMessage;
                client.MeteorError += OnError;
                client.MethodResult += OnMessageResult;
                client.MethodUpdated += OnMessageUpdated;
                client.ClientError += OnClientError;


                client.Connect();
                String callId = Guid.NewGuid().ToString("N");
                client.Subscribe(Guid.NewGuid().ToString("N"), "MeteorPublished");
                client.Publish(callId, "MeteorMethod", new
                {
                    Type = "test",
                    @event = "Hello, Meteor!!!"
                });
                Console.WriteLine("Called method with callId: " + callId);
                Console.ReadLine();
            }
        }
예제 #11
0
        public void DdpClient_Call_ServerMethodInvoked()
        {
            var connection = new InMemoryConnection();

            var client = new DdpClient(connection);

            this.Connect(client, connection);

            this.ClearSentMessages(connection);

            var task = client.Call("TestMethod");

            var methodString = connection.GetSentMessage();

            var method = JsonConvert.DeserializeObject<Method>(methodString);

            connection.Reply(JsonConvert.SerializeObject(new Result() {Id = method.Id}));
            connection.Reply(JsonConvert.SerializeObject(new Updated() {Methods = new[] {method.Id}}));

            task.Wait();

            Assert.AreEqual("TestMethod", method.MethodName);
            Assert.AreEqual(0, method.Parameters.Length);
        }
예제 #12
0
        public void DdpClient_Call_ErrorReturned()
        {
            var connection = new InMemoryConnection();

            var client = new DdpClient(connection);

            this.Connect(client, connection);

            this.ClearSentMessages(connection);

            var task = client.Call("TestMethod");

            var methodString = connection.GetSentMessage();

            var method = JsonConvert.DeserializeObject<Method>(methodString);

            connection.Reply(
                JsonConvert.SerializeObject(new Result()
                {
                    Id = method.Id,
                    Error = new Error() {Details = "Failed", ErrorMessage = "TestFailed", Reason = "ReasonFailed"}
                }));
            connection.Reply(JsonConvert.SerializeObject(new Updated() { Methods = new[] { method.Id } }));

            ExceptionAssert.ExpectAggregateException(() => task.Wait(), typeof(DdpServerException));

            Assert.AreEqual("TestMethod", method.MethodName);
            Assert.AreEqual(0, method.Parameters.Length);

            Assert.IsNotNull(task.Exception);
            Assert.IsTrue(task.IsFaulted);
        }
예제 #13
0
 public void TestInitialize()
 {
     this.testConnection = new InMemoryConnection();
     this.client = new DdpClient(this.testConnection);
 }
예제 #14
0
        public void DdpClient_GetCollection_GetsTypedCollection()
        {
            var connection = new InMemoryConnection();

            var client = new DdpClient(connection);

            this.Connect(client, connection);

            var collection = client.GetCollection<SimpleDdpObject>("Test");

            Assert.IsNotNull(collection);
        }
예제 #15
0
        public void DdpClient_Call_BeforeConnectThrows()
        {
            var connection = new InMemoryConnection();
            var client = new DdpClient(connection);

            PCLTesting.ExceptionAssert.Throws<InvalidOperationException>(() => client.Call("Test"));
        }
예제 #16
0
        public void DdpClient_CallParametersResult_BeforeConnectThrows()
        {
            var connection = new InMemoryConnection();
            var client = new DdpClient(connection);

            PCLTesting.ExceptionAssert.Throws<InvalidOperationException>(() => client.Call<string>("Test", 1 ));
        }
예제 #17
0
        public void DdpClient_SubscribeParameters_BeforeConnectThrows()
        {
            var connection = new InMemoryConnection();
            var client = new DdpClient(connection);

            PCLTesting.ExceptionAssert.Throws<InvalidOperationException>(() => client.Subscribe("TestSub", "Test" ));
        }
예제 #18
0
        private void Connect(DdpClient client, InMemoryConnection connection)
        {
            connection.Reply(
                JsonConvert.SerializeObject(new Connected() { Session = "TestSession" }));

            client.ConnectAsync().Wait();
        }
예제 #19
0
        public void DdpClient_Subscribe_ServerMethodCalled()
        {
            var connection = new InMemoryConnection();

            var client = new DdpClient(connection);

            this.Connect(client, connection);

            this.ClearSentMessages(connection);

            var task = client.Subscribe("TestSubscription");

            var subscribeString = connection.GetSentMessage();
            var subscribe = JsonConvert.DeserializeObject<Subscribe>(subscribeString);

            connection.Reply(JsonConvert.SerializeObject(new Ready() { SubscriptionsReady = new []{subscribe.Id}}));

            task.Wait();

            Assert.IsTrue(task.IsCompleted);
            Assert.AreEqual("TestSubscription", subscribe.Name);
        }