One request per port
SuccessfulCode/SuccessfulCodeTools/master There was a failure building this commit

This commit is contained in:
2020-02-23 16:26:51 +00:00
parent cf1d8ce609
commit b3723e455f
6 changed files with 73 additions and 3 deletions
@@ -0,0 +1,19 @@
using System;
using Xunit;
namespace SuccessfulCodeTools.Tests
{
public class StringExtensionsTests
{
[Theory]
[InlineData("CaMel_cAse", "CamelCase")]
public void CheckSnakeCaseToCamelCase(string input, string expectedOutput)
{
//Arrange
//Act
var result = input.FromSnakeToCamel();
//Assert
Assert.Equal(expectedOutput, result);
}
}
}
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SuccessfulCodeTools\SuccessfulCodeTools.csproj" />
</ItemGroup>
</Project>
+7 -1
View File
@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16 # Visual Studio Version 16
VisualStudioVersion = 16.0.29424.173 VisualStudioVersion = 16.0.29424.173
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SuccessfulCodeTools", "SuccessfulCodeTools\SuccessfulCodeTools.csproj", "{0CF60384-D7C3-491D-B3FD-73BDA7EE8B89}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SuccessfulCodeTools", "SuccessfulCodeTools\SuccessfulCodeTools.csproj", "{0CF60384-D7C3-491D-B3FD-73BDA7EE8B89}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SuccessfulCodeTools.Tests", "SuccessfulCodeTools.Tests\SuccessfulCodeTools.Tests.csproj", "{43979EEF-19B8-46B1-A32B-CC99879F5168}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +17,10 @@ Global
{0CF60384-D7C3-491D-B3FD-73BDA7EE8B89}.Debug|Any CPU.Build.0 = Debug|Any CPU {0CF60384-D7C3-491D-B3FD-73BDA7EE8B89}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0CF60384-D7C3-491D-B3FD-73BDA7EE8B89}.Release|Any CPU.ActiveCfg = Release|Any CPU {0CF60384-D7C3-491D-B3FD-73BDA7EE8B89}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0CF60384-D7C3-491D-B3FD-73BDA7EE8B89}.Release|Any CPU.Build.0 = Release|Any CPU {0CF60384-D7C3-491D-B3FD-73BDA7EE8B89}.Release|Any CPU.Build.0 = Release|Any CPU
{43979EEF-19B8-46B1-A32B-CC99879F5168}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{43979EEF-19B8-46B1-A32B-CC99879F5168}.Debug|Any CPU.Build.0 = Debug|Any CPU
{43979EEF-19B8-46B1-A32B-CC99879F5168}.Release|Any CPU.ActiveCfg = Release|Any CPU
{43979EEF-19B8-46B1-A32B-CC99879F5168}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@@ -0,0 +1,10 @@
namespace SuccessfulCodeTools.Extensions
{
public static class DecimalExtensions
{
public static decimal PercentOf(this decimal value, decimal total)
{
return (value / 100) * total;
}
}
}
@@ -1,5 +1,6 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.Text.RegularExpressions;
namespace SuccessfulCodeTools namespace SuccessfulCodeTools
{ {
@@ -19,5 +20,13 @@ namespace SuccessfulCodeTools
{ {
return Int32.Parse(value); return Int32.Parse(value);
} }
public static string FromSnakeToCamel(this string str)
{
var lowerCasedValue = str.ToLowerInvariant();
return Regex.Replace(lowerCasedValue, "(^[a-zA-Z]|_([a-zA-Z]))", m => {
return m.Value.TrimStart('_').ToUpper();
}, RegexOptions.Compiled);
}
} }
} }
@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
@@ -7,6 +8,7 @@ using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace SuccessfulCodeTools.LowLevelHttpClient namespace SuccessfulCodeTools.LowLevelHttpClient
@@ -15,6 +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>();
public CustomHttpClient(int? outgoingPort = null) public CustomHttpClient(int? outgoingPort = null)
{ {
OutgoingPort = outgoingPort; OutgoingPort = outgoingPort;
@@ -22,11 +25,13 @@ namespace SuccessfulCodeTools.LowLevelHttpClient
public async Task<CustomHttpResponseMessage> SendAsync(CustomHttpRequestMessage message, int? outgoingPort = null) public async Task<CustomHttpResponseMessage> SendAsync(CustomHttpRequestMessage message, int? outgoingPort = null)
{ {
var defaultHeaders = DefaultHeaders.Where(e => !message.Headers.Contains(e)).ToArray(); var defaultHeaders = DefaultHeaders.Where(e => !message.Headers.Contains(e)).ToArray();
message.Headers.AddRange(defaultHeaders); message.Headers.AddRange(defaultHeaders);
var outBoundPort = outgoingPort ?? OutgoingPort ?? 0; var outBoundPort = outgoingPort ?? OutgoingPort ?? 0;
string result = string.Empty; var semphoreLock = (SemaphoreSlim)_dictionary.GetOrAdd(outBoundPort, e => new SemaphoreSlim(1));
semphoreLock.Wait();
var ipLocalEndPoint = new IPEndPoint(IPAddress.Any, outBoundPort); var ipLocalEndPoint = new IPEndPoint(IPAddress.Any, outBoundPort);
using (var tcp = new TcpClient(ipLocalEndPoint)) using (var tcp = new TcpClient(ipLocalEndPoint))
{ {
@@ -52,10 +57,11 @@ namespace SuccessfulCodeTools.LowLevelHttpClient
{ {
await stream.CopyToAsync(memory); await stream.CopyToAsync(memory);
tcp.Client.Disconnect(true); tcp.Client.Disconnect(true);
semphoreLock.Release();
return new CustomHttpResponseMessage(memory); return new CustomHttpResponseMessage(memory);
} }
} }
}
} }
} }
}