A game framework written with osu! in mind.
at master 87 lines 2.9 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System; 5using System.Threading; 6using System.Threading.Tasks; 7using NUnit.Framework; 8using osu.Framework.Platform; 9using osu.Framework.Testing; 10using osu.Framework.Tests.IO; 11 12namespace osu.Framework.Tests.Platform 13{ 14 [TestFixture] 15 public class HeadlessGameHostTest 16 { 17 [Test] 18 public void TestGameHostExceptionDuringSetupHost() 19 { 20 using (var host = new ExceptionDuringSetupGameHost(nameof(TestGameHostExceptionDuringSetupHost))) 21 { 22 Assert.Throws<InvalidOperationException>(() => host.Run(new TestGame())); 23 } 24 } 25 26 [Test] 27 public void TestGameHostDisposalWhenNeverRun() 28 { 29 using (new TestRunHeadlessGameHost(nameof(TestGameHostDisposalWhenNeverRun), true)) 30 { 31 // never call host.Run() 32 } 33 } 34 35 [Test] 36 public void TestIpc() 37 { 38 using (var server = new BackgroundGameHeadlessGameHost(@"server", true)) 39 using (var client = new BackgroundGameHeadlessGameHost(@"client", true)) 40 { 41 Assert.IsTrue(server.IsPrimaryInstance, @"Server wasn't able to bind"); 42 Assert.IsFalse(client.IsPrimaryInstance, @"Client was able to bind when it shouldn't have been able to"); 43 44 var serverChannel = new IpcChannel<Foobar>(server); 45 var clientChannel = new IpcChannel<Foobar>(client); 46 47 void waitAction() 48 { 49 using (var received = new ManualResetEventSlim(false)) 50 { 51 serverChannel.MessageReceived += message => 52 { 53 Assert.AreEqual("example", message.Bar); 54 // ReSharper disable once AccessToDisposedClosure 55 received.Set(); 56 }; 57 58 clientChannel.SendMessageAsync(new Foobar { Bar = "example" }).Wait(); 59 60 received.Wait(); 61 } 62 } 63 64 Assert.IsTrue(Task.Run(waitAction).Wait(10000), @"Message was not received in a timely fashion"); 65 } 66 } 67 68 private class Foobar 69 { 70 public string Bar; 71 } 72 73 public class ExceptionDuringSetupGameHost : TestRunHeadlessGameHost 74 { 75 public ExceptionDuringSetupGameHost(string gameName) 76 : base(gameName) 77 { 78 } 79 80 protected override void SetupForRun() 81 { 82 base.SetupForRun(); 83 throw new InvalidOperationException(); 84 } 85 } 86 } 87}