Skip to content

[#111991911] Limit the size of the ConcurrentQueue of debug messages #1698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Common/Commands.Common/Commands.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
<Compile Include="AzureRmProfileProvider.cs" />
<Compile Include="AzureSMProfileProvder.cs" />
<Compile Include="AzureSubscriptionExtensions.cs" />
<Compile Include="ConcurrentQueueExtensions.cs" />
<Compile Include="Constants.cs" />
<Compile Include="ContextExtensions.cs" />
<Compile Include="IProfileProvider.cs" />
Expand Down
44 changes: 44 additions & 0 deletions src/Common/Commands.Common/ConcurrentQueueExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<string> queue, string item)
{
if (queue == null || item == null)
{
return;
}
lock(queue)
{
while (queue.Count >= Capacity)
{
string result;
queue.TryDequeue(out result);
}
queue.Enqueue(item);
}
}
}
}
2 changes: 1 addition & 1 deletion src/Common/Commands.Common/DebugStreamTraceListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void AddAdalTracing(DebugStreamTraceListener listener)
public ConcurrentQueue<string> Messages;
public override void Write(string message)
{
Messages.Enqueue(message);
Messages.CheckAndEnqueue(message);
}

public override void WriteLine(string message)
Expand Down
7 changes: 4 additions & 3 deletions src/Common/Commands.Common/RecordingTracingInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions src/Common/Commands.Common/ServiceClientTracingInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.WindowsAzure.Commands.Common
{
class ServiceClientTracingInterceptor : IServiceClientTracingInterceptor
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand All @@ -253,5 +255,21 @@ public void SetContextPreservesTokenCache()
profile.SetContextWithCache(context);
Assert.Equal(TokenCache.DefaultShared.Serialize(), profile.Context.TokenCache);
}

[Fact]
public void AzurePSComletMessageQueue()
{
ConcurrentQueue<string> queue = new ConcurrentQueue<string>();

Parallel.For(0, 5, i =>
{
for (int j = 0; j < 300; j++)
{
queue.CheckAndEnqueue(j.ToString());
}
});

Assert.Equal(500, queue.Count);
}
}
}