Fix ConcurrentDictionary (#129) (#144)

This commit is contained in:
Stef Heyenrath
2018-05-28 08:08:18 +02:00
committed by GitHub
parent dc39f91205
commit 0640c88bcd
6 changed files with 19 additions and 27 deletions

View File

@@ -9,6 +9,7 @@ namespace WireMock.Http
/// </summary> /// </summary>
public static class PortUtil public static class PortUtil
{ {
private static readonly IPEndPoint DefaultLoopbackEndpoint = new IPEndPoint(IPAddress.Loopback, port: 0);
private static readonly Regex UrlDetailsRegex = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>\d+)?/", RegexOptions.Compiled); private static readonly Regex UrlDetailsRegex = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>\d+)?/", RegexOptions.Compiled);
/// <summary> /// <summary>
@@ -17,17 +18,10 @@ namespace WireMock.Http
/// <remarks>see http://stackoverflow.com/questions/138043/find-the-next-tcp-port-in-net.</remarks> /// <remarks>see http://stackoverflow.com/questions/138043/find-the-next-tcp-port-in-net.</remarks>
public static int FindFreeTcpPort() public static int FindFreeTcpPort()
{ {
TcpListener tcpListener = null; using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
try
{ {
tcpListener = new TcpListener(IPAddress.Loopback, 0); socket.Bind(DefaultLoopbackEndpoint);
tcpListener.Start(); return ((IPEndPoint)socket.LocalEndPoint).Port;
return ((IPEndPoint)tcpListener.LocalEndpoint).Port;
}
finally
{
tcpListener?.Stop();
} }
} }

View File

@@ -58,7 +58,7 @@ namespace WireMock.Owin
// Set start // Set start
if (!_options.Scenarios.ContainsKey(mapping.Scenario) && mapping.IsStartState) if (!_options.Scenarios.ContainsKey(mapping.Scenario) && mapping.IsStartState)
{ {
_options.Scenarios.Add(mapping.Scenario, null); _options.Scenarios.TryAdd(mapping.Scenario, null);
} }
} }

View File

@@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using WireMock.Logging; using WireMock.Logging;
using WireMock.Matchers; using WireMock.Matchers;
@@ -23,7 +22,9 @@ namespace WireMock.Owin
public bool AllowPartialMapping { get; set; } public bool AllowPartialMapping { get; set; }
public IDictionary<Guid, Mapping> Mappings { get; } = new ConcurrentDictionary<Guid, Mapping>(); public ConcurrentDictionary<Guid, Mapping> Mappings { get; } = new ConcurrentDictionary<Guid, Mapping>(); // Checked
public ConcurrentDictionary<string, object> Scenarios { get; } = new ConcurrentDictionary<string, object>(); // Checked
public ObservableCollection<LogEntry> LogEntries { get; } = new ConcurentObservableCollection<LogEntry>(); public ObservableCollection<LogEntry> LogEntries { get; } = new ConcurentObservableCollection<LogEntry>();
@@ -31,8 +32,6 @@ namespace WireMock.Owin
public int? MaxRequestLogCount { get; set; } public int? MaxRequestLogCount { get; set; }
public IDictionary<string, object> Scenarios { get; } = new ConcurrentDictionary<string, object>();
#if !NETSTANDARD #if !NETSTANDARD
public Action<IAppBuilder> PreWireMockMiddlewareInit { get; set; } public Action<IAppBuilder> PreWireMockMiddlewareInit { get; set; }

View File

@@ -1,5 +1,4 @@
using System.Collections.Concurrent; using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using WireMock.Util; using WireMock.Util;
@@ -8,14 +7,14 @@ using WireMock.Validation;
namespace WireMock namespace WireMock
{ {
/// <summary> /// <summary>
/// The response. /// The ResponseMessage.
/// </summary> /// </summary>
public class ResponseMessage public class ResponseMessage
{ {
/// <summary> /// <summary>
/// Gets the headers. /// Gets the headers.
/// </summary> /// </summary>
public IDictionary<string, WireMockList<string>> Headers { get; set; } = new ConcurrentDictionary<string, WireMockList<string>>(); public IDictionary<string, WireMockList<string>> Headers { get; set; } = new Dictionary<string, WireMockList<string>>();
/// <summary> /// <summary>
/// Gets or sets the status code. /// Gets or sets the status code.

View File

@@ -210,7 +210,7 @@ namespace WireMock.Server
if (settings.SaveMapping) if (settings.SaveMapping)
{ {
var mapping = ToMapping(requestMessage, responseMessage, settings.BlackListedHeaders ?? new string[] { }); var mapping = ToMapping(requestMessage, responseMessage, settings.BlackListedHeaders ?? new string[] { });
_options.Mappings.Add(mapping.Guid, mapping); _options.Mappings.TryAdd(mapping.Guid, mapping);
if (settings.SaveMappingToFile) if (settings.SaveMappingToFile)
{ {
@@ -577,7 +577,7 @@ namespace WireMock.Server
#region Scenarios #region Scenarios
private ResponseMessage ScenariosGet(RequestMessage requestMessage) private ResponseMessage ScenariosGet(RequestMessage requestMessage)
{ {
var scenarios = Scenarios.Select(s => new var scenarios = Scenarios.ToArray().Select(s => new
{ {
Name = s.Key, Name = s.Key,
Started = s.Value != null, Started = s.Value != null,

View File

@@ -56,7 +56,7 @@ namespace WireMock.Server
/// Gets the scenarios. /// Gets the scenarios.
/// </summary> /// </summary>
[PublicAPI] [PublicAPI]
public IDictionary<string, object> Scenarios => new ConcurrentDictionary<string, object>(_options.Scenarios); public ConcurrentDictionary<string, object> Scenarios => new ConcurrentDictionary<string, object>(_options.Scenarios); // Checked
#region Start/Stop #region Start/Stop
/// <summary> /// <summary>
@@ -292,9 +292,9 @@ namespace WireMock.Server
[PublicAPI] [PublicAPI]
public void ResetMappings() public void ResetMappings()
{ {
foreach (var nonAdmin in _options.Mappings.Where(m => !m.Value.IsAdminInterface)) foreach (var nonAdmin in _options.Mappings.ToArray().Where(m => !m.Value.IsAdminInterface))
{ {
_options.Mappings.Remove(nonAdmin); _options.Mappings.TryRemove(nonAdmin.Key, out _);
} }
} }
@@ -308,7 +308,7 @@ namespace WireMock.Server
// Check a mapping exists with the same GUID, if so, remove it. // Check a mapping exists with the same GUID, if so, remove it.
if (_options.Mappings.ContainsKey(guid)) if (_options.Mappings.ContainsKey(guid))
{ {
return _options.Mappings.Remove(guid); return _options.Mappings.TryRemove(guid, out _);
} }
return false; return false;
@@ -317,7 +317,7 @@ namespace WireMock.Server
private bool DeleteMapping(string path) private bool DeleteMapping(string path)
{ {
// Check a mapping exists with the same path, if so, remove it. // Check a mapping exists with the same path, if so, remove it.
var mapping = _options.Mappings.FirstOrDefault(entry => string.Equals(entry.Value.Path, path, StringComparison.OrdinalIgnoreCase)); var mapping = _options.Mappings.ToArray().FirstOrDefault(entry => string.Equals(entry.Value.Path, path, StringComparison.OrdinalIgnoreCase));
return DeleteMapping(mapping.Key); return DeleteMapping(mapping.Key);
} }
@@ -415,7 +415,7 @@ namespace WireMock.Server
} }
else else
{ {
_options.Mappings.Add(mapping.Guid, mapping); _options.Mappings.TryAdd(mapping.Guid, mapping);
} }
} }
} }