public async Task Connect_send_receive_and_disconnect()
        {
            // arrange
            _fayeServerProcess.StartThinServer();
            var socket = new WebSocketClient(uri: TEST_SERVER_URL);

            SetupWebSocket(socket);
            InstantiateTransportClient();
            _connection = await _client.Connect();

            var tcs = new TaskCompletionSource <string>();

            _connection.MessageReceived += (sender,
                                            args) => tcs.SetResult(args.Message);
            // act
            _connection.Send(TestMessageStr);
            var response = await tcs.Task.WithTimeout(s => s,
                                                      5.Seconds());

            // assert
            dynamic responseObj = JsonConvert.DeserializeObject <JArray>(response)[0];
            bool    successful  = responseObj.successful;

            successful
            .Should()
            .BeTrue();
        }
Example #2
0
        public void Not_ssl()
        {
            // arrange
            _websocketProcess.StartThinServer();

            // act + assert
            _socket.Invoking(s => s.Open())
            .ShouldThrow <ConnectionException>()
            .WithMessage(
                "Was able to connect to host 'localhost' on port 8132 but SSL handshake failed.  Are you sure SSL is running?")
            .WithInnerException <IOException>()
            .WithInnerMessage("The handshake failed due to an unexpected packet format.")
            ;
        }
Example #3
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 #4
0
        public void No_ssl_start()
        {
            // arrange
            var thin = new ThinServerProcess(thinPort: 1224,
                                             workingDirectory: ".");

            _tcpListener = new TcpListener(IPAddress.Any,
                                           1224);
            _tcpListener.Start();

            // act
            thin.StartThinServer();
            thin.RubyProc.WaitForExit();

            // assert
            CommandExecuted
            .Should()
            .Be(@".\thin  start -R config.ru -p 1224 -V");
        }
Example #5
0
        public void Shutdown()
        {
            // arrange
            var thin = new ThinServerProcess(thinPort: 1224,
                                             workingDirectory: ".");

            _tcpListener = new TcpListener(IPAddress.Any,
                                           1224);
            _tcpListener.Start();

            // act
            thin.StartThinServer();
            thin.GracefulShutdown();

            // assert
            // because of the quick exit, our shutdown file should already be gone
            var exists = File.Exists(thin.ShutdownTriggerPath);

            exists
            .Should()
            .BeFalse();
        }
Example #6
0
        public void Ssl_start()
        {
            // arrange
            var thin = new ThinServerProcess(thinPort: 1224,
                                             workingDirectory: ".")
            {
                ThinSslCertFile = "certfile",
                ThinSslKeyFile  = "keyfile"
            };

            _tcpListener = new TcpListener(IPAddress.Any,
                                           1224);
            _tcpListener.Start();

            // act
            thin.StartThinServer();
            thin.RubyProc.WaitForExit();

            // assert
            CommandExecuted
            .Should()
            .Be(@".\thin  start -R config.ru -p 1224 -V --ssl --ssl-key-file keyfile --ssl-cert-file certfile");
        }
Example #7
0
        public async Task Connect_lost_connection_comes_back_before_publish()
        {
            // 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 json    = JsonConvert.SerializeObject(messageToSend);
            var lostTcs = new TaskCompletionSource <bool>();

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

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

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

            await backTcs.Task.WithTimeout(t => t,
                                           20.Seconds());

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

            // assert
            var task   = tcs.Task;
            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);
        }