Example #1
0
        public async Task Subscribe_wildcard_channel()
        {
            // arrange
            _fayeServerProcess.StartThinServer();
            var socket = new WebSocketClient(uri: TEST_SERVER_URL);

            SetupWebSocket(socket);
            InstantiateFayeClient();
            _connection = await _fayeClient.Connect();

            const string    wildcardChannel = "/*";
            Action <string> dummyAction     = msg => Console.WriteLine("hi");

            // act + assert
            var exception = await _connection.InvokingAsync(c => c.Subscribe(wildcardChannel,
                                                                             dummyAction))
                            .ShouldThrow <SubscriptionException>();

            var expectedMessage = string.Format(FayeConnection.WILDCARD_CHANNEL_ERROR_FORMAT,
                                                wildcardChannel);

            exception.Message
            .Should()
            .Be(expectedMessage);
        }
Example #2
0
        public async Task Publish_invalid_channel()
        {
            // arrange
            _fayeServerProcess.StartThinServer();
            var socket = new WebSocketClient(uri: TEST_SERVER_URL);

            SetupWebSocket(socket);
            InstantiateFayeClient();
            _connection = await _fayeClient.Connect();

            const string invalidChannel   = "/*";
            var          messageToSendObj = new TestMsg {
                Stuff = "the message"
            };
            var messageToSend = JsonConvert.SerializeObject(messageToSendObj);

            // act + assert
            var exception = await _connection.InvokingAsync(c => c.Publish(channel: invalidChannel,
                                                                           message: messageToSend))
                            .ShouldThrow <PublishException>();

            exception.Message
            .Should()
            .Be("405:/*:Invalid channel");
        }
Example #3
0
        public async Task Subscribe_twice()
        {
            // arrange
            _fayeServerProcess.StartThinServer();
            var socket = new WebSocketClient(uri: TEST_SERVER_URL);

            SetupWebSocket(socket);
            InstantiateFayeClient();
            _connection = await _fayeClient.Connect();

            var secondClient = GetFayeClient(new WebSocketClient(uri: TEST_SERVER_URL));

            _connection2 = await secondClient.Connect();

            var          tcs           = new TaskCompletionSource <string>();
            var          tcs2          = new TaskCompletionSource <string>();
            const string channelName   = "/somechannel";
            var          messageToSend = new TestMsg {
                Stuff = "the message"
            };
            var json = JsonConvert.SerializeObject(messageToSend);
            var task = tcs.Task;

            // act

            await _connection.Subscribe(channelName,
                                        tcs.SetResult);

            await _connection.Subscribe(channelName,
                                        tcs2.SetResult);

            await _connection2.Publish(channel : channelName,
                                       message : json);

            // assert
            var result = await task.Timeout(5.Seconds());

            if (result == Result.Timeout)
            {
                Assert.Fail("Timed out waiting for pub/sub to work");
            }
            var jsonReceived   = task.Result;
            var objectReceived = JsonConvert.DeserializeObject <TestMsg>(jsonReceived);

            objectReceived
            .ShouldBeEquivalentTo(messageToSend);
            task   = tcs2.Task;
            result = await task.Timeout(5.Seconds());

            if (result == Result.Timeout)
            {
                Assert.Fail("Timed out waiting for pub/sub to work");
            }
            jsonReceived   = task.Result;
            objectReceived = JsonConvert.DeserializeObject <TestMsg>(jsonReceived);
            objectReceived
            .ShouldBeEquivalentTo(messageToSend);
        }
Example #4
0
        public async Task Connect_lost_connection_retry_not_allowed()
        {
            // arrange
            TriggerNoRetriesAllowed();
            // port 8133
            const int inputPort = THIN_SERVER_PORT + 1;

            _fayeServerProcess.StartThinServer();
            _socatInterceptor = StartWritableSocket(hostname: "localhost",
                                                    inputPort: inputPort);
            const string urlThroughSocat = "ws://localhost:8133/bayeux";
            var          socket          = new WebSocketClient(uri: urlThroughSocat);

            SetupWebSocket(socket);
            InstantiateFayeClient();
            _connection = await _fayeClient.Connect();

            var          tcs         = new TaskCompletionSource <string>();
            const string channelName = "/somechannel";
            var          lostTcs     = new TaskCompletionSource <bool>();

            _connection.ConnectionLost += (sender,
                                           args) => lostTcs.SetResult(true);
            var backTcs = new TaskCompletionSource <bool>();

            _connection.ConnectionReestablished += (sender,
                                                    args) => backTcs.SetResult(true);
            var tryToRestablishTask = backTcs.Task;

            // act
            await _connection.Subscribe(channelName,
                                        tcs.SetResult);

            // ReSharper disable once CSharpWarnings::CS4014
            Task.Factory.StartNew(() =>
            {
                _socatInterceptor.Kill();
                _socatInterceptor = StartWritableSocket(hostname: "localhost",
                                                        inputPort: inputPort);
            });
            await lostTcs.Task.WithTimeout(t => t,
                                           20.Seconds());

            // assert
            tryToRestablishTask
            .Status
            .Should()
            .Be(TaskStatus.WaitingForActivation,
                "should not try and re-establish since we disabled retries");
            await Task.Delay(10.Seconds());

            tryToRestablishTask
            .Status
            .Should()
            .Be(TaskStatus.WaitingForActivation,
                "Still should not try and re-establish since we disabled retries");
        }
Example #5
0
 public override void SetUp()
 {
     base.SetUp();
     _fayeClient        = null;
     _websocket         = null;
     _connection        = null;
     _fayeServerProcess = new ThinServerProcess(thinPort: 8132,
                                                workingDirectory: WorkingDirectory);
 }
Example #6
0
 public override void SetUp()
 {
     base.SetUp();
     _fayeClient        = null;
     _websocket         = null;
     _connection        = null;
     _connection2       = null;
     _fayeServerProcess = new ThinServerProcess(thinPort: THIN_SERVER_PORT,
                                                workingDirectory: WorkingDirectory);
     _socatInterceptor = null;
     _connectionNumber = 0;
 }
Example #7
0
        public async Task Connect_handshake_and_connect_complete_ok()
        {
            // arrange
            _fayeServerProcess.StartThinServer();
            var socket = new WebSocketClient(uri: TEST_SERVER_URL);

            SetupWebSocket(socket);
            InstantiateFayeClient();

            // act
            _connection = await _fayeClient.Connect();

            // assert
            _connection
            .ClientId
            .Should()
            .NotBeNullOrEmpty();
        }
Example #8
0
        public async Task Unsubscribe()
        {
            // arrange
            _fayeServerProcess.StartThinServer();
            var socket = new WebSocketClient(uri: TEST_SERVER_URL);

            SetupWebSocket(socket);
            InstantiateFayeClient();
            _connection = await _fayeClient.Connect();

            var secondClient = GetFayeClient(new WebSocketClient(uri: TEST_SERVER_URL));

            _connection2 = await secondClient.Connect();

            const string channelName      = "/somechannel";
            var          tcs              = new TaskCompletionSource <object>();
            var          messageToSendObj = new TestMsg {
                Stuff = "the message"
            };
            var messageToSend = JsonConvert.SerializeObject(messageToSendObj);


            await _connection.Subscribe(channelName,
                                        tcs.SetResult); // should never hit this

            // act
            await _connection.Unsubscribe(channelName);

            // assert
            await _connection2.Publish(channelName,
                                       messageToSend);

            await Task.Delay(100.Milliseconds());

            tcs.Task
            .Status
            .Should()
            .Be(TaskStatus.WaitingForActivation,
                "We should never fire our event since we unsubscribe before the 1st message");
        }
Example #9
0
        public async Task Subscribe_reserved_channel_name()
        {
            // arrange
            _fayeServerProcess.StartThinServer();
            var socket = new WebSocketClient(uri: TEST_SERVER_URL);

            SetupWebSocket(socket);
            InstantiateFayeClient();
            _connection = await _fayeClient.Connect();

            const string    reservedChannelName = "/meta/subscribe";
            Action <string> dummyAction         = msg => Console.WriteLine("hi");

            // act + assert
            var exception = await _connection.InvokingAsync(s => s.Subscribe(channel: reservedChannelName,
                                                                             messageReceivedAction: dummyAction))
                            .ShouldThrow <SubscriptionException>();

            exception.Message
            .Should()
            .Be("403:/meta/subscribe:Forbidden channel");
        }
Example #10
0
        public async Task Unsubscribe_not_currently_subscribed()
        {
            // arrange
            _fayeServerProcess.StartThinServer();
            var socket = new WebSocketClient(uri: TEST_SERVER_URL);

            SetupWebSocket(socket);
            InstantiateFayeClient();
            _connection = await _fayeClient.Connect();

            const string channelWeDidntSubscribeTo = "/foobar";

            // act + assert
            var exception = await _connection.InvokingAsync(c => c.Unsubscribe(channelWeDidntSubscribeTo))
                            .ShouldThrow <SubscriptionException>();

            var expectedError = string.Format(FayeConnection.NOT_SUBSCRIBED,
                                              channelWeDidntSubscribeTo);

            exception.Message
            .Should()
            .Be(expectedError);
        }
Example #11
0
        public async Task Connect_lost_connection_comes_back_after_attempt_to_publish()
        {
            // arrange// port 8133
            const int inputPort = THIN_SERVER_PORT + 1;

            _fayeServerProcess.StartThinServer();
            _socatInterceptor = StartWritableSocket(hostname: "localhost",
                                                    inputPort: inputPort);
            const string urlThroughSocat = "ws://localhost:8133/bayeux";
            var          socket          = new WebSocketClient(uri: urlThroughSocat);

            SetupWebSocket(socket);
            InstantiateFayeClient();
            _connection = await _fayeClient.Connect();

            var          tcs           = new TaskCompletionSource <string>();
            const string channelName   = "/somechannel";
            var          messageToSend = new TestMsg {
                Stuff = "the message"
            };
            var lostTcs = new TaskCompletionSource <bool>();

            _connection.ConnectionLost += (sender,
                                           args) => lostTcs.SetResult(true);
            var json = JsonConvert.SerializeObject(messageToSend);

            // act
            await _connection.Subscribe(channelName,
                                        tcs.SetResult);

            // ReSharper disable once CSharpWarnings::CS4014
            Task.Factory.StartNew(() =>
            {
                Logger.Info("Killing socat");
                _socatInterceptor.Kill();
                Logger.Info("Sleeping");
                Thread.Sleep(5.Seconds());
                Logger.Info("Restarting socat");
                _socatInterceptor = StartWritableSocket(hostname: "localhost",
                                                        inputPort: inputPort);
            });
            Logger.Info("Waiting for websocket to acknowledge disconnect");
            await lostTcs.Task.WithTimeout(t => t,
                                           20.Seconds());

            Logger.Info("Disconnect acknowledged, publishing");
            await _connection.Publish(channel : channelName,
                                      message : json);

            // assert
            var task   = tcs.Task;
            var result = await task.Timeout(20.Seconds());

            if (result == Result.Timeout)
            {
                Assert.Fail("Timed out waiting for pub/sub to work");
            }
            var jsonReceived   = task.Result;
            var objectReceived = JsonConvert.DeserializeObject <TestMsg>(jsonReceived);

            objectReceived
            .ShouldBeEquivalentTo(messageToSend);
        }