Skip to content

Commit

Permalink
use single proxy in tests to lower load in pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerje committed Dec 14, 2024
1 parent dcaa88d commit faa3042
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 188 deletions.
114 changes: 46 additions & 68 deletions src/Tests/Unit/ServiceWireTests/NpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class NpTests : IDisposable
private INetTester _tester;

private NpHost _nphost;
private NpClient<INetTester> _clientProxy;

private readonly string _pipeName = "ServiceWireTestHost";

Expand All @@ -30,6 +31,8 @@ public NpTests()
_nphost = new NpHost(_pipeName);
_nphost.AddService<INetTester>(_tester);
_nphost.Open();

_clientProxy = new NpClient<INetTester>(CreateEndPoint());
}

[Fact]
Expand All @@ -40,11 +43,8 @@ public void SimpleTest()
var a = rnd.Next(0, 100);
var b = rnd.Next(0, 100);

using (var clientProxy = new NpClient<INetTester>(CreateEndPoint()))
{
var result = clientProxy.Proxy.Min(a, b);
Assert.Equal(Math.Min(a, b), result);
}
var result = _clientProxy.Proxy.Min(a, b);
Assert.Equal(Math.Min(a, b), result);
}

[Fact]
Expand All @@ -60,11 +60,8 @@ public void SimpleNewtonsoftSerializerTest()
var a = rnd.Next(0, 100);
var b = rnd.Next(0, 100);

using (var clientProxy = new NpClient<INetTester>(new NpEndPoint(_pipeName + "Json"), new NewtonsoftSerializer()))
{
var result = clientProxy.Proxy.Min(a, b);
Assert.Equal(Math.Min(a, b), result);
}
var result = _clientProxy.Proxy.Min(a, b);
Assert.Equal(Math.Min(a, b), result);
}
}

Expand All @@ -81,11 +78,8 @@ public void SimpleProtobufSerializerTest()
var a = rnd.Next(0, 100);
var b = rnd.Next(0, 100);

using (var clientProxy = new NpClient<INetTester>(new NpEndPoint(_pipeName + "Proto"), new ProtobufSerializer()))
{
var result = clientProxy.Proxy.Min(a, b);
Assert.Equal(Math.Min(a, b), result);
}
var result = _clientProxy.Proxy.Min(a, b);
Assert.Equal(Math.Min(a, b), result);
}
}

Expand All @@ -97,11 +91,8 @@ public async Task CalculateAsyncTest()
var a = rnd.Next(0, 100);
var b = rnd.Next(0, 100);

using (var clientProxy = new NpClient<INetTester>(CreateEndPoint()))
{
var result = await clientProxy.Proxy.CalculateAsync(a, b);
Assert.Equal(a + b, result);
}
var result = await _clientProxy.Proxy.CalculateAsync(a, b);
Assert.Equal(a + b, result);
}

[Fact]
Expand All @@ -114,32 +105,26 @@ public void SimpleParallelTest()
var a = rnd.Next(0, 100);
var b = rnd.Next(0, 100);

using (var clientProxy = new NpClient<INetTester>(CreateEndPoint()))
{
var result = clientProxy.Proxy.Min(a, b);
var result = _clientProxy.Proxy.Min(a, b);

if (Math.Min(a, b) != result) state.Break();
Assert.Equal(Math.Min(a, b), result);
}
if (Math.Min(a, b) != result) state.Break();
Assert.Equal(Math.Min(a, b), result);
});
}

[Fact]
public void ResponseTest()
{
using (var clientProxy = new NpClient<INetTester>(CreateEndPoint()))
{
const int count = 50;
const int start = 0;
const int count = 50;
const int start = 0;

var result = clientProxy.Proxy.Range(start, count);
var result = _clientProxy.Proxy.Range(start, count);

for (var i = start; i < count; i++)
{
int temp;
Assert.True(result.TryGetValue(i, out temp));
Assert.Equal(i, temp);
}
for (var i = start; i < count; i++)
{
int temp;
Assert.True(result.TryGetValue(i, out temp));
Assert.Equal(i, temp);
}
}

Expand All @@ -149,25 +134,22 @@ public void ResponseParallelTest()
Parallel.For(0, 4, (index, state) =>
{
Thread.Sleep(100);
using (var clientProxy = new NpClient<INetTester>(CreateEndPoint()))
{
const int count = 5;
const int start = 0;
const int count = 5;
const int start = 0;

var result = clientProxy.Proxy.Range(start, count);
for (var i = start; i < count; i++)
var result = _clientProxy.Proxy.Range(start, count);
for (var i = start; i < count; i++)
{
int temp;
if (result.TryGetValue(i, out temp))
{
if (i != temp) state.Break();
Assert.Equal(i, temp);
}
else
{
int temp;
if (result.TryGetValue(i, out temp))
{
if (i != temp) state.Break();
Assert.Equal(i, temp);
}
else
{
state.Break();
Assert.True(false);
}
state.Break();
Assert.True(false);
}
}
});
Expand All @@ -176,14 +158,11 @@ public void ResponseParallelTest()
[Fact]
public void ResponseWithOutParameterTest()
{
using (var clientProxy = new NpClient<INetTester>(CreateEndPoint()))
{
int quantity = 0;
var result = clientProxy.Proxy.Get(Guid.NewGuid(), "SomeLabel", 45.65, out quantity);
Assert.Equal(44, quantity);
Assert.NotEqual(default(TestResponse), result);
Assert.Equal("MyLabel", result.Label);
}
int quantity = 0;
var result = _clientProxy.Proxy.Get(Guid.NewGuid(), "SomeLabel", 45.65, out quantity);
Assert.Equal(44, quantity);
Assert.NotEqual(default(TestResponse), result);
Assert.Equal("MyLabel", result.Label);
}

[Fact]
Expand All @@ -208,17 +187,16 @@ public void ResponseWithOutParameterNewtonsoftSerializerTest()
[Fact]
public void GetStringsTest()
{
using (var clientProxy = new NpClient<INetTester>(CreateEndPoint()))
{
var result = clientProxy.Proxy.GetStrings();
Assert.Equal(4, result.Length);
Assert.Null(result[2]);
}
var result = _clientProxy.Proxy.GetStrings();
Assert.Equal(4, result.Length);
Assert.Null(result[2]);
}

public void Dispose()
{
_clientProxy.Dispose();
_nphost.Close();
_nphost.Dispose();
}
}
}
106 changes: 45 additions & 61 deletions src/Tests/Unit/ServiceWireTests/TcpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class TcpTests : IDisposable
private TcpHost _tcphost;
private IPAddress _ipAddress;
private const int Port = 8099;
private TcpClient<INetTester> _clientProxy;

private IPEndPoint CreateEndPoint()
{
return new IPEndPoint(_ipAddress, Port);
Expand All @@ -27,6 +29,8 @@ public TcpTests()
_tcphost = new TcpHost(CreateEndPoint());
_tcphost.AddService<INetTester>(_tester);
_tcphost.Open();

_clientProxy = new TcpClient<INetTester>(CreateEndPoint());
}

[Fact]
Expand All @@ -37,12 +41,8 @@ public void SimpleTest()
var a = rnd.Next(0, 100);
var b = rnd.Next(0, 100);

using (var clientProxy = new TcpClient<INetTester>(CreateEndPoint()))
{
var result = clientProxy.Proxy.Min(a, b);

Assert.Equal(Math.Min(a, b), result);
}
var result = _clientProxy.Proxy.Min(a, b);
Assert.Equal(Math.Min(a, b), result);
}

[Fact]
Expand All @@ -53,11 +53,8 @@ public async Task CalculateAsyncTest()
var a = rnd.Next(0, 100);
var b = rnd.Next(0, 100);

using (var clientProxy = new TcpClient<INetTester>(CreateEndPoint()))
{
var result = await clientProxy.Proxy.CalculateAsync(a, b);
Assert.Equal(a + b, result);
}
var result = await _clientProxy.Proxy.CalculateAsync(a, b);
Assert.Equal(a + b, result);
}

[Fact]
Expand All @@ -70,35 +67,29 @@ public void SimpleParallelTest()
var a = rnd.Next(0, 100);
var b = rnd.Next(0, 100);

using (var clientProxy = new TcpClient<INetTester>(CreateEndPoint()))
{
var result = clientProxy.Proxy.Min(a, b);
var result = _clientProxy.Proxy.Min(a, b);

if (Math.Min(a, b) != result)
{
state.Break();
Assert.Equal(Math.Min(a, b), result);
}
if (Math.Min(a, b) != result)
{
state.Break();
Assert.Equal(Math.Min(a, b), result);
}
});
}

[Fact]
public void ResponseTest()
{
using (var clientProxy = new TcpClient<INetTester>(CreateEndPoint()))
{
const int count = 50;
const int start = 0;
const int count = 50;
const int start = 0;

var result = clientProxy.Proxy.Range(start, count);
var result = _clientProxy.Proxy.Range(start, count);

for (var i = start; i < count; i++)
{
int temp;
Assert.True(result.TryGetValue(i, out temp));
Assert.Equal(i, temp);
}
for (var i = start; i < count; i++)
{
int temp;
Assert.True(result.TryGetValue(i, out temp));
Assert.Equal(i, temp);
}
}

Expand All @@ -107,25 +98,22 @@ public void ResponseParallelTest()
{
Parallel.For(0, 4, (index, state) =>
{
using (var clientProxy = new TcpClient<INetTester>(CreateEndPoint()))
{
const int count = 50;
const int start = 0;
const int count = 50;
const int start = 0;

var result = clientProxy.Proxy.Range(start, count);
for (var i = start; i < count; i++)
var result = _clientProxy.Proxy.Range(start, count);
for (var i = start; i < count; i++)
{
int temp;
if (result.TryGetValue(i, out temp))
{
int temp;
if (result.TryGetValue(i, out temp))
{
if (i != temp) state.Break();
Assert.Equal(i, temp);
}
else
{
state.Break();
Assert.True(false);
}
if (i != temp) state.Break();
Assert.Equal(i, temp);
}
else
{
state.Break();
Assert.True(false);
}
}
});
Expand All @@ -134,31 +122,27 @@ public void ResponseParallelTest()
[Fact]
public void ResponseWithOutParameterTest()
{
using (var clientProxy = new TcpClient<INetTester>(CreateEndPoint()))
{
int quantity = 0;
var result = clientProxy.Proxy.Get(Guid.NewGuid(), "SomeLabel", 45.65, out quantity);
Assert.Equal(44, quantity);
Assert.NotEqual(default(TestResponse), result);
Assert.Equal("MyLabel", result.Label);
}
int quantity = 0;
var result = _clientProxy.Proxy.Get(Guid.NewGuid(), "SomeLabel", 45.65, out quantity);
Assert.Equal(44, quantity);
Assert.NotEqual(default(TestResponse), result);
Assert.Equal("MyLabel", result.Label);
}

[Fact]
public void GetStringsTest()
{
using (var clientProxy = new TcpClient<INetTester>(CreateEndPoint()))
{
var result = clientProxy.Proxy.GetStrings();
Assert.Equal(4, result.Length);
Assert.Null(result[2]);
}
var result = _clientProxy.Proxy.GetStrings();
Assert.Equal(4, result.Length);
Assert.Null(result[2]);
}


public void Dispose()
{
_clientProxy.Dispose();
_tcphost.Close();
_tcphost.Dispose();
}
}
}
Loading

0 comments on commit faa3042

Please sign in to comment.