Skip to content

Commit 1336ba8

Browse files
author
Hovsep
committed
Merge pull request Azure#1698 from haocs/bugfix
[#111991911] Limit the size of the ConcurrentQueue of debug messages
2 parents dfc4cf3 + 2b50322 commit 1336ba8

File tree

6 files changed

+72
-9
lines changed

6 files changed

+72
-9
lines changed

src/Common/Commands.Common/Commands.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
<Compile Include="AzureRmProfileProvider.cs" />
150150
<Compile Include="AzureSMProfileProvder.cs" />
151151
<Compile Include="AzureSubscriptionExtensions.cs" />
152+
<Compile Include="ConcurrentQueueExtensions.cs" />
152153
<Compile Include="Constants.cs" />
153154
<Compile Include="ContextExtensions.cs" />
154155
<Compile Include="IProfileProvider.cs" />
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
using System.Text;
19+
using System.Threading.Tasks;
20+
using System.Collections.Concurrent;
21+
22+
namespace Microsoft.WindowsAzure.Commands.Common
23+
{
24+
public static class ConcurrentQueueExtensions
25+
{
26+
private const int Capacity = 500;
27+
public static void CheckAndEnqueue(this ConcurrentQueue<string> queue, string item)
28+
{
29+
if (queue == null || item == null)
30+
{
31+
return;
32+
}
33+
lock(queue)
34+
{
35+
while (queue.Count >= Capacity)
36+
{
37+
string result;
38+
queue.TryDequeue(out result);
39+
}
40+
queue.Enqueue(item);
41+
}
42+
}
43+
}
44+
}

src/Common/Commands.Common/DebugStreamTraceListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static void AddAdalTracing(DebugStreamTraceListener listener)
3434
public ConcurrentQueue<string> Messages;
3535
public override void Write(string message)
3636
{
37-
Messages.Enqueue(message);
37+
Messages.CheckAndEnqueue(message);
3838
}
3939

4040
public override void WriteLine(string message)

src/Common/Commands.Common/RecordingTracingInterceptor.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.Net.Http;
1919
using Hyak.Common;
2020
using Microsoft.WindowsAzure.Commands.Utilities.Common;
21+
using Microsoft.WindowsAzure.Commands.Common;
2122

2223
namespace Microsoft.Azure.Common.Authentication.Models
2324
{
@@ -34,17 +35,17 @@ private void Write(string message, params object[] arguments)
3435
{
3536
if (arguments == null || arguments.Length == 0)
3637
{
37-
MessageQueue.Enqueue(message);
38+
MessageQueue.CheckAndEnqueue(message);
3839
}
3940
else
4041
{
41-
MessageQueue.Enqueue(string.Format(message, arguments));
42+
MessageQueue.CheckAndEnqueue(string.Format(message, arguments));
4243
}
4344
}
4445

4546
public void Information(string message)
4647
{
47-
MessageQueue.Enqueue(message);
48+
MessageQueue.CheckAndEnqueue(message);
4849
}
4950

5051
public void Configuration(string source, string name, string value)

src/Common/Commands.Common/ServiceClientTracingInterceptor.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
using System.Linq;
2121
using System.Text;
2222
using System.Threading.Tasks;
23-
2423
namespace Microsoft.WindowsAzure.Commands.Common
2524
{
2625
class ServiceClientTracingInterceptor : IServiceClientTracingInterceptor
@@ -49,19 +48,19 @@ public void ExitMethod(string invocationId, object returnValue)
4948

5049
public void Information(string message)
5150
{
52-
MessageQueue.Enqueue(message);
51+
MessageQueue.CheckAndEnqueue(message);
5352
}
5453

5554
public void ReceiveResponse(string invocationId, System.Net.Http.HttpResponseMessage response)
5655
{
5756
string responseAsString = response == null ? string.Empty : response.AsFormattedString();
58-
MessageQueue.Enqueue(responseAsString);
57+
MessageQueue.CheckAndEnqueue(responseAsString);
5958
}
6059

6160
public void SendRequest(string invocationId, System.Net.Http.HttpRequestMessage request)
6261
{
6362
string requestAsString = request == null ? string.Empty : request.AsFormattedString();
64-
MessageQueue.Enqueue(requestAsString);
63+
MessageQueue.CheckAndEnqueue(requestAsString);
6564
}
6665

6766
public void TraceError(string invocationId, Exception exception)

src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
using Microsoft.WindowsAzure.Commands.ScenarioTest;
2424
using Microsoft.WindowsAzure.Commands.Common;
2525
using Moq;
26+
using System.Collections.Concurrent;
27+
using System.Threading.Tasks;
2628

2729
namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
2830
{
@@ -243,7 +245,7 @@ public void NoSubscriptionsInListDoesNotThrow()
243245

244246
[Fact]
245247
[Trait(Category.AcceptanceType, Category.CheckIn)]
246-
public void SetContextPreservesTokenCache()
248+
public void SetContextPreservesTokenCache()
247249
{
248250
AzureRMProfile profile = null;
249251
AzureContext context = new AzureContext(null, null, null, null);
@@ -253,5 +255,21 @@ public void SetContextPreservesTokenCache()
253255
profile.SetContextWithCache(context);
254256
Assert.Equal(TokenCache.DefaultShared.Serialize(), profile.Context.TokenCache);
255257
}
258+
259+
[Fact]
260+
public void AzurePSComletMessageQueue()
261+
{
262+
ConcurrentQueue<string> queue = new ConcurrentQueue<string>();
263+
264+
Parallel.For(0, 5, i =>
265+
{
266+
for (int j = 0; j < 300; j++)
267+
{
268+
queue.CheckAndEnqueue(j.ToString());
269+
}
270+
});
271+
272+
Assert.Equal(500, queue.Count);
273+
}
256274
}
257275
}

0 commit comments

Comments
 (0)