/// <summary>
        ///     Check if the service is running.
        /// </summary>
        public static bool EnsureRunning(Context context, string scenario, int waitMs)
        {
            var currentUser = UserUtilities.CurrentUserName();

#if !PLATFORM_OSX
            context.TraceMessage(Severity.Debug, $"Opening ready event name=[{scenario}] for user=[{currentUser}] waitMs={waitMs}.");
            var stopwatch = Stopwatch.StartNew();

            bool running = ensureRunningCore();

            if (running)
            {
                context.TraceMessage(Severity.Debug, $"Opened ready event name=[{scenario}] by {stopwatch.ElapsedMilliseconds}ms.");
            }

            return(running);

            bool ensureRunningCore()
            {
                try
                {
                    if (!IpcUtilities.TryOpenExistingReadyWaitHandle(scenario, out EventWaitHandle readyEvent, waitMs))
                    {
                        context.TraceMessage(Severity.Debug, "Ready event does not exist");
                        return(false);
                    }

                    using (readyEvent)
                    {
                        var waitMsRemaining = Math.Max(0, waitMs - (int)stopwatch.ElapsedMilliseconds);
                        if (!readyEvent.WaitOne(waitMsRemaining))
                        {
                            context.TraceMessage(Severity.Debug, "Ready event was opened but remained unset until timeout");
                            return(false);
                        }

                        return(true);
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    context.TraceMessage(Severity.Debug, "Ready event exists, but user does not have acceptable security access");
                }

                return(false);
            }
#else
            context.TraceMessage(Severity.Debug, $"Not validating ready event (OSX) name=[{scenario}] for user=[{currentUser}] waitMs={waitMs}");
            return(true);
#endif
        }
Example #2
0
        public void IpcDoestNotLastAfterDispose()
        {
            var identifier = $"{nameof(IpcUtilitiesTests)}.{nameof(IpcDoestNotLastAfterDispose)}";

            using (var readyHandle = IpcUtilities.GetReadyWaitHandle(identifier))
            {
                // Ready handle uses ManualReset, so this should leave the handle open for all
                readyHandle.Set();
                readyHandle.WaitOne(100).Should().BeTrue();
            }

            var found = IpcUtilities.TryOpenExistingReadyWaitHandle(identifier, out EventWaitHandle existingReadyHandle);

            found.Should().BeFalse();
            existingReadyHandle.Should().BeNull();
        }