Skip to content

Commit 0281253

Browse files
authored
Fix FileLoggerProcessorTests (#35305)
* Fix FileLoggerProcessorTests * Fixup * Whoops * Fixup * Feedback
1 parent 4d576fb commit 0281253

File tree

1 file changed

+51
-31
lines changed

1 file changed

+51
-31
lines changed

src/Middleware/HttpLogging/test/FileLoggerProcessorTests.cs

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ namespace Microsoft.AspNetCore.HttpLogging
1919
{
2020
public class FileLoggerProcessorTests
2121
{
22+
23+
private string _messageOne = "Message one";
24+
private string _messageTwo = "Message two";
25+
private string _messageThree = "Message three";
26+
2227
public FileLoggerProcessorTests()
2328
{
2429
TempPath = Path.GetTempFileName() + "_";
@@ -41,12 +46,12 @@ public async Task WritesToTextFile()
4146
};
4247
await using (var logger = new FileLoggerProcessor(new OptionsWrapperMonitor<W3CLoggerOptions>(options), new HostingEnvironment(), NullLoggerFactory.Instance))
4348
{
44-
logger.EnqueueMessage("Message one");
49+
logger.EnqueueMessage(_messageOne);
4550
fileName = Path.Combine(path, FormattableString.Invariant($"{options.FileName}{now.Year:0000}{now.Month:00}{now.Day:00}.0000.txt"));
4651
// Pause for a bit before disposing so logger can finish logging
4752
try
4853
{
49-
await WaitForFile(fileName).DefaultTimeout();
54+
await WaitForFile(fileName, _messageOne.Length).DefaultTimeout();
5055
}
5156
catch
5257
{
@@ -60,7 +65,7 @@ public async Task WritesToTextFile()
6065
}
6166
Assert.True(File.Exists(fileName));
6267

63-
Assert.Equal("Message one" + Environment.NewLine, File.ReadAllText(fileName));
68+
Assert.Equal(_messageOne + Environment.NewLine, File.ReadAllText(fileName));
6469
}
6570
finally
6671
{
@@ -86,14 +91,14 @@ public async Task RollsTextFiles()
8691
};
8792
await using (var logger = new FileLoggerProcessor(new OptionsWrapperMonitor<W3CLoggerOptions>(options), new HostingEnvironment(), NullLoggerFactory.Instance))
8893
{
89-
logger.EnqueueMessage("Message one");
90-
logger.EnqueueMessage("Message two");
94+
logger.EnqueueMessage(_messageOne);
95+
logger.EnqueueMessage(_messageTwo);
9196
fileName1 = Path.Combine(path, FormattableString.Invariant($"{options.FileName}{now.Year:0000}{now.Month:00}{now.Day:00}.0000.txt"));
9297
fileName2 = Path.Combine(path, FormattableString.Invariant($"{options.FileName}{now.Year:0000}{now.Month:00}{now.Day:00}.0001.txt"));
9398
// Pause for a bit before disposing so logger can finish logging
9499
try
95100
{
96-
await WaitForFile(fileName2).DefaultTimeout();
101+
await WaitForFile(fileName2, _messageTwo.Length).DefaultTimeout();
97102
}
98103
catch
99104
{
@@ -114,8 +119,8 @@ public async Task RollsTextFiles()
114119
Assert.True(File.Exists(fileName1));
115120
Assert.True(File.Exists(fileName2));
116121

117-
Assert.Equal("Message one" + Environment.NewLine, File.ReadAllText(fileName1));
118-
Assert.Equal("Message two" + Environment.NewLine, File.ReadAllText(fileName2));
122+
Assert.Equal(_messageOne + Environment.NewLine, File.ReadAllText(fileName1));
123+
Assert.Equal(_messageTwo + Environment.NewLine, File.ReadAllText(fileName2));
119124
}
120125
finally
121126
{
@@ -145,13 +150,13 @@ public async Task RespectsMaxFileCount()
145150
{
146151
for (int i = 0; i < 10; i++)
147152
{
148-
logger.EnqueueMessage("Message");
153+
logger.EnqueueMessage(_messageOne);
149154
}
150155
lastFileName = Path.Combine(path, FormattableString.Invariant($"{options.FileName}{now.Year:0000}{now.Month:00}{now.Day:00}.0009.txt"));
151156
// Pause for a bit before disposing so logger can finish logging
152157
try
153158
{
154-
await WaitForFile(lastFileName).DefaultTimeout();
159+
await WaitForFile(lastFileName, _messageOne.Length).DefaultTimeout();
155160
for (int i = 0; i < 6; i++)
156161
{
157162
await WaitForRoll(Path.Combine(path, FormattableString.Invariant($"{options.FileName}{now.Year:0000}{now.Month:00}{now.Day:00}.{i:0000}.txt"))).DefaultTimeout();
@@ -211,23 +216,23 @@ public async Task InstancesWriteToSameDirectory()
211216
{
212217
for (int i = 0; i < 3; i++)
213218
{
214-
logger.EnqueueMessage("Message");
219+
logger.EnqueueMessage(_messageOne);
215220
}
216221
var filePath = Path.Combine(path, FormattableString.Invariant($"{options.FileName}{now.Year:0000}{now.Month:00}{now.Day:00}.0002.txt"));
217222
// Pause for a bit before disposing so logger can finish logging
218-
await WaitForFile(filePath).DefaultTimeout();
223+
await WaitForFile(filePath, _messageOne.Length).DefaultTimeout();
219224
}
220225

221226
// Second instance should pick up where first one left off
222227
await using (var logger = new FileLoggerProcessor(new OptionsWrapperMonitor<W3CLoggerOptions>(options), new HostingEnvironment(), NullLoggerFactory.Instance))
223228
{
224229
for (int i = 0; i < 3; i++)
225230
{
226-
logger.EnqueueMessage("Message");
231+
logger.EnqueueMessage(_messageOne);
227232
}
228233
var filePath = Path.Combine(path, FormattableString.Invariant($"{options.FileName}{now.Year:0000}{now.Month:00}{now.Day:00}.0005.txt"));
229234
// Pause for a bit before disposing so logger can finish logging
230-
await WaitForFile(filePath).DefaultTimeout();
235+
await WaitForFile(filePath, _messageOne.Length).DefaultTimeout();
231236
}
232237

233238
var actualFiles1 = new DirectoryInfo(path)
@@ -246,9 +251,9 @@ public async Task InstancesWriteToSameDirectory()
246251
options.RetainedFileCountLimit = 5;
247252
await using (var logger = new FileLoggerProcessor(new OptionsWrapperMonitor<W3CLoggerOptions>(options), new HostingEnvironment(), NullLoggerFactory.Instance))
248253
{
249-
logger.EnqueueMessage("Message");
254+
logger.EnqueueMessage(_messageOne);
250255
// Pause for a bit before disposing so logger can finish logging
251-
await WaitForFile(Path.Combine(path, FormattableString.Invariant($"{options.FileName}{now.Year:0000}{now.Month:00}{now.Day:00}.0006.txt"))).DefaultTimeout();
256+
await WaitForFile(Path.Combine(path, FormattableString.Invariant($"{options.FileName}{now.Year:0000}{now.Month:00}{now.Day:00}.0006.txt")), _messageOne.Length).DefaultTimeout();
252257
await WaitForRoll(Path.Combine(path, FormattableString.Invariant($"{options.FileName}{now.Year:0000}{now.Month:00}{now.Day:00}.0000.txt"))).DefaultTimeout();
253258
await WaitForRoll(Path.Combine(path, FormattableString.Invariant($"{options.FileName}{now.Year:0000}{now.Month:00}{now.Day:00}.0001.txt"))).DefaultTimeout();
254259
}
@@ -298,20 +303,20 @@ public async Task WritesToNewFileOnNewInstance()
298303

299304
await using (var logger = new FileLoggerProcessor(new OptionsWrapperMonitor<W3CLoggerOptions>(options), new HostingEnvironment(), NullLoggerFactory.Instance))
300305
{
301-
logger.EnqueueMessage("Message one");
302-
logger.EnqueueMessage("Message two");
306+
logger.EnqueueMessage(_messageOne);
307+
logger.EnqueueMessage(_messageTwo);
303308
// Pause for a bit before disposing so logger can finish logging
304-
await WaitForFile(fileName2).DefaultTimeout();
309+
await WaitForFile(fileName2, _messageTwo.Length).DefaultTimeout();
305310
}
306311

307312
// Even with a big enough FileSizeLimit, we still won't try to write to files from a previous instance.
308313
options.FileSizeLimit = 10000;
309314

310315
await using (var logger = new FileLoggerProcessor(new OptionsWrapperMonitor<W3CLoggerOptions>(options), new HostingEnvironment(), NullLoggerFactory.Instance))
311316
{
312-
logger.EnqueueMessage("Message three");
317+
logger.EnqueueMessage(_messageThree);
313318
// Pause for a bit before disposing so logger can finish logging
314-
await WaitForFile(fileName3).DefaultTimeout();
319+
await WaitForFile(fileName3, _messageThree.Length).DefaultTimeout();
315320
}
316321

317322
var actualFiles = new DirectoryInfo(path)
@@ -326,9 +331,9 @@ public async Task WritesToNewFileOnNewInstance()
326331
Assert.True(File.Exists(fileName2));
327332
Assert.True(File.Exists(fileName3));
328333

329-
Assert.Equal("Message one" + Environment.NewLine, File.ReadAllText(fileName1));
330-
Assert.Equal("Message two" + Environment.NewLine, File.ReadAllText(fileName2));
331-
Assert.Equal("Message three" + Environment.NewLine, File.ReadAllText(fileName3));
334+
Assert.Equal(_messageOne + Environment.NewLine, File.ReadAllText(fileName1));
335+
Assert.Equal(_messageTwo + Environment.NewLine, File.ReadAllText(fileName2));
336+
Assert.Equal(_messageThree + Environment.NewLine, File.ReadAllText(fileName3));
332337
}
333338
finally
334339
{
@@ -364,13 +369,13 @@ public async Task WritesToNewFileOnOptionsChange()
364369

365370
await using (var logger = new FileLoggerProcessor(monitor, new HostingEnvironment(), NullLoggerFactory.Instance))
366371
{
367-
logger.EnqueueMessage("Message one");
368-
await WaitForFile(fileName1).DefaultTimeout();
372+
logger.EnqueueMessage(_messageOne);
373+
await WaitForFile(fileName1, _messageOne.Length).DefaultTimeout();
369374
options.LoggingFields = W3CLoggingFields.Date;
370375
monitor.InvokeChanged();
371-
logger.EnqueueMessage("Message two");
376+
logger.EnqueueMessage(_messageTwo);
372377
// Pause for a bit before disposing so logger can finish logging
373-
await WaitForFile(fileName2).DefaultTimeout();
378+
await WaitForFile(fileName2, _messageTwo.Length).DefaultTimeout();
374379
}
375380

376381
var actualFiles = new DirectoryInfo(path)
@@ -384,21 +389,36 @@ public async Task WritesToNewFileOnOptionsChange()
384389
Assert.True(File.Exists(fileName1));
385390
Assert.True(File.Exists(fileName2));
386391

387-
Assert.Equal("Message one" + Environment.NewLine, File.ReadAllText(fileName1));
388-
Assert.Equal("Message two" + Environment.NewLine, File.ReadAllText(fileName2));
392+
Assert.Equal(_messageOne + Environment.NewLine, File.ReadAllText(fileName1));
393+
Assert.Equal(_messageTwo + Environment.NewLine, File.ReadAllText(fileName2));
389394
}
390395
finally
391396
{
392397
Helpers.DisposeDirectory(path);
393398
}
394399
}
395400

396-
private async Task WaitForFile(string fileName)
401+
private async Task WaitForFile(string fileName, int length)
397402
{
398403
while (!File.Exists(fileName))
399404
{
400405
await Task.Delay(100);
401406
}
407+
while (true)
408+
{
409+
try
410+
{
411+
if (File.ReadAllText(fileName).Length >= length)
412+
{
413+
break;
414+
}
415+
}
416+
catch
417+
{
418+
// Continue
419+
}
420+
await Task.Delay(10);
421+
}
402422
}
403423

404424
private async Task WaitForRoll(string fileName)

0 commit comments

Comments
 (0)