diff --git a/SuccessfulCodeTools.Tests/LowLevelHttpClientTests.cs b/SuccessfulCodeTools.Tests/LowLevelHttpClientTests.cs new file mode 100644 index 0000000..af97a35 --- /dev/null +++ b/SuccessfulCodeTools.Tests/LowLevelHttpClientTests.cs @@ -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 + } + } +} diff --git a/SuccessfulCodeTools/LowLevelHttpClient/CustomHttpClient.cs b/SuccessfulCodeTools/LowLevelHttpClient/CustomHttpClient.cs index 11de1de..7bc6152 100644 --- a/SuccessfulCodeTools/LowLevelHttpClient/CustomHttpClient.cs +++ b/SuccessfulCodeTools/LowLevelHttpClient/CustomHttpClient.cs @@ -17,7 +17,7 @@ namespace SuccessfulCodeTools.LowLevelHttpClient { public int? OutgoingPort { get; set; } public KeyValueCollection DefaultHeaders = new KeyValueCollection(); - private static ConcurrentDictionary _dictionary = new ConcurrentDictionary(); + private static readonly ConcurrentDictionary _dictionary = new ConcurrentDictionary(); public CustomHttpClient(int? outgoingPort = null) { OutgoingPort = outgoingPort; @@ -40,6 +40,7 @@ namespace SuccessfulCodeTools.LowLevelHttpClient tcp.Connect(remoteEndpoint); tcp.LingerState.Enabled = false; tcp.LingerState.LingerTime = 0; + tcp.SendTimeout = 500; tcp.ReceiveTimeout = 1000; // Send request headers @@ -57,6 +58,8 @@ namespace SuccessfulCodeTools.LowLevelHttpClient { await stream.CopyToAsync(memory); tcp.Client.Disconnect(true); + stream.Close(); + tcp.Client.Close(); semphoreLock.Release(); return new CustomHttpResponseMessage(memory); }