Observable logs (refactor some code)

This commit is contained in:
Stef Heyenrath
2017-10-07 15:28:36 +02:00
parent 2d39a18b70
commit 207688e42b
9 changed files with 221 additions and 155 deletions

View File

@@ -23,6 +23,11 @@ namespace WireMock.Matchers
/// </summary> /// </summary>
public const double Perfect = 1.0; public const double Perfect = 1.0;
/// <summary>
/// The almost perfect match score
/// </summary>
public const double AlmostPerfect = 0.99;
/// <summary> /// <summary>
/// Convert a bool to the score. /// Convert a bool to the score.
/// </summary> /// </summary>

View File

@@ -25,9 +25,7 @@ namespace WireMock.Owin
{ {
Urls.Add(uriPrefix); Urls.Add(uriPrefix);
int port; PortUtil.TryExtractProtocolAndPort(uriPrefix, out string host, out int port);
string host;
PortUtil.TryExtractProtocolAndPort(uriPrefix, out host, out port);
Ports.Add(port); Ports.Add(port);
} }

View File

@@ -163,7 +163,10 @@ namespace WireMock.Owin
if (_options.MaxRequestLogCount != null) if (_options.MaxRequestLogCount != null)
{ {
var amount = _options.LogEntries.Count - _options.MaxRequestLogCount.Value; var amount = _options.LogEntries.Count - _options.MaxRequestLogCount.Value;
for (var i = 0; i < amount; i++, _options.LogEntries.RemoveAt(0)) ; for (int i = 0; i < amount; i++)
{
_options.LogEntries.RemoveAt(0);
}
} }
if (_options.RequestLogExpirationDuration != null) if (_options.RequestLogExpirationDuration != null)
@@ -174,7 +177,9 @@ namespace WireMock.Owin
{ {
var le = _options.LogEntries[i]; var le = _options.LogEntries[i];
if (le.RequestMessage.DateTime <= checkTime) if (le.RequestMessage.DateTime <= checkTime)
{
_options.LogEntries.RemoveAt(i); _options.LogEntries.RemoveAt(i);
}
} }
} }
} }

View File

@@ -16,7 +16,7 @@ namespace WireMock.Owin
public IList<Mapping> Mappings { get; set; } public IList<Mapping> Mappings { get; set; }
public ObservableCollection<LogEntry> LogEntries { get; set; } public ObservableCollection<LogEntry> LogEntries { get; }
public int? RequestLogExpirationDuration { get; set; } public int? RequestLogExpirationDuration { get; set; }

View File

@@ -447,7 +447,7 @@ namespace WireMock.Server
foreach (var logEntry in LogEntries.Where(le => !le.RequestMessage.Path.StartsWith("/__admin/"))) foreach (var logEntry in LogEntries.Where(le => !le.RequestMessage.Path.StartsWith("/__admin/")))
{ {
var requestMatchResult = new RequestMatchResult(); var requestMatchResult = new RequestMatchResult();
if (request.GetMatchingScore(logEntry.RequestMessage, requestMatchResult) > 0.99) if (request.GetMatchingScore(logEntry.RequestMessage, requestMatchResult) > MatchScores.AlmostPerfect)
{ {
dict.Add(logEntry, requestMatchResult); dict.Add(logEntry, requestMatchResult);
} }
@@ -559,7 +559,6 @@ namespace WireMock.Server
} }
return responseBuilder.WithProxy(responseModel.ProxyUrl, responseModel.X509Certificate2ThumbprintOrSubjectName); return responseBuilder.WithProxy(responseModel.ProxyUrl, responseModel.X509Certificate2ThumbprintOrSubjectName);
} }
if (responseModel.StatusCode.HasValue) if (responseModel.StatusCode.HasValue)

View File

@@ -7,6 +7,7 @@ using JetBrains.Annotations;
using WireMock.Logging; using WireMock.Logging;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
using System.Linq; using System.Linq;
using WireMock.Matchers;
namespace WireMock.Server namespace WireMock.Server
{ {
@@ -20,7 +21,7 @@ namespace WireMock.Server
{ {
add add
{ {
lock (((ICollection) _options.LogEntries).SyncRoot) lock (((ICollection)_options.LogEntries).SyncRoot)
{ {
_options.LogEntries.CollectionChanged += value; _options.LogEntries.CollectionChanged += value;
} }
@@ -69,8 +70,10 @@ namespace WireMock.Server
matcher.GetMatchingScore(log.RequestMessage, requestMatchResult); matcher.GetMatchingScore(log.RequestMessage, requestMatchResult);
} }
if (requestMatchResult.AverageTotalScore > 0.99) if (requestMatchResult.AverageTotalScore > MatchScores.AlmostPerfect)
{
results.Add(log, requestMatchResult); results.Add(log, requestMatchResult);
}
} }
return new ReadOnlyCollection<LogEntry>(results.OrderBy(x => x.Value).Select(x => x.Key).ToList()); return new ReadOnlyCollection<LogEntry>(results.OrderBy(x => x.Value).Select(x => x.Key).ToList());

View File

@@ -23,7 +23,6 @@ namespace WireMock.Server
{ {
private const int ServerStartDelay = 100; private const int ServerStartDelay = 100;
private readonly IOwinSelfHost _httpServer; private readonly IOwinSelfHost _httpServer;
private readonly object _syncRoot = new object();
private readonly WireMockMiddlewareOptions _options = new WireMockMiddlewareOptions(); private readonly WireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
/// <summary> /// <summary>
@@ -293,10 +292,7 @@ namespace WireMock.Server
[PublicAPI] [PublicAPI]
public void AddGlobalProcessingDelay(TimeSpan delay) public void AddGlobalProcessingDelay(TimeSpan delay)
{ {
lock (_syncRoot) _options.RequestProcessingDelay = delay;
{
_options.RequestProcessingDelay = delay;
}
} }
/// <summary> /// <summary>
@@ -305,10 +301,7 @@ namespace WireMock.Server
[PublicAPI] [PublicAPI]
public void AllowPartialMapping() public void AllowPartialMapping()
{ {
lock (_syncRoot) _options.AllowPartialMapping = true;
{
_options.AllowPartialMapping = true;
}
} }
/// <summary> /// <summary>
@@ -323,10 +316,7 @@ namespace WireMock.Server
Check.NotNull(password, nameof(password)); Check.NotNull(password, nameof(password));
string authorization = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password)); string authorization = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
lock (_syncRoot) _options.AuthorizationMatcher = new RegexMatcher("^(?i)BASIC " + authorization + "$");
{
_options.AuthorizationMatcher = new RegexMatcher("^(?i)BASIC " + authorization + "$");
}
} }
/// <summary> /// <summary>
@@ -335,10 +325,7 @@ namespace WireMock.Server
[PublicAPI] [PublicAPI]
public void RemoveBasicAuthentication() public void RemoveBasicAuthentication()
{ {
lock (_syncRoot) _options.AuthorizationMatcher = null;
{
_options.AuthorizationMatcher = null;
}
} }
/// <summary> /// <summary>
@@ -348,10 +335,8 @@ namespace WireMock.Server
[PublicAPI] [PublicAPI]
public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount) public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount)
{ {
lock (_syncRoot) _options.MaxRequestLogCount = maxRequestLogCount;
{
_options.MaxRequestLogCount = maxRequestLogCount;
}
} }
/// <summary> /// <summary>
@@ -361,10 +346,7 @@ namespace WireMock.Server
[PublicAPI] [PublicAPI]
public void SetRequestLogExpirationDuration([CanBeNull] int? requestLogExpirationDuration) public void SetRequestLogExpirationDuration([CanBeNull] int? requestLogExpirationDuration)
{ {
lock (_syncRoot) _options.RequestLogExpirationDuration = requestLogExpirationDuration;
{
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
}
} }
/// <summary> /// <summary>

View File

@@ -0,0 +1,81 @@
using System;
using System.Collections.Concurrent;
using System.Threading;
namespace WireMock.Util
{
/// <summary>
/// http://johnculviner.com/achieving-named-lock-locker-functionality-in-c-4-0/
/// </summary>
internal class NamedReaderWriterLocker
{
private readonly ConcurrentDictionary<string, ReaderWriterLockSlim> _lockDict = new ConcurrentDictionary<string, ReaderWriterLockSlim>();
public ReaderWriterLockSlim GetLock(string name)
{
return _lockDict.GetOrAdd(name, s => new ReaderWriterLockSlim());
}
public TResult RunWithReadLock<TResult>(string name, Func<TResult> body)
{
var rwLock = GetLock(name);
try
{
rwLock.EnterReadLock();
return body();
}
finally
{
rwLock.ExitReadLock();
}
}
public void RunWithReadLock(string name, Action body)
{
var rwLock = GetLock(name);
try
{
rwLock.EnterReadLock();
body();
}
finally
{
rwLock.ExitReadLock();
}
}
public TResult RunWithWriteLock<TResult>(string name, Func<TResult> body)
{
var rwLock = GetLock(name);
try
{
rwLock.EnterWriteLock();
return body();
}
finally
{
rwLock.ExitWriteLock();
}
}
public void RunWithWriteLock(string name, Action body)
{
var rwLock = GetLock(name);
try
{
rwLock.EnterWriteLock();
body();
}
finally
{
rwLock.ExitWriteLock();
}
}
public void RemoveLock(string name)
{
ReaderWriterLockSlim o;
_lockDict.TryRemove(name, out o);
}
}
}

View File

@@ -1,12 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using NFluent; using NFluent;
using RestEase;
using WireMock.RequestBuilders; using WireMock.RequestBuilders;
using WireMock.ResponseBuilders; using WireMock.ResponseBuilders;
using WireMock.Server; using WireMock.Server;
@@ -21,7 +15,7 @@ namespace WireMock.Net.Tests
[Fact] [Fact]
public async void Test() public async void Test()
{ {
// given // Assign
_server = FluentMockServer.Start(); _server = FluentMockServer.Start();
_server _server
@@ -29,16 +23,15 @@ namespace WireMock.Net.Tests
.WithPath("/foo") .WithPath("/foo")
.UsingGet()) .UsingGet())
.RespondWith(Response.Create() .RespondWith(Response.Create()
.WithStatusCode(200)
.WithBody(@"{ msg: ""Hello world!""}")); .WithBody(@"{ msg: ""Hello world!""}"));
var count = 0; int count = 0;
_server.LogEntriesChanged += (sender, args) => count++; _server.LogEntriesChanged += (sender, args) => count++;
// when // Act
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo"); await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
// then // Assert
Check.That(count).Equals(1); Check.That(count).Equals(1);
} }