Ejemplo n.º 1
0
            public async Task ConnectToPipe()
            {
                string pipeName = ServerUtil.GetPipeName();

                var oneSec = TimeSpan.FromSeconds(1);

                Assert.False(await tryConnectToNamedPipe((int)oneSec.TotalMilliseconds, cancellationToken: default));

                // Try again with infinite timeout and cancel
                var cts        = new CancellationTokenSource();
                var connection = tryConnectToNamedPipe(Timeout.Infinite, cts.Token);

                Assert.False(connection.IsCompleted);
                cts.Cancel();
                await Assert.ThrowsAnyAsync <OperationCanceledException>(
                    async() => await connection);

                // Create server and try again
                using var serverData = CreateServer(pipeName);
                Assert.True(await tryConnectToNamedPipe(Timeout.Infinite, cancellationToken: default));

                async Task <bool> tryConnectToNamedPipe(int timeoutMs, CancellationToken cancellationToken)
                {
                    using var pipeStream = await BuildServerConnection.TryConnectToServerAsync(pipeName, timeoutMs, _logger, cancellationToken);

                    return(pipeStream != null);
                }
            }
Ejemplo n.º 2
0
        internal static ServerData Create(
            ICompilerServerLogger logger,
            string pipeName = null,
            ICompilerServerHost compilerServerHost     = null,
            IClientConnectionHost clientConnectionHost = null,
            TimeSpan?keepAlive = null)
        {
            // The total pipe path must be < 92 characters on Unix, so trim this down to 10 chars
            pipeName ??= ServerUtil.GetPipeName();
            compilerServerHost ??= BuildServerController.CreateCompilerServerHost(logger);
            clientConnectionHost ??= BuildServerController.CreateClientConnectionHost(pipeName, logger);
            keepAlive ??= TimeSpan.FromMilliseconds(-1);

            var listener           = new TestableDiagnosticListener();
            var serverListenSource = new TaskCompletionSource <bool>();
            var cts       = new CancellationTokenSource();
            var mutexName = BuildServerConnection.GetServerMutexName(pipeName);
            var task      = Task.Run(() =>
            {
                BuildServerController.CreateAndRunServer(
                    pipeName,
                    compilerServerHost,
                    clientConnectionHost,
                    listener,
                    keepAlive: keepAlive,
                    cancellationToken: cts.Token);
                return(listener);
            });

            return(new ServerData(cts, pipeName, logger, task));
        }
        public async Task OnlyStartOneServer()
        {
            ServerData?serverData = null;

            try
            {
                var pipeName         = ServerUtil.GetPipeName();
                var workingDirectory = TempRoot.CreateDirectory().Path;
                for (var i = 0; i < 5; i++)
                {
                    var response = await BuildServerConnection.RunServerBuildRequestAsync(
                        ProtocolUtil.CreateEmptyCSharp(workingDirectory),
                        pipeName,
                        timeoutOverride : Timeout.Infinite,
                        tryCreateServerFunc : (pipeName, logger) =>
                    {
                        Assert.Null(serverData);
                        serverData = ServerData.Create(logger, pipeName);
                        return(true);
                    },
                        Logger,
                        cancellationToken : default);

                    Assert.True(response is CompletedBuildResponse);
                }
            }
            finally
            {
                serverData?.Dispose();
            }
        }
        public async Task FailedServer()
        {
            var pipeName         = ServerUtil.GetPipeName();
            var workingDirectory = TempRoot.CreateDirectory().Path;
            var count            = 0;

            for (var i = 0; i < 5; i++)
            {
                var response = await BuildServerConnection.RunServerBuildRequestAsync(
                    ProtocolUtil.CreateEmptyCSharp(workingDirectory),
                    pipeName,
                    timeoutOverride : Timeout.Infinite,
                    tryCreateServerFunc : (_, _) =>
                {
                    count++;
                    return(false);
                },
                    Logger,
                    cancellationToken : default);

                Assert.True(response is RejectedBuildResponse);
            }

            Assert.Equal(5, count);
        }
Ejemplo n.º 5
0
 public NamedPipeClientConnectionHostTests(ITestOutputHelper testOutputHelper)
 {
     _host = new NamedPipeClientConnectionHost(
         ServerUtil.GetPipeName(),
         new XunitCompilerServerLogger(testOutputHelper)
         );
 }
        public async Task SimulateServerCrashingOnStartup()
        {
            var pipeName = ServerUtil.GetPipeName();
            var ran      = false;
            var response = await BuildServerConnection.RunServerBuildRequestAsync(
                ProtocolUtil.CreateEmptyCSharp(TempRoot.CreateDirectory().Path),
                pipeName,
                timeoutOverride : (int)TimeSpan.FromSeconds(2).TotalMilliseconds,
                tryCreateServerFunc : (_, _) =>
            {
                ran = true;

                // Correct this is a lie. The server did not start. But it also does a nice
                // job of simulating a hung or crashed server.
                return(true);
            },
                Logger,
                cancellationToken : default);

            Assert.True(response is RejectedBuildResponse);
            Assert.True(ran);
        }
 public NamedPipeClientConnectionHostTests()
 {
     _host = new NamedPipeClientConnectionHost(ServerUtil.GetPipeName());
 }