public void TestRun()
        {
            ILogMessage message1 = Substitute.For <ILogMessage>();
            ILogMessage message2 = Substitute.For <ILogMessage>();
            ILogMessage message3 = Substitute.For <ILogMessage>();
            ILogMessage message4 = Substitute.For <ILogMessage>();
            ILogMessage message5 = Substitute.For <ILogMessage>();
            ILogMessage message6 = Substitute.For <ILogMessage>();
            IMessageQueue <ILogMessage> messageQueue = Substitute.For <IMessageQueue <ILogMessage> >();
            QueueLogRunner logRunner = new QueueLogRunner(messageQueue, 0);
            int            counter   = 0;

            messageQueue.TakeAll().Returns(delegate
            {
                IMessageQueue <ILogMessage> messageQueue2 = Substitute.For <IMessageQueue <ILogMessage> >();
                if (counter == 0)
                {
                    messageQueue2.GetEnumerator().Returns(new ArrayEnumerator <ILogMessage>(message1, message2));
                }
                else if (counter == 1)
                {
                    logRunner.RequestStop(); // Called from Running state
                    messageQueue2.GetEnumerator().Returns(new ArrayEnumerator <ILogMessage>(message3, message4));
                }
                else
                {
                    logRunner.RequestStop(); // Called from StopRequested state
                    messageQueue2.GetEnumerator().Returns(new ArrayEnumerator <ILogMessage>(message5, message6));
                }
                counter++;
                return(messageQueue2);
            });

            IBasicLogger logger = Substitute.For <IBasicLogger>();

            logRunner.Run(logger);

            logRunner.RequestStop(); // Called from Stopped state

            Received.InOrder(delegate
            {
                logger.Log(message1);
                logger.Log(message2);
                logger.Log(message3);
                logger.Log(message4);
                logger.Log(message5);
                logger.Log(message6);
            });
        }
        public void TestRun__AlreadyStarted()
        {
            IMessageQueue <ILogMessage> messageQueue = Substitute.For <IMessageQueue <ILogMessage> >();
            QueueLogRunner logRunner = new QueueLogRunner(messageQueue, 0);
            int            counter   = 0;

            messageQueue.TakeAll().Returns(delegate
            {
                IMessageQueue <ILogMessage> messageQueue2 = Substitute.For <IMessageQueue <ILogMessage> >();
                if (counter == 0)
                {
                    InvalidOperationException ex = Assert.Throws <InvalidOperationException>(delegate
                    {
                        logRunner.Run(Substitute.For <IBasicLogger>());
                    });
                    Assert.Equal("Cannot run from Running state", ex.Message);
                    logRunner.RequestStop();
                    messageQueue2.GetEnumerator().Returns(new ArrayEnumerator <ILogMessage>());
                }
                else
                {
                    InvalidOperationException ex = Assert.Throws <InvalidOperationException>(delegate
                    {
                        logRunner.Run(Substitute.For <IBasicLogger>());
                    });
                    Assert.Equal("Cannot run from StopRequested state", ex.Message);
                    logRunner.RequestStop();
                    messageQueue2.GetEnumerator().Returns(new ArrayEnumerator <ILogMessage>());
                }
                counter++;
                return(messageQueue2);
            });

            IBasicLogger logger = Substitute.For <IBasicLogger>();

            logRunner.Run(logger);

            InvalidOperationException ex2 = Assert.Throws <InvalidOperationException>(delegate
            {
                logRunner.Run(Substitute.For <IBasicLogger>());
            });

            Assert.Equal("Cannot run from Stopped state", ex2.Message);
        }
        public void TestRequestStop__NotStarted()
        {
            QueueLogRunner logRunner = new QueueLogRunner(Substitute.For <IMessageQueue <ILogMessage> >());

            InvalidOperationException ex = Assert.Throws <InvalidOperationException>(delegate
            {
                logRunner.RequestStop();
            });

            Assert.Equal("Cannot request stop from Initialized state", ex.Message);
        }
Example #4
0
        public IEnumerator Run()
        {
            PostPatchLoader.Instance.databaseConfigs = null;

            if (!Directory.Exists(logsDirPath))
            {
                Directory.CreateDirectory(logsDirPath);
            }

            kspLogger.Info("Patching started on a new thread, all output will be directed to " + logPath);

            MessageQueue <ILogMessage> mmLogQueue = new MessageQueue <ILogMessage>();
            QueueLogRunner             logRunner  = new QueueLogRunner(mmLogQueue);
            ITaskStatus loggingThreadStatus       = BackgroundTask.Start(delegate
            {
                using (StreamLogger streamLogger = new StreamLogger(new FileStream(logPath, FileMode.Create)))
                {
                    logRunner.Run(streamLogger);
                    streamLogger.Info("Done!");
                }
            });

            // Wait for game database to be initialized for the 2nd time and wait for any plugins to initialize
            yield return(null);

            yield return(null);

            IBasicLogger mmLogger = new QueueLogger(mmLogQueue);

            IEnumerable <ModListGenerator.ModAddedByAssembly> modsAddedByAssemblies = ModListGenerator.GetAdditionalModsFromStaticMethods(mmLogger);

            IEnumerable <IProtoUrlConfig> databaseConfigs = null;

            MMPatchLoader patchLoader = new MMPatchLoader(modsAddedByAssemblies, mmLogger);

            ITaskStatus patchingThreadStatus = BackgroundTask.Start(delegate
            {
                databaseConfigs = patchLoader.Run();
            });

            while (true)
            {
                yield return(null);

                if (!patchingThreadStatus.IsRunning)
                {
                    logRunner.RequestStop();
                }

                Status = patchLoader.status;
                Errors = patchLoader.errors;

                if (!patchingThreadStatus.IsRunning && !loggingThreadStatus.IsRunning)
                {
                    break;
                }
            }

            if (patchingThreadStatus.IsExitedWithError)
            {
                kspLogger.Exception("The patching thread threw an exception", patchingThreadStatus.Exception);
                FatalErrorHandler.HandleFatalError("The patching thread threw an exception");
            }

            if (loggingThreadStatus.IsExitedWithError)
            {
                kspLogger.Exception("The logging thread threw an exception", loggingThreadStatus.Exception);
                FatalErrorHandler.HandleFatalError("The logging thread threw an exception");
            }

            if (databaseConfigs == null)
            {
                kspLogger.Error("The patcher returned a null collection of configs");
                FatalErrorHandler.HandleFatalError("The patcher returned a null collection of configs");
                yield break;
            }

            PostPatchLoader.Instance.databaseConfigs = databaseConfigs;
        }