public void TestUnhandledExceptionLogging()
        {
            TestException resolvedException = null;

            void logTest(LogEntry entry)
            {
                if (entry.Exception is TestException ex)
                {
                    Assert.IsNull(resolvedException, "exception was forwarded more than once");
                    resolvedException = ex;
                }
            }

            Logger.NewEntry += logTest;

            try
            {
                using (var host = new TestRunHeadlessGameHost())
                {
                    var game = new TestGame();
                    game.Schedule(() => throw new TestException());
                    host.Run(game);
                }
            }
            catch
            {
                // catch crashing exception
            }

            Assert.IsNotNull(resolvedException, "exception wasn't forwarded by logger");
            Logger.NewEntry -= logTest;
        }
        public void TestThreadSafetyResetOnEnteringThread()
        {
            using (var host = new TestRunHeadlessGameHost(nameof(TestThreadSafetyResetOnEnteringThread), new HostOptions()))
            {
                bool isDrawThread   = false;
                bool isUpdateThread = false;
                bool isInputThread  = false;
                bool isAudioThread  = false;

                var task = Task.Factory.StartNew(() =>
                {
                    var game = new TestGame();
                    game.Scheduler.Add(() => host.Exit());

                    host.Run(game);

                    isDrawThread   = ThreadSafety.IsDrawThread;
                    isUpdateThread = ThreadSafety.IsUpdateThread;
                    isInputThread  = ThreadSafety.IsInputThread;
                    isAudioThread  = ThreadSafety.IsAudioThread;
                }, TaskCreationOptions.LongRunning);

                task.WaitSafely();

                Assert.That(!isDrawThread && !isUpdateThread && !isInputThread && !isAudioThread);
            }
        }
 public void TestGameUpdateExceptionNoLogging()
 {
     Assert.Throws <TestException>(() =>
     {
         using (var host = new TestRunHeadlessGameHost())
             host.Run(new CrashTestGame());
     });
 }
示例#4
0
 public void TestGameUnobservedExceptionDoesntCrashGame()
 {
     using (var host = new TestRunHeadlessGameHost())
     {
         TaskCrashTestGame game = new TaskCrashTestGame();
         host.Run(game);
     }
 }
示例#5
0
        public void TestNonPortableInstall()
        {
            Assert.IsFalse(startupStorage.Exists(FrameworkConfigManager.FILENAME));

            using (var nonPortable = new TestRunHeadlessGameHost(@"non-portable"))
            {
                nonPortable.Run(new TestGame());
                Assert.AreNotEqual(startupStorage.GetFullPath(FrameworkConfigManager.FILENAME), nonPortable.Storage.GetFullPath(FrameworkConfigManager.FILENAME));
            }

            Assert.IsFalse(startupStorage.Exists(FrameworkConfigManager.FILENAME));
        }
        private void runWithIgnoreCount(int ignoreCount, int fireCount)
        {
            using (var host = new TestRunHeadlessGameHost())
            {
                host.ExceptionThrown += ex => ignoreCount-- > 0;

                var game = new TestGame();

                for (int i = 0; i < fireCount; i++)
                {
                    game.Schedule(() => throw new TestException());
                }
                game.Schedule(() => game.Exit());

                host.Run(game);
            }
        }
        private void runGameWithLogic(Action <Game> logic, Func <Game, bool> exitCondition = null)
        {
            Storage storage = null;

            try
            {
                using (var host = new TestRunHeadlessGameHost($"{GetType().Name}-{Guid.NewGuid()}", new HostOptions()))
                {
                    using (var game = new TestGame())
                    {
                        game.Schedule(() =>
                        {
                            storage = host.Storage;
                            host.UpdateThread.Scheduler.AddDelayed(() =>
                            {
                                if (exitCondition?.Invoke(game) == true)
                                {
                                    host.Exit();
                                }
                            }, 0, true);

                            logic(game);
                        });

                        host.Run(game);
                    }
                }
            }
            finally
            {
                try
                {
                    storage?.DeleteDirectory(string.Empty);
                }
                catch
                {
                    // May fail due to the file handles still being open on Windows, but this isn't a big problem for us
                }
            }
        }