示例#1
0
        public void MutexAcquiredWhenRunningServer()
        {
            var mutexName = Guid.NewGuid().ToString("N");
            var host      = new Mock <IClientConnectionHost>(MockBehavior.Strict);

            host
            .Setup(x => x.CreateListenTask(It.IsAny <CancellationToken>()))
            .Returns(() =>
            {
                // Use a thread instead of Task to guarantee this code runs on a different
                // thread and we can validate the mutex state.
                var source = new TaskCompletionSource <bool>();
                var thread = new Thread(_ =>
                {
                    Mutex mutex;
                    Assert.True(Mutex.TryOpenExisting(mutexName, out mutex));
                    Assert.False(mutex.WaitOne(millisecondsTimeout: 0));
                    source.SetResult(true);
                });

                // Synchronously wait here.  Don't returned a Task value because we need to
                // ensure the above check completes before the server hits a timeout and
                // releases the mutex.
                thread.Start();
                source.Task.Wait();

                return(new TaskCompletionSource <IClientConnection>().Task);
            });

            var result = VBCSCompiler.Run(mutexName, host.Object, keepAlive: TimeSpan.FromSeconds(1));

            Assert.Equal(CommonCompiler.Succeeded, result);
        }
示例#2
0
        internal static ServerData CreateServer(
            string pipeName  = null,
            TimeSpan?timeout = null,
            ICompilerServerHost compilerServerHost = null)
        {
            pipeName           = pipeName ?? Guid.NewGuid().ToString();
            compilerServerHost = compilerServerHost ?? new DesktopCompilerServerHost(DefaultClientDirectory, DefaultSdkDirectory);

            var taskSource = new TaskCompletionSource <ServerStats>();
            var cts        = new CancellationTokenSource();
            var thread     = new Thread(_ =>
            {
                var listener = new TestableDiagnosticListener();
                try
                {
                    var clientConnectionHost = new NamedPipeClientConnectionHost(compilerServerHost, pipeName);
                    var mutexName            = BuildProtocolConstants.GetServerMutexName(pipeName);
                    VBCSCompiler.Run(
                        mutexName,
                        clientConnectionHost,
                        listener,
                        timeout ?? TimeSpan.FromMilliseconds(-1),
                        cts.Token);
                }
                finally
                {
                    var serverStats = new ServerStats(connections: listener.ConnectionCount, completedConnections: listener.CompletedCount);
                    taskSource.SetResult(serverStats);
                }
            });

            thread.Start();

            return(new ServerData(cts, taskSource.Task, pipeName));
        }
示例#3
0
            private Task CreateServerCore(string pipeName)
            {
                Action action = () =>
                {
                    var compilerServerHost   = new DesktopCompilerServerHost(ClientDirectory, SdkDirectory);
                    var clientConnectionHost = new NamedPipeClientConnectionHost(compilerServerHost, pipeName);
                    var mutexName            = $"{pipeName}.server";
                    VBCSCompiler.Run(mutexName, clientConnectionHost, TimeSpan.FromSeconds(3));
                };

                var task = new Task(action, TaskCreationOptions.LongRunning);

                task.Start(TaskScheduler.Default);
                return(task);
            }
示例#4
0
        public void MutexStopsServerStarting()
        {
            var mutexName = Guid.NewGuid().ToString("N");

            bool holdsMutex;

            using (var mutex = new Mutex(initiallyOwned: true,
                                         name: mutexName,
                                         createdNew: out holdsMutex))
            {
                Assert.True(holdsMutex);
                try
                {
                    var host   = new Mock <IClientConnectionHost>(MockBehavior.Strict);
                    var result = VBCSCompiler.Run(mutexName, host.Object, keepAlive: null);
                    Assert.Equal(CommonCompiler.Failed, result);
                }
                finally
                {
                    mutex.ReleaseMutex();
                }
            }
        }