From 93c91562495f35aa1f7dda0a1d50c4f12966a32c Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Thu, 21 Jan 2016 13:09:14 -0800 Subject: [PATCH] Added size limit for debug stream queue as well as unit test. --- .../Commands.Common/Commands.Common.csproj | 1 + .../ConcurrentQueueExtensions.cs | 44 +++++++++++++++++++ .../DebugStreamTraceListener.cs | 2 +- .../RecordingTracingInterceptor.cs | 7 +-- .../ServiceClientTracingInterceptor.cs | 7 ++- .../AzureRMProfileTests.cs | 20 ++++++++- 6 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 src/Common/Commands.Common/ConcurrentQueueExtensions.cs diff --git a/src/Common/Commands.Common/Commands.Common.csproj b/src/Common/Commands.Common/Commands.Common.csproj index 13fced02e93a..d3d259ea8a98 100644 --- a/src/Common/Commands.Common/Commands.Common.csproj +++ b/src/Common/Commands.Common/Commands.Common.csproj @@ -149,6 +149,7 @@ + diff --git a/src/Common/Commands.Common/ConcurrentQueueExtensions.cs b/src/Common/Commands.Common/ConcurrentQueueExtensions.cs new file mode 100644 index 000000000000..8d47736950a2 --- /dev/null +++ b/src/Common/Commands.Common/ConcurrentQueueExtensions.cs @@ -0,0 +1,44 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Collections.Concurrent; + +namespace Microsoft.WindowsAzure.Commands.Common +{ + public static class ConcurrentQueueExtensions + { + private const int Capacity = 500; + public static void CheckAndEnqueue(this ConcurrentQueue queue, string item) + { + if (queue == null || item == null) + { + return; + } + lock(queue) + { + while (queue.Count >= Capacity) + { + string result; + queue.TryDequeue(out result); + } + queue.Enqueue(item); + } + } + } +} diff --git a/src/Common/Commands.Common/DebugStreamTraceListener.cs b/src/Common/Commands.Common/DebugStreamTraceListener.cs index bae605aeff87..c75fa3fe7bb9 100644 --- a/src/Common/Commands.Common/DebugStreamTraceListener.cs +++ b/src/Common/Commands.Common/DebugStreamTraceListener.cs @@ -34,7 +34,7 @@ public static void AddAdalTracing(DebugStreamTraceListener listener) public ConcurrentQueue Messages; public override void Write(string message) { - Messages.Enqueue(message); + Messages.CheckAndEnqueue(message); } public override void WriteLine(string message) diff --git a/src/Common/Commands.Common/RecordingTracingInterceptor.cs b/src/Common/Commands.Common/RecordingTracingInterceptor.cs index ec8400ceb089..3bce7d23a45f 100644 --- a/src/Common/Commands.Common/RecordingTracingInterceptor.cs +++ b/src/Common/Commands.Common/RecordingTracingInterceptor.cs @@ -18,6 +18,7 @@ using System.Net.Http; using Hyak.Common; using Microsoft.WindowsAzure.Commands.Utilities.Common; +using Microsoft.WindowsAzure.Commands.Common; namespace Microsoft.Azure.Common.Authentication.Models { @@ -34,17 +35,17 @@ private void Write(string message, params object[] arguments) { if (arguments == null || arguments.Length == 0) { - MessageQueue.Enqueue(message); + MessageQueue.CheckAndEnqueue(message); } else { - MessageQueue.Enqueue(string.Format(message, arguments)); + MessageQueue.CheckAndEnqueue(string.Format(message, arguments)); } } public void Information(string message) { - MessageQueue.Enqueue(message); + MessageQueue.CheckAndEnqueue(message); } public void Configuration(string source, string name, string value) diff --git a/src/Common/Commands.Common/ServiceClientTracingInterceptor.cs b/src/Common/Commands.Common/ServiceClientTracingInterceptor.cs index 291668d24f26..f31983f778c7 100644 --- a/src/Common/Commands.Common/ServiceClientTracingInterceptor.cs +++ b/src/Common/Commands.Common/ServiceClientTracingInterceptor.cs @@ -20,7 +20,6 @@ using System.Linq; using System.Text; using System.Threading.Tasks; - namespace Microsoft.WindowsAzure.Commands.Common { class ServiceClientTracingInterceptor : IServiceClientTracingInterceptor @@ -49,19 +48,19 @@ public void ExitMethod(string invocationId, object returnValue) public void Information(string message) { - MessageQueue.Enqueue(message); + MessageQueue.CheckAndEnqueue(message); } public void ReceiveResponse(string invocationId, System.Net.Http.HttpResponseMessage response) { string responseAsString = response == null ? string.Empty : response.AsFormattedString(); - MessageQueue.Enqueue(responseAsString); + MessageQueue.CheckAndEnqueue(responseAsString); } public void SendRequest(string invocationId, System.Net.Http.HttpRequestMessage request) { string requestAsString = request == null ? string.Empty : request.AsFormattedString(); - MessageQueue.Enqueue(requestAsString); + MessageQueue.CheckAndEnqueue(requestAsString); } public void TraceError(string invocationId, Exception exception) diff --git a/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs b/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs index 7a7404c508ce..77242ab25f11 100644 --- a/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs +++ b/src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs @@ -23,6 +23,8 @@ using Microsoft.WindowsAzure.Commands.ScenarioTest; using Microsoft.WindowsAzure.Commands.Common; using Moq; +using System.Collections.Concurrent; +using System.Threading.Tasks; namespace Microsoft.Azure.Commands.ResourceManager.Common.Test { @@ -243,7 +245,7 @@ public void NoSubscriptionsInListDoesNotThrow() [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] - public void SetContextPreservesTokenCache() + public void SetContextPreservesTokenCache() { AzureRMProfile profile = null; AzureContext context = new AzureContext(null, null, null, null); @@ -253,5 +255,21 @@ public void SetContextPreservesTokenCache() profile.SetContextWithCache(context); Assert.Equal(TokenCache.DefaultShared.Serialize(), profile.Context.TokenCache); } + + [Fact] + public void AzurePSComletMessageQueue() + { + ConcurrentQueue queue = new ConcurrentQueue(); + + Parallel.For(0, 5, i => + { + for (int j = 0; j < 300; j++) + { + queue.CheckAndEnqueue(j.ToString()); + } + }); + + Assert.Equal(500, queue.Count); + } } }