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>
public const double Perfect = 1.0;
/// <summary>
/// The almost perfect match score
/// </summary>
public const double AlmostPerfect = 0.99;
/// <summary>
/// Convert a bool to the score.
/// </summary>

View File

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

View File

@@ -163,7 +163,10 @@ namespace WireMock.Owin
if (_options.MaxRequestLogCount != null)
{
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)
@@ -174,7 +177,9 @@ namespace WireMock.Owin
{
var le = _options.LogEntries[i];
if (le.RequestMessage.DateTime <= checkTime)
{
_options.LogEntries.RemoveAt(i);
}
}
}
}

View File

@@ -16,7 +16,7 @@ namespace WireMock.Owin
public IList<Mapping> Mappings { get; set; }
public ObservableCollection<LogEntry> LogEntries { get; set; }
public ObservableCollection<LogEntry> LogEntries { get; }
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/")))
{
var requestMatchResult = new RequestMatchResult();
if (request.GetMatchingScore(logEntry.RequestMessage, requestMatchResult) > 0.99)
if (request.GetMatchingScore(logEntry.RequestMessage, requestMatchResult) > MatchScores.AlmostPerfect)
{
dict.Add(logEntry, requestMatchResult);
}
@@ -559,7 +559,6 @@ namespace WireMock.Server
}
return responseBuilder.WithProxy(responseModel.ProxyUrl, responseModel.X509Certificate2ThumbprintOrSubjectName);
}
if (responseModel.StatusCode.HasValue)

View File

@@ -1,113 +1,116 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using JetBrains.Annotations;
using WireMock.Logging;
using WireMock.Matchers.Request;
using System.Linq;
namespace WireMock.Server
{
public partial class FluentMockServer
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using JetBrains.Annotations;
using WireMock.Logging;
using WireMock.Matchers.Request;
using System.Linq;
using WireMock.Matchers;
namespace WireMock.Server
{
public partial class FluentMockServer
{
/// <summary>
/// Log entries notification handler
/// </summary>
[PublicAPI]
public event NotifyCollectionChangedEventHandler LogEntriesChanged
{
add
{
lock (((ICollection) _options.LogEntries).SyncRoot)
{
_options.LogEntries.CollectionChanged += value;
}
}
remove
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
_options.LogEntries.CollectionChanged -= value;
}
}
}
/// <summary>
/// Gets the request logs.
/// </summary>
[PublicAPI]
public IEnumerable<LogEntry> LogEntries
{
get
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
return new ReadOnlyCollection<LogEntry>(_options.LogEntries);
}
}
}
/// <summary>
/// The search log-entries based on matchers.
/// </summary>
/// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IEnumerable"/>.</returns>
[PublicAPI]
public IEnumerable<LogEntry> FindLogEntries([NotNull] params IRequestMatcher[] matchers)
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
var results = new Dictionary<LogEntry, RequestMatchResult>();
foreach (var log in _options.LogEntries)
{
var requestMatchResult = new RequestMatchResult();
foreach (var matcher in matchers)
{
matcher.GetMatchingScore(log.RequestMessage, requestMatchResult);
}
if (requestMatchResult.AverageTotalScore > 0.99)
results.Add(log, requestMatchResult);
}
return new ReadOnlyCollection<LogEntry>(results.OrderBy(x => x.Value).Select(x => x.Key).ToList());
}
}
/// <summary>
/// Resets the LogEntries.
/// </summary>
[PublicAPI]
public void ResetLogEntries()
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
_options.LogEntries.Clear();
}
}
/// <summary>
/// Deletes the mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
[PublicAPI]
public bool DeleteLogEntry(Guid guid)
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
// Check a logentry exists with the same GUID, if so, remove it.
var existing = _options.LogEntries.FirstOrDefault(m => m.Guid == guid);
if (existing != null)
{
_options.LogEntries.Remove(existing);
return true;
}
return false;
}
}
}
}
/// </summary>
[PublicAPI]
public event NotifyCollectionChangedEventHandler LogEntriesChanged
{
add
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
_options.LogEntries.CollectionChanged += value;
}
}
remove
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
_options.LogEntries.CollectionChanged -= value;
}
}
}
/// <summary>
/// Gets the request logs.
/// </summary>
[PublicAPI]
public IEnumerable<LogEntry> LogEntries
{
get
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
return new ReadOnlyCollection<LogEntry>(_options.LogEntries);
}
}
}
/// <summary>
/// The search log-entries based on matchers.
/// </summary>
/// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IEnumerable"/>.</returns>
[PublicAPI]
public IEnumerable<LogEntry> FindLogEntries([NotNull] params IRequestMatcher[] matchers)
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
var results = new Dictionary<LogEntry, RequestMatchResult>();
foreach (var log in _options.LogEntries)
{
var requestMatchResult = new RequestMatchResult();
foreach (var matcher in matchers)
{
matcher.GetMatchingScore(log.RequestMessage, requestMatchResult);
}
if (requestMatchResult.AverageTotalScore > MatchScores.AlmostPerfect)
{
results.Add(log, requestMatchResult);
}
}
return new ReadOnlyCollection<LogEntry>(results.OrderBy(x => x.Value).Select(x => x.Key).ToList());
}
}
/// <summary>
/// Resets the LogEntries.
/// </summary>
[PublicAPI]
public void ResetLogEntries()
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
_options.LogEntries.Clear();
}
}
/// <summary>
/// Deletes the mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
[PublicAPI]
public bool DeleteLogEntry(Guid guid)
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
// Check a logentry exists with the same GUID, if so, remove it.
var existing = _options.LogEntries.FirstOrDefault(m => m.Guid == guid);
if (existing != null)
{
_options.LogEntries.Remove(existing);
return true;
}
return false;
}
}
}
}

View File

@@ -23,7 +23,6 @@ namespace WireMock.Server
{
private const int ServerStartDelay = 100;
private readonly IOwinSelfHost _httpServer;
private readonly object _syncRoot = new object();
private readonly WireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
/// <summary>
@@ -293,10 +292,7 @@ namespace WireMock.Server
[PublicAPI]
public void AddGlobalProcessingDelay(TimeSpan delay)
{
lock (_syncRoot)
{
_options.RequestProcessingDelay = delay;
}
_options.RequestProcessingDelay = delay;
}
/// <summary>
@@ -305,10 +301,7 @@ namespace WireMock.Server
[PublicAPI]
public void AllowPartialMapping()
{
lock (_syncRoot)
{
_options.AllowPartialMapping = true;
}
_options.AllowPartialMapping = true;
}
/// <summary>
@@ -323,10 +316,7 @@ namespace WireMock.Server
Check.NotNull(password, nameof(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>
@@ -335,10 +325,7 @@ namespace WireMock.Server
[PublicAPI]
public void RemoveBasicAuthentication()
{
lock (_syncRoot)
{
_options.AuthorizationMatcher = null;
}
_options.AuthorizationMatcher = null;
}
/// <summary>
@@ -348,10 +335,8 @@ namespace WireMock.Server
[PublicAPI]
public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount)
{
lock (_syncRoot)
{
_options.MaxRequestLogCount = maxRequestLogCount;
}
_options.MaxRequestLogCount = maxRequestLogCount;
}
/// <summary>
@@ -361,10 +346,7 @@ namespace WireMock.Server
[PublicAPI]
public void SetRequestLogExpirationDuration([CanBeNull] int? requestLogExpirationDuration)
{
lock (_syncRoot)
{
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
}
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
}
/// <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.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using NFluent;
using RestEase;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
@@ -21,7 +15,7 @@ namespace WireMock.Net.Tests
[Fact]
public async void Test()
{
// given
// Assign
_server = FluentMockServer.Start();
_server
@@ -29,16 +23,15 @@ namespace WireMock.Net.Tests
.WithPath("/foo")
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithBody(@"{ msg: ""Hello world!""}"));
var count = 0;
int count = 0;
_server.LogEntriesChanged += (sender, args) => count++;
// when
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
// Act
await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
// then
// Assert
Check.That(count).Equals(1);
}