Skip to content

Commit c0b6f56

Browse files
committed
Making FileTraceWriter lock static to fix concurrency issues
1 parent 061a98d commit c0b6f56

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/WebJobs.Script/FileTraceWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.Azure.WebJobs.Script
1212
{
1313
public class FileTraceWriter : TraceWriter
1414
{
15-
private object _syncLock = new object();
15+
private static object _syncLock = new object();
1616
private readonly string _logFilePath;
1717
private readonly string _instanceId;
1818
private const long _maxLogFileSizeBytes = 5 * 1024 * 1024;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System.Diagnostics;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using Microsoft.Azure.WebJobs.Script;
10+
using Xunit;
11+
12+
namespace WebJobs.Script.Tests
13+
{
14+
public class FileTraceWriterTests
15+
{
16+
private string _logFilePath;
17+
18+
public FileTraceWriterTests()
19+
{
20+
_logFilePath = Path.Combine(Path.GetTempPath(), "WebJobs.Script.Tests", "FileTraceWriterTests");
21+
22+
if (Directory.Exists(_logFilePath))
23+
{
24+
Directory.Delete(_logFilePath, recursive: true);
25+
}
26+
}
27+
28+
[Fact]
29+
public async Task MultipleConcurrentInstances_WritesAreSerialized()
30+
{
31+
int numLines = 100;
32+
33+
// first ensure the file exists by writing a single entry
34+
// this ensures all the instances below will be operating on
35+
// the same file
36+
WriteLogs(_logFilePath, 1);
37+
38+
// start 3 concurrent instances
39+
await Task.WhenAll(
40+
Task.Run(() => WriteLogs(_logFilePath, numLines)),
41+
Task.Run(() => WriteLogs(_logFilePath, numLines)),
42+
Task.Run(() => WriteLogs(_logFilePath, numLines))
43+
);
44+
45+
string logFile = Directory.EnumerateFiles(_logFilePath).Single();
46+
string[] fileLines = File.ReadAllLines(logFile);
47+
Assert.Equal(3 * numLines + 1, fileLines.Length);
48+
}
49+
50+
private void WriteLogs(string logFilePath, int numLogs)
51+
{
52+
FileTraceWriter traceWriter = new FileTraceWriter(logFilePath, TraceLevel.Verbose);
53+
54+
for (int i = 0; i < numLogs; i++)
55+
{
56+
traceWriter.Verbose(string.Format("Test message {0} {1}", Thread.CurrentThread.ManagedThreadId, i));
57+
}
58+
}
59+
}
60+
}

test/WebJobs.Script.Tests/WebJobs.Script.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
<Compile Include="BashEndToEndTests.cs" />
162162
<Compile Include="EndToEndTestFixture.cs" />
163163
<Compile Include="EndToEndTestsBase.cs" />
164+
<Compile Include="FileTraceWriterTests.cs" />
164165
<Compile Include="FSharpEndToEndTests.cs" />
165166
<Compile Include="FunctionGeneratorTests.cs" />
166167
<Compile Include="PhpEndToEndTests.cs" />

0 commit comments

Comments
 (0)