From b10b98cb2dca967254e77d466a19f178d9bf11ee Mon Sep 17 00:00:00 2001 From: Oaz Date: Sat, 9 Sep 2017 18:11:43 +0200 Subject: [PATCH] added some unit test examples --- Iris.NET/Iris.NET.Tests/Iris.NET.Tests.csproj | 58 ++++++++++++ Iris.NET/Iris.NET.Tests/ServerMonitoring.cs | 93 +++++++++++++++++++ .../SimplePublicationSubscription.cs | 85 +++++++++++++++++ Iris.NET/Iris.NET.Tests/packages.config | 4 + 4 files changed, 240 insertions(+) create mode 100644 Iris.NET/Iris.NET.Tests/Iris.NET.Tests.csproj create mode 100644 Iris.NET/Iris.NET.Tests/ServerMonitoring.cs create mode 100644 Iris.NET/Iris.NET.Tests/SimplePublicationSubscription.cs create mode 100644 Iris.NET/Iris.NET.Tests/packages.config diff --git a/Iris.NET/Iris.NET.Tests/Iris.NET.Tests.csproj b/Iris.NET/Iris.NET.Tests/Iris.NET.Tests.csproj new file mode 100644 index 0000000..d05040f --- /dev/null +++ b/Iris.NET/Iris.NET.Tests/Iris.NET.Tests.csproj @@ -0,0 +1,58 @@ + + + + Debug + AnyCPU + {BBBD11E0-9827-4C86-9DEA-1D75C53C1BE3} + Library + Iris.NET.Tests + Iris.NET.Tests + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + false + + + full + true + bin\Release + prompt + 4 + false + + + + + ..\packages\NUnit.2.6.4\lib\nunit.framework.dll + + + + + + + + + + + + + {856716E0-8895-410B-AEEC-88A27BDECD75} + Iris.NET.Server + + + {F9C1A457-5A5F-4EEA-8076-AF6038F4C646} + Iris.NET.Client + + + {0692286E-6499-4F89-8971-C6F0252A54A1} + Iris.NET.Common + + + \ No newline at end of file diff --git a/Iris.NET/Iris.NET.Tests/ServerMonitoring.cs b/Iris.NET/Iris.NET.Tests/ServerMonitoring.cs new file mode 100644 index 0000000..38114e9 --- /dev/null +++ b/Iris.NET/Iris.NET.Tests/ServerMonitoring.cs @@ -0,0 +1,93 @@ +using System; +using NUnit.Framework; +using Iris.NET.Server; +using System.Threading; +using System.Net; +using System.Reflection; + +namespace Iris.NET.Tests +{ + [TestFixture] + public class ServerMonitoring + { + IrisServer _theServer; + + [SetUp] + public void Initialize() + { + _theServer = new IrisServer(); + } + + [TearDown] + public void Cleanup() + { + if(_theServer.IsRunning) + _theServer.Stop(); + } + + [Test] + public void ServerProperties() + { + Assert.AreEqual(false, _theServer.IsRunning); + Assert.AreEqual(null, _theServer.Port); + Assert.AreEqual(null, _theServer.Address); + _theServer.Start(56666); + Assert.AreEqual(true, _theServer.IsRunning); + Assert.AreEqual(56666, _theServer.Port); + Assert.AreEqual(new IPAddress(0), _theServer.Address); + _theServer.Stop(); + Assert.AreEqual(false, _theServer.IsRunning); + Assert.AreEqual(null, _theServer.Port); + Assert.AreEqual(null, _theServer.Address); + } + + [Test] + public void ServerIdAreUnique() + { + var otherServer = new IrisServer(); + Assert.AreNotEqual(otherServer.Id, _theServer.Id); + } + + [Test] + public void ServerStartCanBeMonitored() + { + var monitorStart = new AutoResetEvent(false); + _theServer.OnStarted += () => monitorStart.Set(); + _theServer.Start(56666); + Assert.IsTrue(monitorStart.WaitOne(1000), "Server was not started"); + } + + [Test] + public void ServerStopCanBeMonitored() + { + var monitorStop = new AutoResetEvent(false); + _theServer.OnStopped += () => monitorStop.Set(); + _theServer.Start(56666); + _theServer.Stop(); + Assert.IsTrue(monitorStop.WaitOne(1000), "Server was not stopped"); + } + + [Test] + public void ServerFailureCanBeMonitored() + { + var monitorFailure = new AutoResetEvent(false); + _theServer.OnServerException += ex => + { + Assert.IsNotNull(ex.Message); + monitorFailure.Set(); + }; + _theServer.Start(56666); + SimulateFailureByClosingTheServerTcpListener(); + Assert.IsTrue(monitorFailure.WaitOne(2000), "Server failure was not detected"); + } + + private void SimulateFailureByClosingTheServerTcpListener() + { + var listener = (System.Net.Sockets.TcpListener) typeof(IrisServer).GetField( + "_serverSocket", + BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField + ).GetValue(_theServer); + listener.Stop(); + } + } +} diff --git a/Iris.NET/Iris.NET.Tests/SimplePublicationSubscription.cs b/Iris.NET/Iris.NET.Tests/SimplePublicationSubscription.cs new file mode 100644 index 0000000..4973bf4 --- /dev/null +++ b/Iris.NET/Iris.NET.Tests/SimplePublicationSubscription.cs @@ -0,0 +1,85 @@ +using System; +using NUnit.Framework; +using Iris.NET.Server; +using System.Threading; +using System.Threading.Tasks; +using Iris.NET.Client; + +namespace Iris.NET.Tests +{ + [TestFixture] + public class SimplePublicationSubscription + { + [Test] + public async Task BetweenLocalNodes() + { + await PubSub( + _theServer.GetServerConfig(), + _theServer.GetServerConfig()); + } + + [Test] + public async Task BetweenClientNodes() + { + await PubSub( + _theClientConfig, + _theClientConfig); + } + [Test] + public async Task FromClientToLocalNode() + { + await PubSub( + _theClientConfig, + _theServer.GetServerConfig()); + } + + [Test] + public async Task FromLocalToClientNode() + { + await PubSub( + _theServer.GetServerConfig(), + _theClientConfig); + } + + public async Task PubSub(PUBCONF pubconf, SUBCONF subconf) + where SUBNODE : AbstractIrisNode, new() + where SUBCONF : IrisBaseConfig + where PUBNODE : AbstractIrisNode, new() + where PUBCONF : IrisBaseConfig + { + var received = new AutoResetEvent(false); + var subnode = new SUBNODE(); + subnode.Connect(subconf); + using (var subscription = await subnode.Subscribe("TheFooChannel", (content, hook) => + { + Assert.AreEqual("Foo ==> Bar", content); + received.Set(); + })) + { + Assert.IsNotNull(subscription); + var pubnode = new PUBNODE(); + pubnode.Connect(pubconf); + var publicationIsOk = await pubnode.Publish("TheFooChannel", "Foo ==> Bar"); + Assert.IsTrue(publicationIsOk); + Assert.IsTrue(received.WaitOne(5000), "Content was not received on subscription"); + } + } + + IrisServer _theServer; + IrisClientConfig _theClientConfig; + + [SetUp] + public void Initialize() + { + _theServer = new IrisServer(); + _theServer.Start(56666); + _theClientConfig = new IrisClientConfig() { Hostname = "127.0.0.1", Port = 56666 }; + } + + [TearDown] + public void Cleanup() + { + _theServer.Stop(); + } + } +} diff --git a/Iris.NET/Iris.NET.Tests/packages.config b/Iris.NET/Iris.NET.Tests/packages.config new file mode 100644 index 0000000..c714ef3 --- /dev/null +++ b/Iris.NET/Iris.NET.Tests/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file