Ejemplo n.º 1
0
        public async Task SendMessageAsync_Should_Invoke_MessageProcessor()
        {
            using CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(DefaultTimeOut * 3));
            _ = StartServer(cts.Token, 9988);
            TcpNetworkConnector tcpNetworkConnector = await StartClient(cts.Token, 9988);

            TestRequestMessage message = new TestRequestMessage()
            {
                Message = "Test123"
            };
            await tcpNetworkConnector.SendMessageAsync(message, cts.Token);

            IMessage messageInMessageProcessor = await MessageProcessor.GetMessageAsync(cts.Token);

            Assert.AreEqual(message.Id, messageInMessageProcessor?.Id);
            cts.Cancel();
        }
        public async Task SendMessageAsync_Should_Invoke_MessageProcessor()
        {
            using CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(DefaultTimeOut * 3));
            _ = StartServer(cts.Token, 9977);
            TcpNetworkConnector tcpNetworkConnector = await StartClient(cts.Token, 9977);

            AuthenticateRequest message = new AuthenticateRequest()
            {
                CredentialTypeCode = "Name", Password = "******", Username = "******"
            };
            await tcpNetworkConnector.SendMessageAsync(message, cts.Token);

            IMessage messageInMessageProcessor = await MessageProcessor.GetMessageAsync(cts.Token);

            Assert.AreEqual(message.Id, messageInMessageProcessor?.Id);
            cts.Cancel();
        }
Ejemplo n.º 3
0
        public async Task RunAsync(Action <string> log)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            List <Task> tasks = new List <Task>();

            for (int j = 0; j < ClientCount; j++)
            {
                Task clientTask = Task.Run(async() =>
                {
                    IMessageProcessor messageProcessor   = new ClientMessageProcessor();
                    IMessageSerializer messageSerializer = new MessagePackMessageSerializer();
                    INetworkConnector networkConnector   = new TcpNetworkConnector(messageSerializer, messageProcessor, Host, Port);
                    await networkConnector.ConnectAsync(CancellationToken.None);
                    networkConnector.Start();

                    MessageListener <LoginResponse> loginResponseListener = new MessageListener <LoginResponse>();
                    loginResponseListener.Subscribe(messageProcessor);
                    for (int i = 0; i < MessageCount; i++)
                    {
                        LoginRequest loginRequest = new LoginRequest(Guid.NewGuid(), "Mario", "password");
                        await networkConnector.SendMessageAsync(loginRequest, CancellationToken.None);
                        LoginResponse loginResponse = await loginResponseListener.ReceiveMessageAsync();
                        log.Invoke($"Success: {loginResponse.IsSuccess}, Username: {loginResponse.UserDto.Name}, RequestId: {loginResponse.RequestId}, ResponseId: {loginResponse.Id}");
                    }
                    loginResponseListener.Unsubscribe();
                });
                tasks.Add(clientTask);
            }

            await Task.WhenAll(tasks).ContinueWith(action =>
            {
                stopwatch.Stop();
                log.Invoke($"Messages per second: {TotalMessages / stopwatch.Elapsed.TotalSeconds:N0}");
            });
        }