Stop using ConcurrentBag in tests for complex types (#1990)
dotnet/corefx#23068
This commit is contained in:
parent
d3fb5e76f1
commit
02028b65c7
|
|
@ -535,10 +535,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
|
|||
public async Task StartRequestProcessingCreatesLogScopeWithConnectionId()
|
||||
{
|
||||
_frameConnection.StartRequestProcessing(new DummyApplication());
|
||||
|
||||
_frameConnection.OnConnectionClosed(ex: null);
|
||||
|
||||
await _frameConnection.StopAsync().TimeoutAfter(TimeSpan.FromSeconds(5));
|
||||
|
||||
var scopeObjects = ((TestKestrelTrace)_frameConnectionContext.ServiceContext.Log)
|
||||
.Logger
|
||||
|
|
@ -546,10 +542,17 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
|
|||
.OfType<IReadOnlyList<KeyValuePair<string, object>>>()
|
||||
.ToList();
|
||||
|
||||
_frameConnection.OnConnectionClosed(ex: null);
|
||||
|
||||
await _frameConnection.StopAsync().TimeoutAfter(TimeSpan.FromSeconds(5));
|
||||
|
||||
Assert.Equal(1, scopeObjects.Count);
|
||||
var pairs = scopeObjects[0].ToDictionary(p => p.Key, p => p.Value);
|
||||
Assert.True(pairs.ContainsKey("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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
|
|||
private class TestEventListener : EventListener
|
||||
{
|
||||
private volatile bool _disposed;
|
||||
private ConcurrentBag<EventWrittenEventArgs> _events = new ConcurrentBag<EventWrittenEventArgs>();
|
||||
private ConcurrentQueue<EventWrittenEventArgs> _events = new ConcurrentQueue<EventWrittenEventArgs>();
|
||||
|
||||
public IEnumerable<EventWrittenEventArgs> EventData => _events;
|
||||
|
||||
|
|
@ -92,7 +92,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
|
|||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
_events.Add(eventData);
|
||||
_events.Enqueue(eventData);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
|
@ -17,9 +16,9 @@ namespace Microsoft.AspNetCore.Testing
|
|||
|
||||
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);
|
||||
|
||||
|
|
@ -29,8 +28,9 @@ namespace Microsoft.AspNetCore.Testing
|
|||
|
||||
public IDisposable BeginScope<TState>(TState state)
|
||||
{
|
||||
Scopes.Add(state);
|
||||
return new Disposable(() => { });
|
||||
Scopes.Enqueue(state);
|
||||
|
||||
return new Disposable(() => { Scopes.TryDequeue(out _); });
|
||||
}
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel)
|
||||
|
|
@ -52,7 +52,7 @@ namespace Microsoft.AspNetCore.Testing
|
|||
}
|
||||
}
|
||||
|
||||
Messages.Add(new LogMessage
|
||||
Messages.Enqueue(new LogMessage
|
||||
{
|
||||
LogLevel = logLevel,
|
||||
EventId = eventId,
|
||||
|
|
|
|||
Loading…
Reference in New Issue