Skip to content

Commit

Permalink
Change Bytes read (#25)
Browse files Browse the repository at this point in the history
* Test to make sure socket has closed
  • Loading branch information
izavala authored and Aaronontheweb committed Mar 15, 2019
1 parent 8b9e7e1 commit e7130e0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using Akka.Actor;
// -----------------------------------------------------------------------
// <copyright file="ReadinessStatusClusterUpSpecs.cs" company="Petabridge, LLC">
// Copyright (C) 2015 - 2019 Petabridge, LLC <https://petabridge.com>
// </copyright>
// -----------------------------------------------------------------------

using Akka.Actor;
using Akka.HealthCheck.Readiness;
using Xunit;
using Xunit.Abstractions;
Expand Down
23 changes: 18 additions & 5 deletions src/Akka.HealthCheck.Tests/AkkaHealthCheckIntegrationSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Akka.HealthCheck.Readiness;
using Akka.HealthCheck.Transports.Files;
using Akka.HealthCheck.Transports.Sockets;
using Akka.Util;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -23,15 +24,21 @@ namespace Akka.HealthCheck.Tests
public class AkkaHealthCheckIntegrationSpec : TestKit.Xunit.TestKit
{
public AkkaHealthCheckIntegrationSpec(ITestOutputHelper helper)
: base(HealthcheckConfig, output: helper)
: base(GetConfig(), output: helper)
{
}

public static readonly Config HealthcheckConfig = @"


public static Config GetConfig()
{
var PortNumber = ThreadLocalRandom.Current.Next(10000, 64000);

Config HealthcheckConfig = @"
akka.healthcheck{
liveness{
transport = tcp
tcp.port = 15050
tcp.port = " + PortNumber + @"
}
readiness{
Expand All @@ -40,6 +47,8 @@ public AkkaHealthCheckIntegrationSpec(ITestOutputHelper helper)
}
}
";
return HealthcheckConfig;
}

[Fact(DisplayName = "End2End: should load complete custom Akka.HealthCheck config")]
public async Task Should_load_custom_HealthCheck_system_correctly()
Expand Down Expand Up @@ -69,17 +78,21 @@ public async Task Should_load_custom_HealthCheck_system_correctly()
// Readiness probe should not exist
AwaitCondition(() => !File.Exists(filePath));

//Created a new client to see if it would be able to connect.
var tcpClient2 = new TcpClient(AddressFamily.InterNetworkV6);

// liveness probe should be disconnected
try
{
await tcpClient2.ConnectAsync(IPAddress.IPv6Loopback, tcpPort);
var bytesRead = await tcpClient.GetStream().ReadAsync(new byte[10], 0, 10);
bytesRead.Should().Be(0);
}
catch
{
}

tcpClient.Connected.Should().BeFalse();
//Second client should not be able to connect as socket has been closed
AwaitCondition(()=> !tcpClient2.Connected);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
Expand Down Expand Up @@ -41,14 +42,20 @@ public async Task Should_successfully_open_and_close_signal()

try
{
var bytesRead = await tcpClient.GetStream().ReadAsync(new byte[10], 0, 10);
bytesRead.Should().Be(0);
AwaitAssert(() => tcpClient.GetStream().ReadAsync(new byte[10], 0, 10).Should().Be(8));
}
catch
{
}

tcpClient.Connected.Should().BeFalse();
var tcpClient2 = new TcpClient(AddressFamily.InterNetworkV6);
try
{
await tcpClient2.ConnectAsync(IPAddress.IPv6Loopback, PortNumber);
//Should throw execption as socket will refuse to establish a connection
}
catch{ }
tcpClient2.Connected.Should().BeFalse();
}

[Fact(DisplayName = "SocketTransport should idempotently close TCP signal")]
Expand All @@ -73,16 +80,16 @@ public async Task Should_successfully_repeatedly_open_signal()
var result2 = await Transport.Go("bar", CancellationToken.None);
result.Success.Should().BeTrue();

var bytesRead = tcpClient.Available;
bytesRead.Should().Be(0);

AwaitAssert(()=> tcpClient.Available.Should().Be(8));
tcpClient.Connected.Should().BeTrue();

// special case - need to test the NULL pattern
var result3 = await Transport.Go(null, CancellationToken.None);
result.Success.Should().BeTrue();

bytesRead = tcpClient.Available;
bytesRead.Should().Be(0);

AwaitAssert(() => tcpClient.Available.Should().Be(8));
tcpClient.Connected.Should().BeTrue();
}
}
Expand Down

0 comments on commit e7130e0

Please sign in to comment.