示例#1
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");
        }