Skip to content

added some unit test examples #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Iris.NET/Iris.NET.Tests/Iris.NET.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{BBBD11E0-9827-4C86-9DEA-1D75C53C1BE3}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>Iris.NET.Tests</RootNamespace>
<AssemblyName>Iris.NET.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ServerMonitoring.cs" />
<Compile Include="SimplePublicationSubscription.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Iris.NET.Server\Iris.NET.Server.csproj">
<Project>{856716E0-8895-410B-AEEC-88A27BDECD75}</Project>
<Name>Iris.NET.Server</Name>
</ProjectReference>
<ProjectReference Include="..\Iris.NET.Client\Iris.NET.Client.csproj">
<Project>{F9C1A457-5A5F-4EEA-8076-AF6038F4C646}</Project>
<Name>Iris.NET.Client</Name>
</ProjectReference>
<ProjectReference Include="..\Iris.NET.Common\Iris.NET.Common.csproj">
<Project>{0692286E-6499-4F89-8971-C6F0252A54A1}</Project>
<Name>Iris.NET.Common</Name>
</ProjectReference>
</ItemGroup>
</Project>
93 changes: 93 additions & 0 deletions Iris.NET/Iris.NET.Tests/ServerMonitoring.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
85 changes: 85 additions & 0 deletions Iris.NET/Iris.NET.Tests/SimplePublicationSubscription.cs
Original file line number Diff line number Diff line change
@@ -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<IrisLocalNode, IrisServerConfig, IrisLocalNode, IrisServerConfig>(
_theServer.GetServerConfig(),
_theServer.GetServerConfig());
}

[Test]
public async Task BetweenClientNodes()
{
await PubSub<IrisClientNode, IrisClientConfig, IrisClientNode, IrisClientConfig>(
_theClientConfig,
_theClientConfig);
}
[Test]
public async Task FromClientToLocalNode()
{
await PubSub<IrisClientNode, IrisClientConfig, IrisLocalNode, IrisServerConfig>(
_theClientConfig,
_theServer.GetServerConfig());
}

[Test]
public async Task FromLocalToClientNode()
{
await PubSub<IrisLocalNode, IrisServerConfig, IrisClientNode, IrisClientConfig>(
_theServer.GetServerConfig(),
_theClientConfig);
}

public async Task PubSub<PUBNODE, PUBCONF,SUBNODE, SUBCONF>(PUBCONF pubconf, SUBCONF subconf)
where SUBNODE : AbstractIrisNode<SUBCONF>, new()
where SUBCONF : IrisBaseConfig
where PUBNODE : AbstractIrisNode<PUBCONF>, 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();
}
}
}
4 changes: 4 additions & 0 deletions Iris.NET/Iris.NET.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="2.6.4" targetFramework="net45" />
</packages>