Example #1
0
        public void RemoveDeadClients()
        {
            var socket          = new MockSocket();
            var dispatcherInbox = new BoundedInbox();
            var client          = new ConnectionController(dispatcherInbox, socket, ClientId.Next());

            client.OnRequestReceived = () => { };

            socket.ExpectConnected(() => false);

            var dispatcher = new DispatchController(dispatcherInbox, new BoundedInbox(), new BoundedInbox[4],
                                                    new IdentityHash());

            dispatcher.OnConnectionAccepted = (_) => { };
            dispatcher.AddConnection(client);
            dispatcher.HandleNewConnection();

            var firstMessage = new byte[100];
            var message      = new Message(firstMessage);

            message.Header.ClientId           = client.ClientId;
            message.Header.MessageSizeInBytes = 100;
            message.Header.MessageKind        = MessageKind.CloseConnection;

            Assert.True(dispatcherInbox.TryWrite(firstMessage));

            dispatcher.HandleRequest();

            socket.ExpectAllDone();
        }
Example #2
0
        public void Setup()
        {
            var dispatchInbox = new BoundedInbox();
            var chainInbox    = new BoundedInbox();
            var coinInboxes   = new BoundedInbox[ShardCount];

            for (var i = 0; i < coinInboxes.Length; i++)
            {
                coinInboxes[i] = new BoundedInbox();
            }

            _dispatcher = new DispatchController(
                dispatchInbox,
                chainInbox,
                coinInboxes,
                new IdentityHash());

            // Configure the wake-up callbacks to do nothing.
            _dispatcher.OnBlockMessageDispatched = () => { };
            for (var i = 0; i < _dispatcher.OnCoinMessageDispatched.Length; i++)
            {
                _dispatcher.OnCoinMessageDispatched[i] = () => { }
            }
            ;

            _store = new VolatileChainStore();

            _chainController = new ChainController(
                _store,
                chainInbox,
                dispatchInbox,
                lineage => { });

            var c1 = ClientId.Next();

            _clientConn = new ConnectionController(dispatchInbox, _socket, c1);
            _handleMask = c1.Mask;

            _dispatcher.AddConnection(_clientConn);
            _dispatcher.OnConnectionAccepted = _ => { };
            _dispatcher.HandleNewConnection();

            _c0 = new ClientId(0);
        }
        /// <summary>
        /// DIを実行する
        /// </summary>
        /// <param name="appSettings">アプリケーション設定を上書きする場合は指定する</param>
        /// <param name="testLogs">ログの格納先</param>
        /// <param name="exceptionMethodName">例外を発生させるサービスのメソッド名</param>
        private void DependencyInjection(Dictionary <string, string> appSettings = null, List <TestLog> testLogs = null, string exceptionMethodName = null)
        {
            // DI前のFailureBlobを取得
            var locaSettingsBuilder  = new TestDiProviderBuilder();
            var locaSettingsProvider = locaSettingsBuilder.Build();

            _failureBlob = locaSettingsProvider.GetService <FailureBlob>();

            var builder = new TestDiProviderBuilder <AppSettings>(FunctionsHostBuilderExtend.AddUtility);

            builder.ServiceCollection.AddTransient <DispatchController>();
            builder.ServiceCollection.AddTransient <FailureBlob>();

            if (appSettings != null)
            {
                builder.AddConfigures(appSettings);
            }

            Mock <DateTimeProvider> timeProviderMock = new Mock <DateTimeProvider>();

            timeProviderMock.SetupGet(tp => tp.UtcNow).Returns(TestTime);
            builder.ServiceCollection.AddSingleton <ITimeProvider>(timeProviderMock.Object);

            if (testLogs != null)
            {
                builder.ServiceCollection.AddSingleton <ILogger <DispatchService> >(new TestLogger <DispatchService>(testLogs));
            }

            if (!string.IsNullOrEmpty(exceptionMethodName))
            {
                builder.ServiceCollection.AddTransient <IDispatchService, DispatchServiceMock>();
            }

            ServiceProvider provider = builder.Build();

            _diAppSettings = provider.GetService <AppSettings>();
            _target        = provider.GetService <DispatchController>();

            var dispatchServiceMock = provider.GetService <IDispatchService>() as DispatchServiceMock;

            dispatchServiceMock?.Init(exceptionMethodName);
        }
Example #4
0
        public void QueueTooManyClients()
        {
            var socket          = new MockSocket();
            var dispatcherInbox = new BoundedInbox();
            var dispatcher      = new DispatchController(dispatcherInbox, new BoundedInbox(), new BoundedInbox[64],
                                                         new IdentityHash());

            dispatcher.OnConnectionAccepted = (_) => { };

            for (var i = 0; i < Constants.MaxActiveConnections; i++)
            {
                var client0 = new ConnectionController(dispatcherInbox, socket, ClientId.Next());
                client0.OnRequestReceived = () => { };
                dispatcher.AddConnection(client0);
                dispatcher.HandleNewConnection();
            }

            var client1 = new ConnectionController(dispatcherInbox, socket, ClientId.Next());

            client1.OnRequestReceived = () => { };

            socket.ExpectSend(data =>
            {
                Assert.Equal(ProtocolErrorResponse.SizeInBytes, data.Length);

                var errorMessage = new ProtocolErrorResponse(data);
                Assert.Equal(MessageKind.ProtocolErrorResponse, errorMessage.MessageHeader.MessageKind);
                Assert.Equal(ProtocolErrorStatus.TooManyActiveClients, errorMessage.Status);

                return(data.Length);
            });

            dispatcher.AddConnection(client1);
            dispatcher.HandleNewConnection();
            client1.HandleResponse();
            socket.ExpectAllDone();
        }
Example #5
0
        /// <summary>
        /// DIを実行する
        /// </summary>
        /// <param name="appSettings">アプリケーション設定を上書きする場合は指定する</param>
        /// <param name="testLogs">ログの格納先</param>
        private void DependencyInjection(Dictionary <string, string> appSettings = null, List <TestLog> testLogs = null)
        {
            var builder = new TestDiProviderBuilder <AppSettings>(FunctionsHostBuilderExtend.AddUtility);

            builder.ServiceCollection.AddTransient <DispatchController>();
            builder.ServiceCollection.AddTransient <FailureBlob>();
            builder.AddConfigures(appSettings);

            Mock <DateTimeProvider> timeProviderMock = new Mock <DateTimeProvider>();

            timeProviderMock.SetupGet(tp => tp.UtcNow).Returns(TestTime);
            builder.ServiceCollection.AddSingleton <ITimeProvider>(timeProviderMock.Object);

            if (testLogs != null)
            {
                builder.ServiceCollection.AddSingleton <ILogger <DispatchService> >(new TestLogger <DispatchService>(testLogs));
            }

            ServiceProvider provider = builder.Build();

            failureBlob = provider.GetService <FailureBlob>();
            settings    = provider.GetService <AppSettings>() as AppSettings;
            target      = provider.GetService <DispatchController>();
        }
Example #6
0
        public void SendAnswers()
        {
            var dispatcherInbox = new BoundedInbox();
            var socket1         = new MockSocket();
            var socket2         = new MockSocket();
            var client1         = new ConnectionController(dispatcherInbox, socket1, ClientId.Next());
            var client2         = new ConnectionController(dispatcherInbox, socket2, ClientId.Next());

            client1.OnRequestReceived = () => { };
            client2.OnRequestReceived = () => { };

            var dispatcher = new DispatchController(dispatcherInbox, new BoundedInbox(), new BoundedInbox[2],
                                                    new IdentityHash());

            // Nil handling of notifications
            dispatcher.OnBlockMessageDispatched = () => { };
            for (var i = 0; i < dispatcher.OnCoinMessageDispatched.Length; i++)
            {
                dispatcher.OnCoinMessageDispatched[i] = () => { }
            }
            ;
            dispatcher.OnConnectionAccepted = (_) => { };


            var firstMessage = new byte[Constants.MaxRequestSize];
            var message      = new Message(firstMessage);

            message.Header.ClientId           = client2.ClientId;
            message.Header.MessageSizeInBytes = Constants.MaxRequestSize;
            message.Header.MessageKind        = MessageKind.GetCoinResponse;


            var secondMessage = new byte[Constants.MaxRequestSize];

            message = new Message(secondMessage);
            message.Header.ClientId           = client1.ClientId;
            message.Header.MessageSizeInBytes = Constants.MaxRequestSize;
            message.Header.MessageKind        = MessageKind.GetCoinResponse;

            var thirdMessage = new byte[50];

            message = new Message(thirdMessage);
            message.Header.ClientId           = client1.ClientId;
            message.Header.MessageSizeInBytes = 50;
            message.Header.MessageKind        = MessageKind.GetCoinResponse;


            var fourthMessage = new byte[50];

            message = new Message(fourthMessage);
            message.Header.ClientId           = client2.ClientId;
            message.Header.MessageSizeInBytes = 50;
            message.Header.MessageKind        = MessageKind.GetCoinResponse;

            dispatcher.AddConnection(client1);
            dispatcher.AddConnection(client2);
            dispatcher.HandleNewConnection();
            dispatcher.HandleNewConnection();


            // Write messages into dispatcher buffer for both clients
            for (var i = 0; i < Constants.SocketSendBufferSize / Constants.MaxRequestSize; i++)
            {
                socket1.ExpectConnected(() => true);
                socket2.ExpectConnected(() => true);

                Assert.True(dispatcherInbox.TryWrite(firstMessage));
                Assert.True(dispatcherInbox.TryWrite(secondMessage));

                // Try sending the answers, fails
                dispatcher.HandleRequest();
                dispatcher.HandleRequest();
            }

            // Try sending message to first client, socket gets closed
            socket1.ExpectConnected(() => true);
            socket1.ExpectClose();
            // Try sending message to second client, socket gets closed
            socket2.ExpectConnected(() => true);
            socket2.ExpectClose();

            Assert.True(dispatcherInbox.TryWrite(thirdMessage));
            Assert.True(dispatcherInbox.TryWrite(fourthMessage));
            dispatcher.HandleRequest();
            dispatcher.HandleRequest();

            socket1.ExpectAllDone();
            socket2.ExpectAllDone();
        }
    }
Example #7
0
        public void FillChainControllerInbox()
        {
            var socket1         = new MockSocket();
            var socket2         = new MockSocket();
            var dispatcherInbox = new BoundedInbox();
            var client1         = new ConnectionController(dispatcherInbox, socket1, ClientId.Next());
            var client2         = new ConnectionController(dispatcherInbox, socket2, ClientId.Next());

            client1.OnRequestReceived = () => { };
            client2.OnRequestReceived = () => { };

            Func <int, int, MockSocket.SpanToInt> func = (s1, s2) =>
            {
                return(data =>
                {
                    data.Clear();
                    BinaryPrimitives.TryWriteInt32LittleEndian(data, s1);
                    // TODO: [vermorel] PingChainController has been removed, logic need to be upgraded.
                    //MessageKind.PingChainController.WriteTo(data.Slice(MessageHeaderHelper.MessageKindStart));
                    return s2;
                });
            };

            socket2.ExpectReceive(func(LargeMessageSize, MessageHeader.SizeInBytes));
            socket2.ExpectReceive(func(LargeMessageSize, LargeMessageSize));
            socket1.ExpectReceive(func(LargeMessageSize, MessageHeader.SizeInBytes));
            socket1.ExpectReceive(func(LargeMessageSize, LargeMessageSize));
            // request too short
            var bodyStart = sizeof(int) + RequestId.SizeInBytes + ClientId.SizeInBytes + sizeof(MessageKind);

            socket2.ExpectReceive(func(bodyStart, bodyStart));


            socket2.ExpectSend(data =>
            {
                Assert.Equal(ProtocolErrorResponse.SizeInBytes, data.Length);
                var message = new ProtocolErrorResponse(data);
                Assert.Equal(MessageKind.ProtocolErrorResponse, message.MessageHeader.MessageKind);
                Assert.Equal(ProtocolErrorStatus.RequestTooShort, message.Status);

                return(ProtocolErrorResponse.SizeInBytes);
            });

            socket2.ExpectConnected(() => true);
            socket1.ExpectConnected(() => true);

            var dispatcher = new DispatchController(dispatcherInbox,
                                                    new BoundedInbox(Constants.MaxResponseSize),
                                                    Enumerable.Range(0, 32).Select(x => new BoundedInbox()).ToArray(),
                                                    new IdentityHash());

            // Nil handling of notifications
            dispatcher.OnBlockMessageDispatched = () => { };
            for (var i = 0; i < dispatcher.OnCoinMessageDispatched.Length; i++)
            {
                dispatcher.OnCoinMessageDispatched[i] = () => { }
            }
            ;

            dispatcher.OnConnectionAccepted = (_) => { };
            dispatcher.AddConnection(client1);
            dispatcher.AddConnection(client2);

            dispatcher.HandleNewConnection();
            dispatcher.HandleNewConnection();
            client1.HandleRequest();
            client2.HandleRequest();
            client2.HandleRequest();
            client2.HandleResponse();
            dispatcher.HandleRequest();
            dispatcher.HandleRequest();
            dispatcher.HandleRequest();

            socket1.ExpectAllDone();
            socket2.ExpectAllDone();
        }