示例#1
0
        public void QueueTooManyClients()
        {
            var queue   = new ConcurrentQueue <ClientConnection>();
            var socket  = new MockSocket();
            var client0 = new ClientConnection(socket, ClientId.Next(), 100);
            var client1 = new ClientConnection(socket, ClientId.Next(), 100);
            var client2 = new ClientConnection(socket, ClientId.Next(), 100);
            var client3 = new ClientConnection(socket, ClientId.Next(), 100);

            socket.ExpectConnected(() => true);
            socket.ExpectSend(data =>
            {
                Console.WriteLine(BitConverter.ToString(data.ToArray()));
                Console.WriteLine(BitConverter.ToString(MessageCreationHelper.NoMoreSpaceForClientsMessage));
                Assert.Equal(MessageCreationHelper.NoMoreSpaceForClientsMessage, data.ToArray());
                return(data.Length);
            });
            socket.ExpectClose();

            var dispatcher = new Dispatcher(queue, 3, BoundedInbox.Create(), new BoundedInbox[64],
                                            BoundedInbox.Create());

            queue.Enqueue(client0);
            queue.Enqueue(client1);
            queue.Enqueue(client2);
            queue.Enqueue(client3);
            dispatcher.DequeueNewClients();

            socket.ExpectAllDone();
        }
示例#2
0
        public void FillUpExitBuffer()
        {
            PrepareArrays();

            Socket.ExpectConnected(() => true);
            Socket.ExpectSend(data => {
                Assert.Equal(1000, data.Length);
                return(0);
            });

            Socket.ExpectConnected(() => true);

            Socket.ExpectConnected(() => true);

            Socket.ExpectConnected(() => true);

            Socket.ExpectConnected(() => true);

            Socket.ExpectClose();

            Socket.ExpectConnected(() => false);

            Assert.True(_cc.TryWrite(_bytes1K));
            Assert.True(_cc.TryWrite(_bytes1K));
            Assert.True(_cc.TryWrite(_bytes1K));
            Assert.True(_cc.TryWrite(_bytes996));
            Assert.False(_cc.TryWrite(_bytes10));
            Assert.False(_cc.TryWrite(_bytes4));

            Socket.ExpectAllDone();

            // No more sending possible
            Socket.ExpectConnected(() => false);

            _cc.Send();

            Socket.ExpectAllDone();
        }
示例#3
0
        public void SendAnswers()
        {
            var queue   = new ConcurrentQueue <ClientConnection>();
            var socket  = new MockSocket();
            var outbox  = BoundedInbox.Create();
            var client1 = new ClientConnection(socket, ClientId.Next(), 100);
            var client2 = new ClientConnection(socket, ClientId.Next(), 100);

            queue.Enqueue(client1);
            queue.Enqueue(client2);

            var dispatcher = new Dispatcher(queue, 3, outbox,
                                            new BoundedInbox[2], BoundedInbox.Create());

            // Try sending message to one client, doesn't succeed
            socket.ExpectConnected(() => true);
            socket.ExpectSend(data => 0);
            // Try sending message to the other client, doesn't succeed
            socket.ExpectConnected(() => true);
            socket.ExpectSend(data => 0);
            // Try sending message to first client, socket gets closed
            socket.ExpectConnected(() => true);
            socket.ExpectClose();
            // Try sending message to second client, socket gets closed
            socket.ExpectConnected(() => true);
            socket.ExpectClose();

            var firstMessage = new byte[100];

            BitConverter.GetBytes(100).CopyTo(new Span <byte>(firstMessage));
            client2.ConnectionId.WriteTo(  // send back to client2
                firstMessage.AsSpan(ClientServerMessage.ClientIdStart, ClientServerMessage.ClientIdSizeInBytes));

            var secondMessage = new byte[100];

            BitConverter.GetBytes(100).CopyTo(new Span <byte>(secondMessage));
            client1.ConnectionId.WriteTo( // send back to client1
                secondMessage.AsSpan(ClientServerMessage.ClientIdStart, ClientServerMessage.ClientIdSizeInBytes));

            var thirdMessage = new byte[50];

            BitConverter.GetBytes(50).CopyTo(new Span <byte>(thirdMessage));
            client1.ConnectionId.WriteTo( // send back to client1
                thirdMessage.AsSpan(ClientServerMessage.ClientIdStart, ClientServerMessage.ClientIdSizeInBytes));

            var fourthMessage = new byte[50];

            BitConverter.GetBytes(50).CopyTo(new Span <byte>(fourthMessage));
            client2.ConnectionId.WriteTo( // send back to client2
                fourthMessage.AsSpan(ClientServerMessage.ClientIdStart, ClientServerMessage.ClientIdSizeInBytes));

            // Write messages into dispatcher buffer for both clients
            Assert.True(outbox.TryWrite(firstMessage));
            Assert.True(outbox.TryWrite(secondMessage));
            dispatcher.DequeueNewClients();
            // Try sending the answers, fails
            dispatcher.SendResponses();

            Assert.True(outbox.TryWrite(thirdMessage));
            Assert.True(outbox.TryWrite(fourthMessage));
            dispatcher.SendResponses();

            socket.ExpectAllDone();
        }