Enable port release
SuccessfulCode/SuccessfulCodeTools/master This commit looks good

This commit is contained in:
2020-02-25 00:58:55 +00:00
parent 5274607ddd
commit 6f83e96055
2 changed files with 31 additions and 1 deletions
@@ -0,0 +1,27 @@
using SuccessfulCodeTools.LowLevelHttpClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace SuccessfulCodeTools.Tests
{
public class LowLevelHttpClientTests
{
[Fact]
public void CheckLowLevelHttpClientConcurrency()
{
//Arrange
Parallel.ForEach(Enumerable.Range(1, 20), (e) =>
{
var client = new CustomHttpClient(32455);
client.SendAsync(new CustomHttpRequestMessage(HttpMethod.Get, "http://www.mocky.io/v2/5e546e473100004900eb31a5")).GetAwaiter().GetResult();
});
//Act
//Assert
}
}
}
@@ -17,7 +17,7 @@ namespace SuccessfulCodeTools.LowLevelHttpClient
{ {
public int? OutgoingPort { get; set; } public int? OutgoingPort { get; set; }
public KeyValueCollection<string, string> DefaultHeaders = new KeyValueCollection<string, string>(); public KeyValueCollection<string, string> DefaultHeaders = new KeyValueCollection<string, string>();
private static ConcurrentDictionary<int, object> _dictionary = new ConcurrentDictionary<int, object>(); private static readonly ConcurrentDictionary<int, object> _dictionary = new ConcurrentDictionary<int, object>();
public CustomHttpClient(int? outgoingPort = null) public CustomHttpClient(int? outgoingPort = null)
{ {
OutgoingPort = outgoingPort; OutgoingPort = outgoingPort;
@@ -40,6 +40,7 @@ namespace SuccessfulCodeTools.LowLevelHttpClient
tcp.Connect(remoteEndpoint); tcp.Connect(remoteEndpoint);
tcp.LingerState.Enabled = false; tcp.LingerState.Enabled = false;
tcp.LingerState.LingerTime = 0; tcp.LingerState.LingerTime = 0;
tcp.SendTimeout = 500; tcp.SendTimeout = 500;
tcp.ReceiveTimeout = 1000; tcp.ReceiveTimeout = 1000;
// Send request headers // Send request headers
@@ -57,6 +58,8 @@ namespace SuccessfulCodeTools.LowLevelHttpClient
{ {
await stream.CopyToAsync(memory); await stream.CopyToAsync(memory);
tcp.Client.Disconnect(true); tcp.Client.Disconnect(true);
stream.Close();
tcp.Client.Close();
semphoreLock.Release(); semphoreLock.Release();
return new CustomHttpResponseMessage(memory); return new CustomHttpResponseMessage(memory);
} }