Ejemplo n.º 1
0
        async Task RunHeartbeatTest(CancellationToken cancellationToken)
        {
            System.Console.WriteLine("TEST: WATCHDOG HEARTBEAT TEST");
            // enable heartbeats
            await instanceClient.DreamDaemon.Update(new DreamDaemon
            {
                HeartbeatSeconds = 1,
            }, cancellationToken);

            var startJob = await StartDD(cancellationToken).ConfigureAwait(false);

            await WaitForJob(startJob, 40, false, null, cancellationToken);

            // lock on to DD and pause it so it can't heartbeat
            var ddProcs = System.Diagnostics.Process.GetProcessesByName("DreamDaemon").ToList();

            if (ddProcs.Count != 1)
            {
                Assert.Fail($"Incorrect number of DD processes: {ddProcs.Count}");
            }

            using var ddProc = ddProcs.Single();
            IProcessExecutor executor = null;

            executor = new ProcessExecutor(
                new PlatformIdentifier().IsWindows
                                        ? (IProcessFeatures) new WindowsProcessFeatures()
                                        : new PosixProcessFeatures(new Lazy <IProcessExecutor>(() => executor), Mock.Of <IIOManager>(), Mock.Of <ILogger <PosixProcessFeatures> >()),
                Mock.Of <ILogger <ProcessExecutor> >(),
                LoggerFactory.Create(x => { }));
            using var ourProcessHandler = executor
                                          .GetProcess(ddProc.Id);

            // Ensure it's responding to heartbeats
            await Task.WhenAny(Task.Delay(20000), ourProcessHandler.Lifetime);

            Assert.IsFalse(ddProc.HasExited);

            await instanceClient.DreamDaemon.Update(new DreamDaemon
            {
                SoftShutdown = true
            }, cancellationToken);

            ourProcessHandler.Suspend();

            await Task.WhenAny(ourProcessHandler.Lifetime, Task.Delay(TimeSpan.FromMinutes(1)));

            var timeout = 20;

            do
            {
                await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken).ConfigureAwait(false);

                var ddStatus = await instanceClient.DreamDaemon.Read(cancellationToken);

                Assert.AreEqual(1U, ddStatus.HeartbeatSeconds.Value);
                if (ddStatus.Status.Value == WatchdogStatus.Offline)
                {
                    break;
                }

                if (--timeout == 0)
                {
                    Assert.Fail("DreamDaemon didn't shutdown within the timeout!");
                }
            }while (timeout > 0);

            // disable heartbeats
            await instanceClient.DreamDaemon.Update(new DreamDaemon
            {
                HeartbeatSeconds = 0,
            }, cancellationToken);
        }