private IServiceCollection GetServices(ISocketManager <ISocketClient <Message, Message>, Message> manager,
                                               ISocketClient <Message, Message> gmClient)
        {
            IServiceCollection services = new ServiceCollection();
            var conf = new ServerConfiguration()
            {
                GMPort = 44360, ListenerIP = "127.0.0.1", PlayerPort = 44370
            };

            services.AddSingleton(conf);
            services.AddSingleton(MockGenerator.Get <Serilog.ILogger>());

            services.AddSingleton(manager);

            ServiceShareContainer shareContainer = new ServiceShareContainer
            {
                GameStarted     = false,
                ConfirmedAgents = new Dictionary <int, bool>(),
                GMClient        = gmClient
            };

            services.AddSingleton(shareContainer);

            services.AddSingleton(Mock.Of <IApplicationLifetime>());
            services.AddSingleton(new ServiceSynchronization(2, 2));

            services.AddHostedService <CommunicationService>();

            return(services);
        }
예제 #2
0
        public async void GameMasterStarts()
        {
            // Arrange
            var    source = new CancellationTokenSource();
            string url    = "http://127.0.0.1:3005";

            string[] args    = new string[] { $"urls={url}", "CsPort=1" };
            var      webhost = Utilities.CreateHostBuilder(typeof(GameMaster.Startup), args).
                               ConfigureServices(serv => serv.AddSingleton(MockGenerator.Get <ILogger>())).
                               Build();

            hosts.Add(webhost);

            // Act
            await webhost.StartAsync(source.Token);

            HttpResponseMessage response;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri($"{url}");
                response           = await client.PostAsync("api/InitGame", null);
            }
            await Task.Delay(500);

            source.Cancel();

            // Assert
            Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);

            var gameMaster = webhost.Services.GetService <GameMaster.Models.GM>();

            Assert.False(gameMaster == null, "GameMaster should not be null");
            Assert.True(gameMaster.WasGameInitialized, "Game should be initialized");
        }
예제 #3
0
 public TcpSocketClientTests()
 {
     logger        = MockGenerator.Get <ILogger>();
     tcpClientMock = new Mock <IClient>()
     {
         DefaultValue = DefaultValue.Mock
     };
     tcpClient = tcpClientMock.Object;
 }
예제 #4
0
        public async void PlayersStart()
        {
            // Arrange
            int playersCount = 10;
            var source       = new CancellationTokenSource();

            string[]   argsRed      = new string[] { "TeamID=red", "urls=https://127.0.0.1:0", "CsPort=1" };
            string[]   argsBlue     = new string[] { "TeamID=blue", "urls=https://127.0.0.1:0", "CsPort=1" };
            IWebHost[] webHostsRed  = new IWebHost[playersCount];
            IWebHost[] webHostsBlue = new IWebHost[playersCount];
            for (int i = 0; i < playersCount; ++i)
            {
                webHostsRed[i] = Utilities.CreateHostBuilder(typeof(Player.Startup), argsRed).
                                 ConfigureServices(serv => serv.AddSingleton(MockGenerator.Get <ILogger>())).
                                 Build();
                webHostsBlue[i] = Utilities.CreateHostBuilder(typeof(Player.Startup), argsBlue).
                                  ConfigureServices(serv => serv.AddSingleton(MockGenerator.Get <ILogger>())).
                                  Build();

                hosts.Add(webHostsBlue[i]);
                hosts.Add(webHostsRed[i]);
            }

            // Act
            for (int i = 0; i < playersCount; ++i)
            {
                await webHostsRed[i].StartAsync(source.Token);
                await webHostsBlue[i].StartAsync(source.Token);
            }
            await Task.Delay(500);

            source.Cancel();

            // Assert
            var playerRed = webHostsRed.Last().Services.GetService <Player.Models.Player>();

            Assert.False(playerRed == null, "Player should not be null");
            Assert.True(playerRed.Team == Team.Red, "Player should get color provided via args");

            var playerBlue = webHostsBlue.Last().Services.GetService <Player.Models.Player>();

            Assert.False(playerBlue == null, "Player should not be null");
            Assert.True(playerBlue.Team == Team.Blue, "Player should get color provided via args");
        }
예제 #5
0
        public async void CommunicationServerStarts()
        {
            // Arrange
            var source = new CancellationTokenSource();
            int gmPort = 3003;

            string[] csArgs  = new string[] { $"urls=http://127.0.0.1:1025", "PlayerPort=2", $"GMPort={gmPort}" };
            var      webhost = Utilities.CreateHostBuilder(typeof(CommunicationServer.Startup), csArgs).
                               ConfigureServices(serv => serv.AddSingleton(MockGenerator.Get <ILogger>())).
                               Build();
            string gmUrl = "http://127.0.0.1:4000";

            string[] gmArgs = new string[] { $"urls={gmUrl}", $"CsPort={gmPort}" };
            var      gmHost = Utilities.CreateHostBuilder(typeof(GameMaster.Startup), gmArgs).
                              ConfigureServices(serv => serv.AddSingleton(MockGenerator.Get <ILogger>())).
                              Build();

            hosts.Add(webhost);
            hosts.Add(gmHost);

            // Act
            await webhost.StartAsync(source.Token);

            await gmHost.StartAsync(source.Token);

            HttpResponseMessage response;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri($"{gmUrl}");
                response           = await client.PostAsync("api/InitGame", null);
            }
            await Task.Delay(8000);

            source.Cancel();

            // Assert
            var container = webhost.Services.GetService <ServiceShareContainer>();

            Assert.True(container.GMClient != null, "GMClient in container should not be null");
        }
 public TcpSocketManagerTests()
 {
     logger           = MockGenerator.Get <ILogger>();
     tcpSocketManager = new TcpSocketManager <Message, Message>(logger);
 }
예제 #7
0
        public async Task TestExecuteAsyncShouldReadMessages()
        {
            // Arrange
            IServiceCollection services = new ServiceCollection();

            services.AddSingleton(logger);

            var clientMock = MockGenerator.Get <ISocketClient <Message, Message> >();

            services.AddSingleton(clientMock);
            var queue = new BufferBlock <Message>();
            StartGamePayload payloadStart = new StartGamePayload
            {
                AgentID   = 1,
                AlliesIDs = new int[2] {
                    1, 2
                },
                LeaderID   = 1,
                EnemiesIDs = new int[2] {
                    3, 4
                },
                TeamID    = Team.Red,
                BoardSize = new BoardSize {
                    X = 3, Y = 3
                },
                GoalAreaSize    = 1,
                NumberOfPlayers = new NumberOfPlayers {
                    Allies = 2, Enemies = 2
                },
                NumberOfPieces       = 2,
                NumberOfGoals        = 2,
                Penalties            = new Penalties(),
                ShamPieceProbability = 0.5f,
                Position             = new Position {
                    X = 1, Y = 1
                },
            };
            Message messageStart = new Message()
            {
                MessageID = MessageID.StartGame,
                Payload   = payloadStart,
            };

            queue.Post(messageStart);
            services.AddSingleton(queue);
            services.AddSingleton <PlayerConfiguration>();
            services.AddSingleton(MockGenerator.Get <IStrategy>());
            services.AddSingleton <Models.Player>();
            services.AddSingleton(Mock.Of <IApplicationLifetime>());
            var context = new ServiceSynchronization(1, 1);

            services.AddSingleton(context);

            services.AddHostedService <PlayerService>();
            var serviceProvider = services.BuildServiceProvider();
            var hostedService   = (PlayerService)serviceProvider.GetService <IHostedService>();

            // Act
            int delay = 500;
            await Task.Run(async() =>
            {
                await hostedService.StartAsync(CancellationToken.None);
                await Task.Delay(delay);
                await hostedService.StopAsync(CancellationToken.None);
            });

            // Assert
            Assert.Equal(0, queue.Count);
        }
예제 #8
0
 private void AddLogging(IServiceCollection services)
 {
     services.AddSingleton(MockGenerator.Get <ILogger>());
 }