Esempio n. 1
0
        public void SetUp()
        {
            clientConnections = new List <IConnection>();
            serverConnections = new List <IConnection>();

            var config = new Config {
                MaxConnections = ClientCount
            };

            maxFragmentMessageSize = config.MaxReliableFragments * (config.MaxPacketSize - AckSystem.MIN_RELIABLE_FRAGMENT_HEADER_SIZE);
            NotifyWaitTime         = config.TimeBeforeEmptyAck * 2;


            server  = new PeerInstanceWithSocket(config);
            clients = new PeerInstanceWithSocket[ClientCount];
            Action <IConnection> serverConnect = (conn) => serverConnections.Add(conn);

            server.peer.OnConnected += serverConnect;

            server.peer.Bind(TestEndPoint.CreateSubstitute());
            for (int i = 0; i < ClientCount; i++)
            {
                clients[i] = new PeerInstanceWithSocket(config);
                clientConnections.Add(clients[i].peer.Connect(server.endPoint));
            }

            UpdateAll();
        }
Esempio n. 2
0
        public void BindShoudlCallSocketBind()
        {
            var endPoint = TestEndPoint.CreateSubstitute();

            peer.Bind(endPoint);

            socket.Received(1).Bind(Arg.Is(endPoint));
        }
Esempio n. 3
0
        public void ConnectShouldReturnANewConnection()
        {
            IEndPoint   endPoint = TestEndPoint.CreateSubstitute();
            IConnection conn     = peer.Connect(endPoint);

            Assert.That(conn, Is.TypeOf <Connection>(), "returned type should be connection");
            Assert.That(conn.State, Is.EqualTo(ConnectionState.Connecting), "new connection should be connecting");
        }
Esempio n. 4
0
        public void ConnectShouldSendMessageToSocket()
        {
            IEndPoint endPoint = TestEndPoint.CreateSubstitute();

            peer.Connect(endPoint);

            byte[] expected = connectRequest;

            socket.Received(1).Send(
                Arg.Is(endPoint),
                Arg.Is <byte[]>(actual => actual.AreEquivalentIgnoringLength(expected)),
                Arg.Is(expected.Length)
                );
        }
Esempio n. 5
0
        public IEnumerator ShouldInvokeConnectionFailedIfNoReplyAfterMax()
        {
            IEndPoint   endPoint = TestEndPoint.CreateSubstitute();
            IConnection conn     = peer.Connect(endPoint);

            // wait enough time so that  would have been called
            // make sure to call update so events are invoked
            // 0.5 little extra to be sure
            float end = time.Now + config.MaxConnectAttempts * config.ConnectAttemptInterval + 0.5f;

            while (end > time.Now)
            {
                peer.UpdateTest();
                yield return(null);
            }

            connectAction.DidNotReceiveWithAnyArgs().Invoke(default);
Esempio n. 6
0
        public void ServerAcceptsAllClients()
        {
            server.peer.Bind(TestEndPoint.CreateSubstitute());

            Action <IConnection> connectAction = Substitute.For <Action <IConnection> >();

            server.peer.OnConnected += connectAction;

            for (int i = 0; i < ClientCount; i++)
            {
                // tell client i to connect
                clients[i].peer.Connect(server.endPoint);
                Action <IConnection> clientConnectAction = Substitute.For <Action <IConnection> >();
                clients[i].peer.OnConnected += clientConnectAction;

                // no change untill update
                Assert.That(server.socket.Sent.Count, Is.EqualTo(i));
                connectAction.ReceivedWithAnyArgs(i).Invoke(default);
Esempio n. 7
0
 public static void SetupReceiveCall(this ISocket socket, byte[] data, IEndPoint endPoint = null, int?length = null)
 {
     socket.Poll().Returns(true, false);
     socket
     // when any call
     .When(x => x.Receive(Arg.Any <byte[]>(), out Arg.Any <IEndPoint>()))
     // return the data from endpoint
     .Do(x =>
     {
         var dataArg = (byte[])x[0];
         for (var i = 0; i < data.Length; i++)
         {
             dataArg[i] = data[i];
         }
         x[1] = endPoint ?? TestEndPoint.CreateSubstitute();
     });
     socket.Receive(Arg.Any <byte[]>(), out Arg.Any <IEndPoint>()).Returns(length ?? data.Length);
 }
Esempio n. 8
0
        public void AcceptsConnectionForValidMessage()
        {
            peer.Bind(TestEndPoint.CreateSubstitute());

            var connectAction = Substitute.For <Action <IConnection> >();

            peer.OnConnected += connectAction;

            var endPoint = TestEndPoint.CreateSubstitute();

            socket.SetupReceiveCall(connectRequest, endPoint);
            peer.UpdateTest();

            // server sends accept and invokes event locally
            socket.Received(1).Send(endPoint, Arg.Is <byte[]>(x =>
                                                              x.Length >= 2 &&
                                                              x[0] == (byte)PacketType.Command &&
                                                              x[1] == (byte)Commands.ConnectionAccepted
                                                              ), 2);
            connectAction.ReceivedWithAnyArgs(1).Invoke(default);
Esempio n. 9
0
        public IEnumerator ShouldResendConnectMessageIfNoReply()
        {
            IEndPoint endPoint = TestEndPoint.CreateSubstitute();

            _ = peer.Connect(endPoint);

            byte[] expected = connectRequest;

            // wait enough time so that  would have been called
            // make sure to call update so events are invoked
            // 0.5 little extra to be sure
            float end           = time.Now + config.MaxConnectAttempts * config.ConnectAttemptInterval + 0.5f;
            float nextSendCheck = 0;
            int   sendCount     = 0;

            while (end > time.Now)
            {
                peer.UpdateTest();
                if (nextSendCheck < time.Now)
                {
                    nextSendCheck = time.Now + config.ConnectAttemptInterval * 1.1f;
                    sendCount++;

                    // check send
                    int expectedCount = Mathf.Min(sendCount, config.MaxConnectAttempts);
                    socket.Received(expectedCount).Send(
                        Arg.Is(endPoint),
                        Arg.Is <byte[]>(actual => actual.AreEquivalentIgnoringLength(expected)),
                        Arg.Is(expected.Length)
                        );
                }
                yield return(null);
            }

            // check send is called max attempts times
            socket.Received(config.MaxConnectAttempts).Send(
                Arg.Is(endPoint),
                Arg.Is <byte[]>(actual => actual.AreEquivalentIgnoringLength(expected)),
                Arg.Is(expected.Length)
                );
        }
Esempio n. 10
0
        public void CloseSendsDisconnectMessageToAllConnections()
        {
            var endPoint = TestEndPoint.CreateSubstitute();

            peer.Bind(endPoint);

            var endPoints = new IEndPoint[maxConnections];

            for (var i = 0; i < maxConnections; i++)
            {
                endPoints[i] = TestEndPoint.CreateSubstitute();

                socket.SetupReceiveCall(connectRequest, endPoints[i]);
                peer.UpdateTest();
            }

            for (var i = 0; i < maxConnections; i++)
            {
                socket.ClearReceivedCalls();
            }

            peer.Close();

            var disconnectCommand = new byte[3]
            {
                (byte)PacketType.Command,
                (byte)Commands.Disconnect,
                (byte)DisconnectReason.RequestedByRemotePeer,
            };

            for (var i = 0; i < maxConnections; i++)
            {
                socket.Received(1).Send(
                    Arg.Is(endPoints[i]),
                    Arg.Is <byte[]>(actual => actual.AreEquivalentIgnoringLength(disconnectCommand)),
                    Arg.Is(disconnectCommand.Length)
                    );
            }
        }