コード例 #1
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);
        }
コード例 #2
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");
        }
コード例 #3
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);
        }