Stop using ConcurrentBag in tests for complex types (#1990)

dotnet/corefx#23068
This commit is contained in:
Stephen Halter 2017-08-11 16:30:03 -07:00 committed by GitHub
parent d3fb5e76f1
commit 02028b65c7
3 changed files with 15 additions and 12 deletions

View File

@ -535,10 +535,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
public async Task StartRequestProcessingCreatesLogScopeWithConnectionId() public async Task StartRequestProcessingCreatesLogScopeWithConnectionId()
{ {
_frameConnection.StartRequestProcessing(new DummyApplication()); _frameConnection.StartRequestProcessing(new DummyApplication());
_frameConnection.OnConnectionClosed(ex: null);
await _frameConnection.StopAsync().TimeoutAfter(TimeSpan.FromSeconds(5));
var scopeObjects = ((TestKestrelTrace)_frameConnectionContext.ServiceContext.Log) var scopeObjects = ((TestKestrelTrace)_frameConnectionContext.ServiceContext.Log)
.Logger .Logger
@ -546,10 +542,17 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
.OfType<IReadOnlyList<KeyValuePair<string, object>>>() .OfType<IReadOnlyList<KeyValuePair<string, object>>>()
.ToList(); .ToList();
_frameConnection.OnConnectionClosed(ex: null);
await _frameConnection.StopAsync().TimeoutAfter(TimeSpan.FromSeconds(5));
Assert.Equal(1, scopeObjects.Count); Assert.Equal(1, scopeObjects.Count);
var pairs = scopeObjects[0].ToDictionary(p => p.Key, p => p.Value); var pairs = scopeObjects[0].ToDictionary(p => p.Key, p => p.Value);
Assert.True(pairs.ContainsKey("ConnectionId")); Assert.True(pairs.ContainsKey("ConnectionId"));
Assert.Equal(_frameConnection.ConnectionId, pairs["ConnectionId"]); Assert.Equal(_frameConnection.ConnectionId, pairs["ConnectionId"]);
// Verify the scope was disposed after request processing completed
Assert.True(((TestKestrelTrace)_frameConnectionContext.ServiceContext.Log).Logger.Scopes.IsEmpty);
} }
} }
} }

View File

@ -84,7 +84,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
private class TestEventListener : EventListener private class TestEventListener : EventListener
{ {
private volatile bool _disposed; private volatile bool _disposed;
private ConcurrentBag<EventWrittenEventArgs> _events = new ConcurrentBag<EventWrittenEventArgs>(); private ConcurrentQueue<EventWrittenEventArgs> _events = new ConcurrentQueue<EventWrittenEventArgs>();
public IEnumerable<EventWrittenEventArgs> EventData => _events; public IEnumerable<EventWrittenEventArgs> EventData => _events;
@ -92,7 +92,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{ {
if (!_disposed) if (!_disposed)
{ {
_events.Add(eventData); _events.Enqueue(eventData);
} }
} }

View File

@ -3,7 +3,6 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Threading.Tasks;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -17,9 +16,9 @@ namespace Microsoft.AspNetCore.Testing
public bool ThrowOnCriticalErrors { get; set; } = true; public bool ThrowOnCriticalErrors { get; set; } = true;
public ConcurrentBag<LogMessage> Messages { get; } = new ConcurrentBag<LogMessage>(); public ConcurrentQueue<LogMessage> Messages { get; } = new ConcurrentQueue<LogMessage>();
public ConcurrentBag<object> Scopes { get; } = new ConcurrentBag<object>(); public ConcurrentQueue<object> Scopes { get; } = new ConcurrentQueue<object>();
public int TotalErrorsLogged => Messages.Count(message => message.LogLevel == LogLevel.Error); public int TotalErrorsLogged => Messages.Count(message => message.LogLevel == LogLevel.Error);
@ -29,8 +28,9 @@ namespace Microsoft.AspNetCore.Testing
public IDisposable BeginScope<TState>(TState state) public IDisposable BeginScope<TState>(TState state)
{ {
Scopes.Add(state); Scopes.Enqueue(state);
return new Disposable(() => { });
return new Disposable(() => { Scopes.TryDequeue(out _); });
} }
public bool IsEnabled(LogLevel logLevel) public bool IsEnabled(LogLevel logLevel)
@ -52,7 +52,7 @@ namespace Microsoft.AspNetCore.Testing
} }
} }
Messages.Add(new LogMessage Messages.Enqueue(new LogMessage
{ {
LogLevel = logLevel, LogLevel = logLevel,
EventId = eventId, EventId = eventId,