Repository navigation
Fix W3CLogger tests - #40922
Fix W3CLogger tests#40922
Conversation
| var startDate = DateTime.Parse(lines[1].Substring(13), CultureInfo.InvariantCulture); | ||
| // Assert that the log was written in the last 10 seconds | ||
| Assert.True(now.Subtract(startDate).TotalSeconds < 10); | ||
| var delta = now.Subtract(startDate).TotalSeconds; |
There was a problem hiding this comment.
Shouldn't these be startDate.Subtract(now)?
There was a problem hiding this comment.
Ah, yeah, it should - the tests are also failing inconsistently, going to try changing DateTimeOffset to DateTime in W3CLoggerProcessor (don't my remember why I used the former instead of the latter in the first place)
There was a problem hiding this comment.
Yeah, looks like the result varies from around -1 second to 1 second (seen that on both Windows & non-Windows), despite now definitely being set before startDate - it seems like a known issue that DateTime.Now/DateTime.UtcNow aren't 100% precise, but I'm seeing results of up to -.8 seconds, which seems like a pretty large inaccuracy. @stephentoub @tannergooding do you guys know if that's to be expected? The use case here is we have a logger that writes the current time to a file, then have tests that compare that time to DateTime.UtcNow to make sure the delta is between 0 and 10 seconds, but sometimes the delta is negative.
There was a problem hiding this comment.
The issue is that you're writing the date+time without milliseconds but then comparing with DateTime.UtcNow which has milliseconds, so you'll be up to .99999 seconds off when comparing.
There was a problem hiding this comment.
Ah, duh 🤦♂️ - fixing now
There was a problem hiding this comment.
Also noting that Windows generally only guarantees accuracy up to 16ms for these APIs, so you generally shouldn't rely on any accuracy greater than 0.016 seconds (although 0.8 is of course much greater than that).
Discovered as part of #40844 - the tests just check if the time delta is less than 10 seconds, which means they still passed when the number was negative. That explains why they failed on some machines and not others before we unified on using
UTCNow- they'd fail in time zones that were ahead of UTC.