This commit is contained in:
@@ -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 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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user