Exemplo n.º 1
0
        public void TestConnectThrowsExceptionIfAlreadyConnected()
        {
            WebSocketJetConnection webSocketJetConnection = new WebSocketJetConnection("ws://172.19.191.179:8081");
            ITimer timer = A.Dummy <ITimer>();

            webSocketJetConnection.ConnectTimer = timer;
            IWebSocket webSocket = WebSocketFakesFactory.CreateWebSocketThatConnectsSuccessful();

            webSocketJetConnection.SetWebSocket(webSocket);

            webSocketJetConnection.Connect(A.Dummy <Action <bool> >(), 1000.0);

            Assert.Throws <JetPeerException>(() =>
            {
                //Second call.
                webSocketJetConnection.Connect(A.Dummy <Action <bool> >(), 1000.0);
            });
        }
Exemplo n.º 2
0
        public void TestSendCallsWebSocketSend()
        {
            WebSocketJetConnection webSocketJetConnection = new WebSocketJetConnection("ws://172.19.191.179:8081");
            IWebSocket             webSocket = WebSocketFakesFactory.CreateWebSocketThatConnectsAndClosesSuccessful();

            webSocketJetConnection.SetWebSocket(webSocket);

            webSocketJetConnection.Connect(A.Dummy <Action <bool> >(), 1000.0);
            webSocketJetConnection.SendMessage("msg");
            A.CallTo(() => webSocket.Send("msg")).MustHaveHappened(Repeated.Exactly.Once);
        }
Exemplo n.º 3
0
        public void TestDiconnectCallsWebSocketCloseAsync()
        {
            WebSocketJetConnection webSocketJetConnection = new WebSocketJetConnection("ws://172.19.191.179:8081");
            IWebSocket             webSocket = WebSocketFakesFactory.CreateWebSocketThatConnectsAndClosesSuccessful();

            webSocketJetConnection.SetWebSocket(webSocket);

            webSocketJetConnection.Connect(A.Dummy <Action <bool> >(), 1000.0);
            webSocketJetConnection.Disconnect();

            A.CallTo(() => webSocket.CloseAsync(CloseStatusCode.Away)).MustHaveHappened(Repeated.Exactly.Once);
        }
Exemplo n.º 4
0
        public void TestDisconnectSucceeds()
        {
            WebSocketJetConnection webSocketJetConnection = new WebSocketJetConnection("ws://172.19.191.179:8081");
            IWebSocket             webSocket = WebSocketFakesFactory.CreateWebSocketThatConnectsAndClosesSuccessful();

            webSocketJetConnection.SetWebSocket(webSocket);

            webSocketJetConnection.Connect(A.Dummy <Action <bool> >(), 1000.0);
            webSocketJetConnection.Disconnect();

            Assert.IsFalse(webSocketJetConnection.IsConnected, $"Expected {webSocketJetConnection.IsConnected} to be false but received true.");
        }
Exemplo n.º 5
0
        public void TestConnectSucceeds()
        {
            WebSocketJetConnection webSocketJetConnection = new WebSocketJetConnection("ws://172.19.191.179:8081");
            ITimer timer = A.Dummy <ITimer>();

            webSocketJetConnection.ConnectTimer = timer;
            IWebSocket webSocket = WebSocketFakesFactory.CreateWebSocketThatConnectsSuccessful();

            webSocketJetConnection.SetWebSocket(webSocket);

            webSocketJetConnection.Connect(A.Dummy <Action <bool> >(), 1000.0);
            Assert.IsTrue(webSocketJetConnection.IsConnected);
        }
Exemplo n.º 6
0
        public void TestConnectCallsWebSocketConnectAsync()
        {
            WebSocketJetConnection webSocketJetConnection = new WebSocketJetConnection("ws://172.19.191.179:8081");
            ITimer timer = A.Dummy <ITimer>();

            webSocketJetConnection.ConnectTimer = timer;
            IWebSocket webSocket = A.Fake <IWebSocket>();

            webSocketJetConnection.SetWebSocket(webSocket);

            webSocketJetConnection.Connect(A.Dummy <Action <bool> >(), 1000.0);
            A.CallTo(() => webSocket.ConnectAsync()).MustHaveHappened(Repeated.Exactly.Once);
        }
Exemplo n.º 7
0
        public void TestConnectTimeoutClosesWebSocketIfAlive()
        {
            WebSocketJetConnection webSocketJetConnection = new WebSocketJetConnection("ws://172.19.191.179:8081");
            ITimer     timer     = A.Fake <ITimer>();
            IWebSocket webSocket = WebSocketFakesFactory.CreateWebSocketThatFailsConnectDueTimeout(timer);

            webSocketJetConnection.SetWebSocket(webSocket);
            webSocketJetConnection.ConnectTimer = timer;
            A.CallTo(() => webSocket.IsAlive).Returns(true);
            webSocketJetConnection.Connect(A.Dummy <Action <bool> >(), 1000.0);

            A.CallTo(() => webSocket.Close()).MustHaveHappened(Repeated.Exactly.Once);
        }
Exemplo n.º 8
0
        public void TestConnectTimeoutInvokesCompleteAction()
        {
            WebSocketJetConnection webSocketJetConnection = new WebSocketJetConnection("ws://172.19.191.179:8081");
            ITimer     timer     = A.Fake <ITimer>();
            IWebSocket webSocket = WebSocketFakesFactory.CreateWebSocketThatFailsConnectDueTimeout(timer);

            webSocketJetConnection.SetWebSocket(webSocket);
            webSocketJetConnection.ConnectTimer = timer;
            A.CallTo(() => webSocket.IsAlive).Returns(false);
            Action <bool> completed = A.Fake <Action <bool> >();

            webSocketJetConnection.Connect(completed, 10.0);

            A.CallTo(() => completed(false)).MustHaveHappened(Repeated.Exactly.Once);
        }
Exemplo n.º 9
0
        public void TestConnectCallsTimer()
        {
            WebSocketJetConnection webSocketJetConnection = new WebSocketJetConnection("ws://172.19.191.179:8081");
            IWebSocket             webSocket = WebSocketFakesFactory.CreateWebSocketThatConnectsSuccessful();

            webSocketJetConnection.SetWebSocket(webSocket);
            ITimer timer = A.Fake <ITimer>();

            webSocketJetConnection.ConnectTimer = timer;

            double timeoutMs = 4242.42;

            webSocketJetConnection.Connect(A.Dummy <Action <bool> >(), timeoutMs);

            AssertTimerCallsOnConnect(timer, timeoutMs);
        }
Exemplo n.º 10
0
        public void TestConnectInvokesCompletedAction()
        {
            WebSocketJetConnection webSocketJetConnection = new WebSocketJetConnection("ws://172.19.191.179:8081");
            ITimer timer = A.Dummy <ITimer>();

            webSocketJetConnection.ConnectTimer = timer;
            IWebSocket webSocket = WebSocketFakesFactory.CreateWebSocketThatConnectsSuccessful();

            A.CallTo(() => webSocket.IsAlive).Returns(true);
            webSocketJetConnection.SetWebSocket(webSocket);
            Action <bool> completed = A.Fake <Action <bool> >();

            webSocketJetConnection.Connect(completed, 1000.0);

            A.CallTo(() => completed(true)).MustHaveHappened(Repeated.Exactly.Once);
        }
Exemplo n.º 11
0
        public void TestConnectTimeoutCallsTimer()
        {
            WebSocketJetConnection webSocketJetConnection = new WebSocketJetConnection("ws://172.19.191.179:8081");
            ITimer     timer     = A.Fake <ITimer>();
            IWebSocket webSocket = WebSocketFakesFactory.CreateWebSocketThatFailsConnectDueTimeout(timer);

            webSocketJetConnection.SetWebSocket(webSocket);
            webSocketJetConnection.ConnectTimer = timer;

            Action <bool> connectCallback = A.Fake <Action <bool> >();

            A.CallTo(() => connectCallback(false)).Invokes(() => {
                webSocket.OnOpen += Raise.WithEmpty();
            });

            webSocketJetConnection.Connect(connectCallback, 1234.56);

            AssertTimerCallsOnConnect(timer, 1234.56);
            A.CallTo(() => connectCallback(true)).MustNotHaveHappened();
            A.CallTo(() => connectCallback(false)).MustHaveHappened(Repeated.Exactly.Once);
        }
Exemplo n.º 12
0
        public void TestHandleIncomingMessageInvokesEventHandlerMessageIsText()
        {
            WebSocketJetConnection webSocketJetConnection = new WebSocketJetConnection("ws://172.19.191.179:8081");
            IWebSocket             webSocket = A.Fake <IWebSocket>();

            webSocketJetConnection.SetWebSocket(webSocket);
            webSocketJetConnection.Connect(A.Dummy <Action <bool> >(), 1000.0);
            List <StringEventArgs> receivedEvents = new List <StringEventArgs>();

            webSocketJetConnection.HandleIncomingMessage += delegate(object sender, StringEventArgs args)
            {
                receivedEvents.Add(args);
            };

            int numOfMessageEvents = 5;

            for (int i = 0; i < numOfMessageEvents; i++)
            {
                webSocket.OnMessage += Raise.With(webSocket, CreateMessageEventArgsUsingReflection(true));
            }

            Assert.AreEqual(numOfMessageEvents, receivedEvents.Count);
        }