Skip to content

Commit

Permalink
parallel tests create own client
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerje committed Dec 14, 2024
1 parent 766fd40 commit f2fef4f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 78 deletions.
37 changes: 22 additions & 15 deletions src/Tests/Unit/ServiceWireTests/NpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,13 @@ public void SimpleParallelTest()
var a = rnd.Next(0, 100);
var b = rnd.Next(0, 100);

var result = _clientProxy.Proxy.Min(a, b);

if (Math.Min(a, b) != result) state.Break();
Assert.Equal(Math.Min(a, b), result);
using (var clientProxy = new NpClient<INetTester>(CreateEndPoint()))
{
var result = clientProxy.Proxy.Min(a, b);
if (Math.Min(a, b) != result) state.Break();
Assert.Equal(Math.Min(a, b), result);
}
Task.Delay(100);
});
}

Expand Down Expand Up @@ -137,21 +140,25 @@ public void ResponseParallelTest()
const int count = 5;
const int start = 0;

var result = _clientProxy.Proxy.Range(start, count);
for (var i = start; i < count; i++)
using (var clientProxy = new NpClient<INetTester>(CreateEndPoint()))
{
int temp;
if (result.TryGetValue(i, out temp))
{
if (i != temp) state.Break();
Assert.Equal(i, temp);
}
else
var result = clientProxy.Proxy.Range(start, count);
for (var i = start; i < count; i++)
{
state.Break();
Assert.True(false);
int temp;
if (result.TryGetValue(i, out temp))
{
if (i != temp) state.Break();
Assert.Equal(i, temp);
}
else
{
state.Break();
Assert.True(false);
}
}
}
Task.Delay(100);
});
}

Expand Down
71 changes: 38 additions & 33 deletions src/Tests/Unit/ServiceWireTests/TcpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,65 +29,67 @@ public TcpTests()
_tcphost = new TcpHost(CreateEndPoint());
_tcphost.AddService<INetTester>(_tester);
_tcphost.Open();
Task.Delay(500);
Task.Delay(100);
_clientProxy = new TcpClient<INetTester>(CreateEndPoint());
}

[Fact]
public void SimpleTest()
{
Task.Delay(500);
Task.Delay(100);
var rnd = new Random();

var a = rnd.Next(0, 100);
var b = rnd.Next(0, 100);

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

[Fact]
public async Task CalculateAsyncTest()
{
await Task.Delay(500);
await Task.Delay(100);
var rnd = new Random();

var a = rnd.Next(0, 100);
var b = rnd.Next(0, 100);

var result = await _clientProxy.Proxy.CalculateAsync(a, b);
Assert.Equal(a + b, result);
await Task.Delay(500);
await Task.Delay(100);
}

[Fact]
public void SimpleParallelTest()
{
Task.Delay(500);
Task.Delay(100);
var rnd = new Random();

Parallel.For(0, 4, (index, state) =>
{
var a = rnd.Next(0, 100);
var b = rnd.Next(0, 100);

var result = _clientProxy.Proxy.Min(a, b);

if (Math.Min(a, b) != result)
using (var clientProxy = new TcpClient<INetTester>(CreateEndPoint()))
{
state.Break();
Assert.Equal(Math.Min(a, b), result);
var result = clientProxy.Proxy.Min(a, b);
if (Math.Min(a, b) != result)
{
state.Break();
Assert.Equal(Math.Min(a, b), result);
}
}
Task.Delay(500);
Task.Delay(100);
});
Task.Delay(500);
Task.Delay(100);
}

[Fact]
public void ResponseTest()
{
Task.Delay(500);
Task.Delay(100);
const int count = 50;
const int start = 0;

Expand All @@ -99,58 +101,61 @@ public void ResponseTest()
Assert.True(result.TryGetValue(i, out temp));
Assert.Equal(i, temp);
}
Task.Delay(500);
Task.Delay(100);
}

[Fact]
public void ResponseParallelTest()
{
Task.Delay(500);
Task.Delay(100);
Parallel.For(0, 4, (index, state) =>
{
const int count = 50;
const int start = 0;

var result = _clientProxy.Proxy.Range(start, count);
for (var i = start; i < count; i++)
using (var clientProxy = new TcpClient<INetTester>(CreateEndPoint()))
{
int temp;
if (result.TryGetValue(i, out temp))
var result = clientProxy.Proxy.Range(start, count);
for (var i = start; i < count; i++)
{
if (i != temp) state.Break();
Assert.Equal(i, temp);
}
else
{
state.Break();
Assert.True(false);
int temp;
if (result.TryGetValue(i, out temp))
{
if (i != temp) state.Break();
Assert.Equal(i, temp);
}
else
{
state.Break();
Assert.True(false);
}
}
}
Task.Delay(500);
Task.Delay(100);
});
Task.Delay(500);
Task.Delay(100);
}

[Fact]
public void ResponseWithOutParameterTest()
{
Task.Delay(500);
Task.Delay(100);
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);
Task.Delay(500);
Task.Delay(100);
}

[Fact]
public void GetStringsTest()
{
Task.Delay(500);
Task.Delay(100);
var result = _clientProxy.Proxy.GetStrings();
Assert.Equal(4, result.Length);
Assert.Null(result[2]);
Task.Delay(500);
Task.Delay(100);
}


Expand Down
65 changes: 35 additions & 30 deletions src/Tests/Unit/ServiceWireTests/TcpZkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,64 +52,66 @@ public TcpZkTests()
_tcphost = new TcpHost(CreateEndPoint(), zkRepository: _repo);
_tcphost.AddService<INetTester>(_tester);
_tcphost.Open();
Task.Delay(500);
Task.Delay(100);
_clientProxy = new TcpClient<INetTester>(CreateZkClientEndPoint());
}

[Fact]
public void SimpleZkTest()
{
Task.Delay(500);
Task.Delay(100);
var rnd = new Random();

var a = rnd.Next(0, 100);
var b = rnd.Next(0, 100);

var result = _clientProxy.Proxy.Min(a, b);
Assert.Equal<int>(Math.Min(a, b), result);
Task.Delay(500);
Task.Delay(100);
}

[Fact]
public async Task CalculateAsyncTest()
{
Task.Delay(500);
Task.Delay(100);

Check warning on line 76 in src/Tests/Unit/ServiceWireTests/TcpZkTests.cs

View workflow job for this annotation

GitHub Actions / build_windows

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 76 in src/Tests/Unit/ServiceWireTests/TcpZkTests.cs

View workflow job for this annotation

GitHub Actions / build_windows

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 76 in src/Tests/Unit/ServiceWireTests/TcpZkTests.cs

View workflow job for this annotation

GitHub Actions / build_linux

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 76 in src/Tests/Unit/ServiceWireTests/TcpZkTests.cs

View workflow job for this annotation

GitHub Actions / build_linux

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
var rnd = new Random();

var a = rnd.Next(0, 100);
var b = rnd.Next(0, 100);

var result = await _clientProxy.Proxy.CalculateAsync(a, b);
Assert.Equal(a + b, result);
Task.Delay(500);
Task.Delay(100);

Check warning on line 84 in src/Tests/Unit/ServiceWireTests/TcpZkTests.cs

View workflow job for this annotation

GitHub Actions / build_windows

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 84 in src/Tests/Unit/ServiceWireTests/TcpZkTests.cs

View workflow job for this annotation

GitHub Actions / build_windows

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 84 in src/Tests/Unit/ServiceWireTests/TcpZkTests.cs

View workflow job for this annotation

GitHub Actions / build_linux

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 84 in src/Tests/Unit/ServiceWireTests/TcpZkTests.cs

View workflow job for this annotation

GitHub Actions / build_linux

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
}

[Fact]
public void SimpleParallelZkTest()
{
Task.Delay(500);
Task.Delay(100);
var rnd = new Random();
Parallel.For(0, 12, (index, state) =>
{
var a = rnd.Next(0, 100);
var b = rnd.Next(0, 100);

var result = _clientProxy.Proxy.Min(a, b);

if (Math.Min(a, b) != result)
using (var clientProxy = new TcpClient<INetTester>(CreateZkClientEndPoint()))
{
state.Break();
Assert.Equal(Math.Min(a, b), result);
var result = _clientProxy.Proxy.Min(a, b);
if (Math.Min(a, b) != result)
{
state.Break();
Assert.Equal(Math.Min(a, b), result);
}
}
Task.Delay(500);
Task.Delay(100);
});
Task.Delay(500);
Task.Delay(100);
}

[Fact]
public void ResponseZkTest()
{
Task.Delay(500);
Task.Delay(100);
const int count = 50;
const int start = 0;

Expand All @@ -127,38 +129,41 @@ public void ResponseZkTest()
Assert.True(false);
}
}
Task.Delay(500);
Task.Delay(100);
}

[Fact]
public void ResponseParallelTest()
{
Task.Delay(500);
Task.Delay(100);
Random rnd = new Random(DateTime.Now.Millisecond);
Parallel.For(0, 12, (index, state) =>
{
const int count = 50;
const int start = 0;

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

for (var i = start; i < count; i++)
using (var clientProxy = new TcpClient<INetTester>(CreateZkClientEndPoint()))
{
int temp;
if (result.TryGetValue(i, out temp))
{
if(i != temp) state.Break();
Assert.Equal(i, temp);
}
else
var result = clientProxy.Proxy.Range(start, count);

for (var i = start; i < count; i++)
{
state.Break();
Assert.True(false);
int temp;
if (result.TryGetValue(i, out temp))
{
if (i != temp) state.Break();
Assert.Equal(i, temp);
}
else
{
state.Break();
Assert.True(false);
}
}
}
Task.Delay(500);
Task.Delay(100);
});
Task.Delay(500);
Task.Delay(100);
}

public void Dispose()
Expand Down

0 comments on commit f2fef4f

Please sign in to comment.