Skip to content

Fix W3CLogger tests - #40922

Merged
wtgodbe merged 9 commits into
dotnet:mainfrom
wtgodbe:wtgodbe/TestFix
Mar 29, 2022
Merged

wtgodbe merged 9 commits into
dotnet:mainfrom
wtgodbe:wtgodbe/TestFix

Conversation

@wtgodbe

@wtgodbe wtgodbe commented Mar 28, 2022

Copy link
Copy Markdown
Member

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.

@wtgodbe
wtgodbe enabled auto-merge (squash) March 28, 2022 21:07
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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these be startDate.Subtract(now)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@BrennanConroy BrennanConroy Mar 29, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, duh 🤦‍♂️ - fixing now

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@wtgodbe
wtgodbe merged commit 63cd2d4 into dotnet:main Mar 29, 2022
@ghost ghost added this to the 7.0-preview3 milestone Mar 29, 2022
@dougbu dougbu modified the milestones: 7.0-preview3, 7.0-preview4 Apr 5, 2022
@amcasey amcasey added area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares and removed area-runtime labels Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants