From 02028b65c797aa51f93b28f48a2f805bf03e9576 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Fri, 11 Aug 2017 16:30:03 -0700 Subject: [PATCH] Stop using ConcurrentBag in tests for complex types (#1990) dotnet/corefx#23068 --- test/Kestrel.Core.Tests/FrameConnectionTests.cs | 11 +++++++---- test/Kestrel.FunctionalTests/EventSourceTests.cs | 4 ++-- test/shared/TestApplicationErrorLogger.cs | 12 ++++++------ 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/test/Kestrel.Core.Tests/FrameConnectionTests.cs b/test/Kestrel.Core.Tests/FrameConnectionTests.cs index 9cf86bc7dc..555838c820 100644 --- a/test/Kestrel.Core.Tests/FrameConnectionTests.cs +++ b/test/Kestrel.Core.Tests/FrameConnectionTests.cs @@ -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>>() .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); } } } diff --git a/test/Kestrel.FunctionalTests/EventSourceTests.cs b/test/Kestrel.FunctionalTests/EventSourceTests.cs index e2f40011a7..639e8d5153 100644 --- a/test/Kestrel.FunctionalTests/EventSourceTests.cs +++ b/test/Kestrel.FunctionalTests/EventSourceTests.cs @@ -84,7 +84,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests private class TestEventListener : EventListener { private volatile bool _disposed; - private ConcurrentBag _events = new ConcurrentBag(); + private ConcurrentQueue _events = new ConcurrentQueue(); public IEnumerable EventData => _events; @@ -92,7 +92,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests { if (!_disposed) { - _events.Add(eventData); + _events.Enqueue(eventData); } } diff --git a/test/shared/TestApplicationErrorLogger.cs b/test/shared/TestApplicationErrorLogger.cs index 936624f1a8..144b251802 100644 --- a/test/shared/TestApplicationErrorLogger.cs +++ b/test/shared/TestApplicationErrorLogger.cs @@ -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 Messages { get; } = new ConcurrentBag(); + public ConcurrentQueue Messages { get; } = new ConcurrentQueue(); - public ConcurrentBag Scopes { get; } = new ConcurrentBag(); + public ConcurrentQueue Scopes { get; } = new ConcurrentQueue(); public int TotalErrorsLogged => Messages.Count(message => message.LogLevel == LogLevel.Error); @@ -29,8 +28,9 @@ namespace Microsoft.AspNetCore.Testing public IDisposable BeginScope(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,