Esempio n. 1
0
        public void ReadOutMessagePartiallyReceived()
        {
            // Console.WriteLine($"{20:X16}");

            // We say 10 bytes are available, but the length of the message will say 100
            Socket.ExpectConnected(() => true);

            Socket.ExpectAvailable(() => 10);

            Socket.ExpectReceive(data => {
                data[0] = 0x64;
                return(10);
            });

            Assert.Equal(0, _cc.ReceiveNext().Length);

            Socket.ExpectConnected(() => true);
            Socket.ExpectAvailable(() => 100);

            Socket.ExpectReceive(data =>
            {
                data[90] = 0x14;
                return(100);
            });

            Assert.Equal(100, _cc.ReceiveNext().Length);

            Socket.ExpectConnected(() => true);
            Socket.ExpectAvailable(() => 10);

            Socket.ExpectReceive(data => 10);

            Assert.Equal(20, _cc.ReceiveNext().Length);

            Socket.ExpectConnected(() => true);
            Socket.ExpectAvailable(() => 0);

            Assert.Equal(0, _cc.ReceiveNext().Length);

            Socket.ExpectAllDone();
        }
Esempio n. 2
0
        public void terab_utxo_get_block()
        {
            terab_utxo_commit_block();

            _socket.ExpectConnected(() => true);
            _socket.ExpectConnected(() => true);
            _socket.ExpectConnected(() => true);
            _socket.ExpectAvailable(() => GetBlockHandle.SizeInBytes);

            _socket.ExpectReceive(data =>
            {
                data.Clear();
                Assert.True(data.Length >= GetBlockHandle.SizeInBytes);

                var getHandle = new GetBlockHandle(1, 0, BlockId.Genesis);
                MessageSerializers.ClientSerializeGetBlockHandle(getHandle, data);

                return(GetBlockHandle.SizeInBytes);
            });

            _socket.ExpectConnected(() => true);
            _socket.ExpectAvailable(() => 0);

            _socket.ExpectConnected(() => true);
            _socket.ExpectSend(data =>
            {
                Assert.Equal(BlockHandleResponse.SizeInBytes, data.Length);
                BlockHandleResponse response = MessageSerializers.ClientDeserializeBlockHandleResponse(data);
                Assert.Equal(BlockAlias.Genesis, response.BlockHandle);
                return(BlockHandleResponse.SizeInBytes);
            });

            _dispatcher.ListenToConnections();

            _controllerThread.DoWork();

            _dispatcher.SendResponses();

            _socket.ExpectAllDone();
        }
Esempio n. 3
0
        public void ReadMultipleMessagesFromClient()
        {
            var queue   = new ConcurrentQueue <ClientConnection>();
            var socket  = new MockSocket();
            var client1 = new ClientConnection(socket, ClientId.Next(), 100);
            var client2 = new ClientConnection(socket, ClientId.Next(), 100);

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

            // Client1 messages
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 600);
            socket.ExpectReceive(data =>
            {
                data.Clear();
                // Three messages, of sizes 200, 300 and 100
                BitConverter.GetBytes(200).CopyTo(data);
                BitConverter.GetBytes(300).CopyTo(data.Slice(200));
                BitConverter.GetBytes(100).CopyTo(data.Slice(500));
                return(600);
            });
            // Reading second message
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);
            // Reading third message
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);
            // Exiting loop
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);

            // Client2 messages
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 100);
            socket.ExpectReceive(data =>
            {
                data.Clear();
                BitConverter.GetBytes(70).CopyTo(data);
                BitConverter.GetBytes(30).CopyTo(data.Slice(70));
                return(100);
            });
            // Reading second message
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);
            // Exiting loop
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);

            // Some more messages for Client1
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 60);
            socket.ExpectReceive(data =>
            {
                data.Clear();
                // Two messages, of sizes 10 and 40
                BitConverter.GetBytes(25).CopyTo(data);
                BitConverter.GetBytes(35).CopyTo(data.Slice(25));
                return(60);
            });
            // Reading second message
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);
            // Exiting loop
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);

            // No more messages for Client2
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);

            queue.Enqueue(client1);
            queue.Enqueue(client2);
            dispatcher.DequeueNewClients();
            // Client1 reading 3 messages, Client2 two
            dispatcher.ListenToConnections();
            // Client1 reading 2 more messages, Client2 none
            dispatcher.ListenToConnections();

            socket.ExpectAllDone();
        }
Esempio n. 4
0
        public void FillShardedInboxes()
        {
            var queue   = new ConcurrentQueue <ClientConnection>();
            var socket  = new MockSocket();
            var client1 = new ClientConnection(socket, ClientId.Next(), 100);
            var client2 = new ClientConnection(socket, ClientId.Next(), 100);

            BoundedInbox[] threadInboxes =
            {
                BoundedInbox.Create(), BoundedInbox.Create()
            };

            var dispatcher = new Dispatcher(queue, 3, BoundedInbox.Create(),
                                            threadInboxes, BoundedInbox.Create());

            // Client1 sharded message
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => LargeMessageSize);
            socket.ExpectReceive(data =>
            {
                data.Clear();
                BitConverter.GetBytes(LargeMessageSize).CopyTo(data);
                data[ClientServerMessage.ShardedIndStart] = 0xFF; // 0xFF: all bits set to one, so "sharded" is set to 1 for sure
                data[ClientServerMessage.PayloadStart]    = 0x01;
                return(LargeMessageSize);
            });
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);

            // Client2 sharded message
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => LargeMessageSize);
            socket.ExpectReceive(data =>
            {
                data.Clear();
                BitConverter.GetBytes(LargeMessageSize).CopyTo(data);
                data[12] = 0xFF;
                return(LargeMessageSize);
            });
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);

            // Client1 sharded message, fills up first sharded inbox
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => LargeMessageSize);
            socket.ExpectReceive(data =>
            {
                data.Clear();
                BitConverter.GetBytes(LargeMessageSize).CopyTo(data);
                data[ClientServerMessage.ShardedIndStart] = 0xFF; // 0xFF: all bits set to one, so "sharded" is set to 1 for sure
                data[ClientServerMessage.PayloadStart]    = 0x00;
                return(LargeMessageSize);
            });
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);

            // Client2 sharded message, fills up second sharded inbox
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => LargeMessageSize);
            socket.ExpectReceive(data =>
            {
                data.Clear();
                BitConverter.GetBytes(LargeMessageSize).CopyTo(data);
                data[ClientServerMessage.ShardedIndStart] = 0xFF; // 0xFF: all bits set to one, so "sharded" is set to 1 for sure
                data[ClientServerMessage.PayloadStart]    = 0x01;
                return(LargeMessageSize);
            });
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);

            // Read and get error message server busy writing into second sharded inbox
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 20);
            socket.ExpectReceive(data =>
            {
                data.Clear();
                BitConverter.GetBytes(20).CopyTo(data);
                data[ClientServerMessage.ShardedIndStart] = 0xFF;
                data[ClientServerMessage.PayloadStart]    = 0x01;
                return(20);
            });
            socket.ExpectConnected(() => true);
            socket.ExpectSend(data =>
            {
                Assert.Equal(ClientServerMessage.PayloadStart, data.Length);
                Assert.Equal(data[13], (byte)MessageType.ServerBusy);
                return(ClientServerMessage.PayloadStart);
            });
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);

            // Read and get error message server busy writing into first sharded inbox
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 20);
            socket.ExpectReceive(data =>
            {
                data.Clear();
                BitConverter.GetBytes(20).CopyTo(data);
                data[ClientServerMessage.ShardedIndStart] = 0xFF; // 0xFF: all bits set to one, so "sharded" is set to 1 for sure
                data[ClientServerMessage.PayloadStart]    = 0x00;
                return(20);
            });
            socket.ExpectConnected(() => true);
            socket.ExpectSend(data =>
            {
                Assert.Equal(ClientServerMessage.PayloadStart, data.Length);
                Assert.Equal(MessageType.ServerBusy, ClientServerMessage.GetMessageType(data));
                return(ClientServerMessage.PayloadStart);
            });
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);

            queue.Enqueue(client1);
            queue.Enqueue(client2);
            dispatcher.DequeueNewClients();
            // Client1 into sharded inbox 1, Client2 into sharded inbox 2
            dispatcher.ListenToConnections();
            // Client1 filling up sharded inbox 2, Client2 filling up sharded inbox 1
            dispatcher.ListenToConnections();
            // Errors when trying to write into any inbox
            dispatcher.ListenToConnections();

            socket.ExpectAllDone();
        }
Esempio n. 5
0
        public void FillControllerInbox()
        {
            var queue   = new ConcurrentQueue <ClientConnection>();
            var socket  = new MockSocket();
            var client1 = new ClientConnection(socket, ClientId.Next(), 100);
            var client2 = new ClientConnection(socket, ClientId.Next(), 100);


            // Client1 empty inbox
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);
            // Client2 unsharded message
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => LargeMessageSize);
            socket.ExpectReceive(data =>
            {
                data.Clear();
                BitConverter.GetBytes(LargeMessageSize).CopyTo(data);
                return(LargeMessageSize);
            });
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);

            // Client1 unsharded message, fills up ControllerInbox
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => LargeMessageSize);
            socket.ExpectReceive(data =>
            {
                data.Clear();
                BitConverter.GetBytes(LargeMessageSize).CopyTo(data);
                return(LargeMessageSize);
            });
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);

            // Client2 unsharded message, gets BufferFull response
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => ClientServerMessage.PayloadStart); // minimal valid message size
            socket.ExpectReceive(data =>
            {
                data.Clear();
                BitConverter.GetBytes(ClientServerMessage.PayloadStart).CopyTo(data);
                return(ClientServerMessage.PayloadStart);
            });
            socket.ExpectConnected(() => true);
            socket.ExpectSend(data =>
            {
                Assert.Equal(ClientServerMessage.PayloadStart, data.Length);
                Assert.Equal(data[13], (int)MessageType.ServerBusy);
                return(ClientServerMessage.PayloadStart);
            });
            socket.ExpectConnected(() => true);
            socket.ExpectAvailable(() => 0);

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

            queue.Enqueue(client1);
            queue.Enqueue(client2);
            dispatcher.DequeueNewClients();
            // Client1 empty inbox, Client2 LargeMessageSize bytes unsharded
            dispatcher.ListenToConnections();
            // Client1 LargeMessageSize bytes unsharded, Client2 10 bytes, error message
            dispatcher.ListenToConnections();

            socket.ExpectAllDone();
        }