예제 #1
0
        public async Task SendMessageAfterServerStops_IsFailure()
        {
            var configuration = ApiTestHelper.GetConfiguration();
            var factory       = ApiTestHelper.GetRequestProcessorFactory();
            var serializer    = ApiTestHelper.GetMessageSerializer();

            using var server = new ApiServer(configuration, factory, serializer, LogManager.GetLogger("Server"));
            using var client = new ApiClient(configuration, serializer, LogManager.GetLogger("Client"));

            var result = await server.StartAsync();

            Assert.True(result);

            result = await client.ConnectAsync();

            Assert.True(result);
            Assert.Equal(ConnectionState.Connected, client.State);

            server.Stop();

            var response = await client.SendMessageAsync("Resource", "Action", "Content");

            Assert.False(response.IsSuccess);
            Assert.Equal(ConnectionState.Disconnected, client.State);
        }
예제 #2
0
        public async Task SendMessageWithClientConnect_IsSuccess()
        {
            var configuration = ApiTestHelper.GetConfiguration();
            var factory       = ApiTestHelper.GetRequestProcessorFactory();
            var serializer    = ApiTestHelper.GetMessageSerializer();

            using var server = new ApiServer(configuration, factory, serializer, LogManager.GetLogger("Server"));
            using var client = new ApiClient(configuration, serializer, LogManager.GetLogger("Client"));

            var startResult = await server.StartAsync().ConfigureAwait(false);

            Assert.True(startResult);
            Assert.Equal(ServerState.Started, server.State);

            var connectResult = await client.ConnectAsync().ConfigureAwait(false);

            Assert.True(connectResult);
            Assert.Equal(ConnectionState.Connected, client.State);

            var response = await client.SendMessageAsync("Resource", "Action", "Content").ConfigureAwait(false);

            Assert.NotNull(response);
            Assert.True(response.IsSuccess);
            Assert.Equal("Content", response.Content);
        }
예제 #3
0
        public async Task StartServer_IsSuccess()
        {
            var configuration = ApiTestHelper.GetConfiguration();
            var factory       = ApiTestHelper.GetRequestProcessorFactory();
            var serializer    = ApiTestHelper.GetMessageSerializer();

            using var server = new ApiServer(configuration, factory, serializer, LogManager.GetLogger("Server"));

            var result = await server.StartAsync();

            Assert.True(result);
            Assert.Equal(ServerState.Started, server.State);
        }
예제 #4
0
        public async Task StartServerWithDifferentName_IsSuccess()
        {
            var factory    = ApiTestHelper.GetRequestProcessorFactory();
            var serializer = ApiTestHelper.GetMessageSerializer();

            using var server1 = new ApiServer(new ApiConfiguration("Micser.Test.1"), factory, serializer, LogManager.GetLogger("Server1"));
            using var server2 = new ApiServer(new ApiConfiguration("Micser.Test.2"), factory, serializer, LogManager.GetLogger("Server2"));

            var result1 = await server1.StartAsync();

            var result2 = await server2.StartAsync();

            Assert.True(result1);
            Assert.True(result2);
            Assert.Equal(ServerState.Started, server1.State);
            Assert.Equal(ServerState.Started, server2.State);
        }
예제 #5
0
        public async Task SendMessageOneClientParallelism()
        {
            var configuration = ApiTestHelper.GetConfiguration();
            var factory       = ApiTestHelper.GetRequestProcessorFactory();
            var logger        = LogManager.GetCurrentClassLogger();
            var serializer    = ApiTestHelper.GetMessageSerializer();

            using var server = new ApiServer(configuration, factory, serializer, LogManager.GetLogger("Server"));
            using var client = new ApiClient(configuration, serializer, LogManager.GetLogger("Client"));

            var startResult = await server.StartAsync();

            Assert.True(startResult);
            var connectResult = await client.ConnectAsync();

            Assert.True(connectResult);

            const int count = 1000;
            var       tasks = new Task <ApiResponse> [count];

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            for (var i = 0; i < count; i++)
            {
                tasks[i] = client.SendMessageAsync("Parallel", "Action", new TestData {
                    Value = i
                });
            }

            await Task.WhenAll(tasks);

            stopwatch.Stop();

            logger.Info($"Sent {count} messages. Duration: {stopwatch.Elapsed}");

            for (var i = 0; i < count; i++)
            {
                var response = tasks[i].Result;
                Assert.NotNull(response);
                var testData = response.Content as TestData;
                Assert.NotNull(testData);
                Assert.Equal(i, testData.Value);
            }
        }
예제 #6
0
        public async Task StartServerWithSameName_IsFailure()
        {
            var configuration = ApiTestHelper.GetConfiguration();
            var factory       = ApiTestHelper.GetRequestProcessorFactory();
            var serializer    = ApiTestHelper.GetMessageSerializer();

            using var server1 = new ApiServer(configuration, factory, serializer, LogManager.GetLogger("Server1"));
            using var server2 = new ApiServer(configuration, factory, serializer, LogManager.GetLogger("Server2"));

            var result1 = await server1.StartAsync();

            var result2 = await server2.StartAsync();

            Assert.True(result1);
            Assert.False(result2);
            Assert.Equal(ServerState.Started, server1.State);
            Assert.Equal(ServerState.Stopped, server2.State);
        }
예제 #7
0
        public async Task ConnectMultipleClients_IsFailure()
        {
            var configuration = ApiTestHelper.GetConfiguration();
            var factory       = ApiTestHelper.GetRequestProcessorFactory();
            var serializer    = ApiTestHelper.GetMessageSerializer();

            using var server  = new ApiServer(configuration, factory, serializer, LogManager.GetLogger("Server"));
            using var client1 = new ApiClient(configuration, serializer, LogManager.GetLogger("Client1"));
            using var client2 = new ApiClient(configuration, serializer, LogManager.GetLogger("Client2"));

            var result = await server.StartAsync();

            Assert.True(result);
            result = await client1.ConnectAsync();

            Assert.True(result);
            result = await client2.ConnectAsync();

            Assert.False(result);
        }
예제 #8
0
        public async Task ConnectDisconnect_SetsState()
        {
            var configuration = ApiTestHelper.GetConfiguration();
            var factory       = ApiTestHelper.GetRequestProcessorFactory();
            var serializer    = ApiTestHelper.GetMessageSerializer();

            using var server = new ApiServer(configuration, factory, serializer, LogManager.GetLogger("Server"));
            using var client = new ApiClient(configuration, serializer, LogManager.GetLogger("Client"));

            var result = await server.StartAsync();

            Assert.True(result);

            result = await client.ConnectAsync();

            Assert.True(result);
            Assert.Equal(ConnectionState.Connected, client.State);

            client.Disconnect();
            Assert.Equal(ConnectionState.Disconnected, client.State);
        }