Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
Migrated RPC project into MessageQueue solution.
Browse files Browse the repository at this point in the history
Created initial tests.
  • Loading branch information
DJGosnell committed Sep 1, 2016
1 parent c8fc219 commit a103457
Show file tree
Hide file tree
Showing 23 changed files with 1,115 additions and 0 deletions.
126 changes: 126 additions & 0 deletions DtronixMessageQueue.Rpc.Tests/DtronixMessageQueue.Rpc.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\xunit.runner.visualstudio.2.2.0-beta2-build1149\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\packages\xunit.runner.visualstudio.2.2.0-beta2-build1149\build\net20\xunit.runner.visualstudio.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{926DD542-217A-4D5B-AA81-54D22B6B295C}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DtronixMessageQueue.Rpc.Tests</RootNamespace>
<AssemblyName>DtronixMessageQueue.Rpc.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.assert, Version=2.2.0.3300, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.assert.2.2.0-beta2-build3300\lib\netstandard1.0\xunit.assert.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.core, Version=2.2.0.3300, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.extensibility.core.2.2.0-beta2-build3300\lib\net45\xunit.core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.execution.desktop, Version=2.2.0.3300, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.extensibility.execution.2.2.0-beta2-build3300\lib\net45\xunit.execution.desktop.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
</ItemGroup>
</When>
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="RpcTestsBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RpcClientTests.cs" />
<Compile Include="Services\Server\CalculatorService.cs" />
<Compile Include="Services\Server\ICalculatorService.cs" />
<Compile Include="SimpleRpcSession.cs" />
<Compile Include="Utilities.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DtronixMessageQueue.Rpc\DtronixMessageQueue.Rpc.csproj">
<Project>{f034c114-e1a9-42fc-a2b0-0864c821179b}</Project>
<Name>DtronixMessageQueue.Rpc</Name>
</ProjectReference>
<ProjectReference Include="..\DtronixMessageQueue\DtronixMessageQueue.csproj">
<Project>{060bfeca-a3d2-4871-ae35-28deccb95fe3}</Project>
<Name>DtronixMessageQueue</Name>
</ProjectReference>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
</ItemGroup>
</When>
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\xunit.runner.visualstudio.2.2.0-beta2-build1149\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.runner.visualstudio.2.2.0-beta2-build1149\build\net20\xunit.runner.visualstudio.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
36 changes: 36 additions & 0 deletions DtronixMessageQueue.Rpc.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("DtronixMessageQueue.Rpc.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DtronixMessageQueue.Rpc.Tests")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("926dd542-217a-4d5b-aa81-54d22b6b295c")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
86 changes: 86 additions & 0 deletions DtronixMessageQueue.Rpc.Tests/RpcClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Diagnostics;
using System.Threading;
using DtronixMessageQueue.Rpc.Tests.Services.Server;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using Xunit;
using Xunit.Abstractions;

namespace DtronixMessageQueue.Rpc.Tests {
public class RpcClientTests : RpcTestsBase {


public RpcClientTests(ITestOutputHelper output) : base(output) {

}
public class Test {
public string TestStr { get; set; }
public int Length { get; set; }

}

[Fact]
public void Client_calls_proxy_method() {

Server.Connected += (sender, args) => {
args.Session.AddService(new CalculatorService());
};


Client.Connected += (sender, args) => {
args.Session.AddProxy<ICalculatorService>(new CalculatorService());
var service = Client.Session.GetProxy<ICalculatorService>();
var result = service.Add(100, 200);

TestStatus.Set();
};

StartAndWait();
}

[Fact]
public void Client_calls_proxy_method_sequential() {

Server.Connected += (sender, args) => {
args.Session.AddService<ICalculatorService>(new CalculatorService());
};


Client.Connected += (sender, args) => {
args.Session.AddProxy<ICalculatorService>(new CalculatorService());
var service = Client.Session.GetProxy<ICalculatorService>();
Stopwatch stopwatch = Stopwatch.StartNew();

int added_int = 0;
for (int i = 0; i < 2; i++) {
added_int = service.Add(added_int, 1);
}

Output.WriteLine($"{stopwatch.ElapsedMilliseconds}");
TestStatus.Set();
};

StartAndWait();
}



[Fact]
public void serializes_data() {
var writer = new MqMessageWriter(Config);
var bwriter = new BsonWriter(writer) {CloseOutput = false};

var s = new Test {
Length = 51235115,
TestStr = "ASFSFASFsfaasf aslgheqw8tyh 23 hy wasgh asdgio a"
};

JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(bwriter, s);


var message2 = writer.ToMessage();
}
}
}
124 changes: 124 additions & 0 deletions DtronixMessageQueue.Rpc.Tests/RpcTestsBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Xunit;
using Xunit.Abstractions;

namespace DtronixMessageQueue.Rpc.Tests {
public class RpcTestsBase : IDisposable {
private Random random = new Random();
public ITestOutputHelper Output;

public RpcServer<SimpleRpcSession> Server { get; protected set; }
public RpcClient<SimpleRpcSession> Client { get; protected set; }
public int Port { get; }

protected MqSocketConfig Config;

public Exception LastException { get; set; }

public TimeSpan TestTimeout { get; set; } = new TimeSpan(0, 0, 0, 0, 60000);

public ManualResetEventSlim TestStatus { get; set; } = new ManualResetEventSlim(false);

public RpcTestsBase(ITestOutputHelper output) {
this.Output = output;
Port = FreeTcpPort();

Config = new MqSocketConfig {
Ip = "127.0.0.1",
Port = Port
};

Server = new RpcServer<SimpleRpcSession>(Config);
Client = new RpcClient<SimpleRpcSession>(Config);
}


public static int FreeTcpPort() {
TcpListener l = new TcpListener(IPAddress.Loopback, 0);
l.Start();
int port = ((IPEndPoint) l.LocalEndpoint).Port;
l.Stop();
return port;
}

public void StartAndWait(bool timeout_error = true, int timeout_length = -1) {
if (Server.IsRunning == false) {
Server.Start();
}
if (Client.IsRunning == false) {
Client.Connect();
}

timeout_length = timeout_length != -1 ? timeout_length : (int)TestTimeout.TotalMilliseconds;

TestStatus.Wait(new TimeSpan(0, 0, 0, 0, timeout_length));

if (timeout_error && TestStatus.IsSet == false) {
throw new TimeoutException("Test timed out.");
}

if (LastException != null) {
throw LastException;
}

try {
Server.Stop();
Client.Close();
} catch {
// ignored
}
}

public void CompareMessages(MqMessage expected, MqMessage actual) {
try {


// Total frame count comparison.
Assert.Equal(expected.Count, actual.Count);

for (int i = 0; i < expected.Count; i++) {
// Frame length comparison.
Assert.Equal(expected[i].DataLength, actual[i].DataLength);

Assert.Equal(expected[i].Buffer, actual[i].Buffer);
}
} catch (Exception e) {
LastException = e;
}
}

public MqMessage GenerateRandomMessage(int frames = -1, int frame_length = -1) {
var frame_count = frames == -1 ? random.Next(8, 16) : frames;
var message = new MqMessage();
for (int i = 0; i < frame_count; i++) {
MqFrame frame;

if (frame_length == -1) {
frame = new MqFrame(Rpc.Tests.Utilities.SequentialBytes(random.Next(50, 1024*16 - 3)),
(i + 1 < frame_count) ? MqFrameType.More : MqFrameType.Last, Config);
} else {
frame = new MqFrame(Rpc.Tests.Utilities.SequentialBytes(frame_length),
(i + 1 < frame_count) ? MqFrameType.More : MqFrameType.Last, Config);
}
message.Add(frame);
}

return message;

}


public void Dispose() {
try {
Server.Stop();
} catch { }
try {
Client.Close();
} catch { }

}
}
}
Loading

0 comments on commit a103457

Please sign in to comment.