Skip to content

Commit fc107b3

Browse files
Update tests
1 parent fffeb5e commit fc107b3

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/.idea/.idea.Stravaig.Extensions.Logging.Diagnostics/.idea/projectSettingsUpdater.xml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Stravaig.Extensions.Logging.Diagnostics.Tests/TestsUsingTheLoggerDirectly.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void InformationMessageIsCapturedAndReadable()
2121
// Arrange
2222
var logger = new TestCaptureLogger();
2323
const string message = "This is a test log message.";
24-
24+
2525
// Act
2626
logger.LogInformation(message);
2727

@@ -34,21 +34,21 @@ public void InformationMessageIsCapturedAndReadable()
3434
entry.LogLevel.ShouldBe(LogLevel.Information);
3535
entry.FormattedMessage.ShouldContain(message);
3636
}
37-
37+
3838
[Test]
3939
public void MultipleLogsAreCapturedAndRetrievedInTheCorrectOrder()
4040
{
4141
// Arrange
4242
var logger = new TestCaptureLogger();
43-
43+
4444
// Act
4545
logger.LogTrace("This is a trace message.");
4646
logger.LogDebug("This is a debug message.");
4747
logger.LogInformation("This is an information message.");
4848
logger.LogWarning("This is a warning message.");
4949
logger.LogError("This is an error message.");
5050
logger.LogCritical("This is a critical message.");
51-
51+
5252
// Assert
5353
var logEntries = logger.GetLogs();
5454
logEntries.Count.ShouldBe(6);
@@ -65,9 +65,9 @@ public void NoLoggerCapturedReturnsEmptyList()
6565
{
6666
// Arrange
6767
var logger = new TestCaptureLogger();
68-
68+
6969
// Act - none, no logs are being created.
70-
70+
7171
// Assert
7272
var logEntries = logger.GetLogs();
7373
logEntries.Count.ShouldBe(0);
@@ -104,46 +104,53 @@ public void ManyThreadsAccessingLogger()
104104
{
105105
const int timeoutMs = 30000;
106106
const int iterationsPerThread = 25000;
107-
int numThreads = Environment.ProcessorCount;
107+
int numThreads = Environment.ProcessorCount * 4;
108108
int expectedLogCount = iterationsPerThread * numThreads;
109109
Console.WriteLine($"Performing {iterationsPerThread} iterations on each of {numThreads} threads for an expected total of {expectedLogCount} log messages.");
110110
Task[] tasks = new Task[numThreads];
111111
var logger = new TestCaptureLogger();
112112

113113
for (int taskNumber = 0; taskNumber < numThreads; taskNumber++)
114114
{
115+
int localTaskNumber = taskNumber;
115116
tasks[taskNumber] = Task.Factory.StartNew(() =>
116117
{
118+
Console.WriteLine($"Starting task {localTaskNumber} on thread {Environment.CurrentManagedThreadId}");
117119
for (int i = 0; i < iterationsPerThread; i++)
118120
{
119121
logger.LogInformation(
120122
"Log iteration {Iteration} on thread {ThreadId}",
121123
i,
122-
Thread.CurrentThread.ManagedThreadId);
124+
Environment.CurrentManagedThreadId);
123125
}
126+
Console.WriteLine($"Finished task {localTaskNumber} on thread {Environment.CurrentManagedThreadId}");
124127
});
125128
}
126129

127130
using CancellationTokenSource source = new CancellationTokenSource(timeoutMs);
128131
Task.WaitAll(tasks, source.Token);
129-
132+
130133
tasks.ShouldAllBe(t => t.IsCompleted);
131134
var logs = logger.GetLogs();
132135
logs.Count.ShouldBe(expectedLogCount);
133-
136+
134137
// Check no duplicate sequence numbers
135138
logs.Select(l => l.Sequence).Distinct().Count().ShouldBe(expectedLogCount);
136-
139+
137140
// Checks that log entries are in sequence order
138141
logs.ShouldBeInOrder(SortDirection.Ascending);
139-
142+
140143
// Check that timestamps progressed forward-only
141144
Enumerable.Range(0, logs.Count - 2)
142145
.Select( i => new
143146
{
144147
Index = i,
145148
Current = logs[i],
146-
Next = logs[i+1]
149+
CurrentSequence = logs[i].Sequence,
150+
CurrentTimestamp = logs[i].TimestampUtc.ToString("O"),
151+
Next = logs[i+1],
152+
NextSequence = logs[i+1].Sequence,
153+
NextTimestamp = logs[i+1].TimestampUtc.ToString("O"),
147154
}).ShouldAllBe(e => e.Current.TimestampUtc <= e.Next.TimestampUtc);
148155

149156
DateTime first = logs.First().TimestampUtc;
@@ -152,4 +159,4 @@ public void ManyThreadsAccessingLogger()
152159
Console.WriteLine($"Logging started at {first:HH:mm:ss.fff} and ended at {last:HH:mm:ss.fff} taking a total of {(last-first):G}");
153160
}
154161
}
155-
}
162+
}

0 commit comments

Comments
 (0)