Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate DateTime.Now to DateTime.UtcNow to make sure all test reporti… #1612

Merged
merged 5 commits into from
May 24, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ internal string WriteEventLogs(List<EventLogEntry> eventLogEntries, int maxLogEn
"{0}-{1}-{2:yyyy}{2:MM}{2:dd}-{2:HH}{2:mm}{2:ss}.{2:fff}",
"Event Log",
Environment.MachineName,
DateTime.Now);
DateTime.UtcNow);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add tests for these?

Copy link
Author

Choose a reason for hiding this comment

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

done


string eventLogDirPath = Path.Combine(Path.GetTempPath(), eventLogDirName);

Expand Down Expand Up @@ -413,7 +413,7 @@ private void OnSessionEnd(object sender, SessionEndEventArgs e)
EqtTrace.Verbose("EventLogDataCollector: SessionEnd received");
}

this.WriteCollectedEventLogEntries(e.Context, true, TimeSpan.MaxValue, DateTime.Now);
this.WriteCollectedEventLogEntries(e.Context, true, TimeSpan.MaxValue, DateTime.UtcNow);
}

private void OnTestCaseStart(object sender, TestCaseStartEventArgs e)
Expand Down Expand Up @@ -450,7 +450,7 @@ private void OnTestCaseEnd(object sender, TestCaseEndEventArgs e)
e.TestOutcome);
}

this.WriteCollectedEventLogEntries(e.Context, false, TimeSpan.MaxValue, DateTime.Now);
this.WriteCollectedEventLogEntries(e.Context, false, TimeSpan.MaxValue, DateTime.UtcNow);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public RunInfo(string textMessage, Exception ex, string computer, TestOutcome ou
this.exception = ex;
this.computer = computer;
this.outcome = outcome;
this.timestamp = DateTime.Now.ToUniversalTime();
this.timestamp = DateTime.UtcNow;
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ private void Initialize()
{
this.runUser = string.Empty;
}
this.created = DateTime.Now.ToUniversalTime();
this.queued = DateTime.Now.ToUniversalTime();
this.started = DateTime.Now.ToUniversalTime();
this.finished = DateTime.Now.ToUniversalTime();
this.created = DateTime.UtcNow;
this.queued = DateTime.UtcNow;
this.started = DateTime.UtcNow;
this.finished = DateTime.UtcNow;
}
}
}
8 changes: 4 additions & 4 deletions src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public void TestRunCompleteHandler(object sender, TestRunCompleteEventArgs e)
XmlElement rootElement = helper.CreateRootElement("TestRun");

// Save runId/username/creation time etc.
this.testRun.Finished = DateTime.Now;
this.testRun.Finished = DateTime.UtcNow;
helper.SaveSingleFields(rootElement, this.testRun, parameters);

// Save test settings
Expand Down Expand Up @@ -410,7 +410,7 @@ private void InitializeInternal()
this.passTests = 0;
this.failTests = 0;
this.runLevelStdOut = new StringBuilder();
this.testRunStartTime = DateTime.Now;
this.testRunStartTime = DateTime.UtcNow;
}

/// <summary>
Expand Down Expand Up @@ -589,7 +589,7 @@ private void UpdateTestLinks(ITestElement testElement, ITestElement parentTestEl
/// <param name="parentTestResult"></param>
/// <param name="rocksteadyTestResult"></param>
/// <returns>Trx test result</returns>
private ITestResult CreateTestResult(Guid executionId, Guid parentExecutionId, TestType testType,
private ITestResult CreateTestResult(Guid executionId, Guid parentExecutionId, TestType testType,
ITestElement testElement, ITestElement parentTestElement, ITestResult parentTestResult, ObjectModel.TestResult rocksteadyTestResult)
{
// Create test result
Expand All @@ -611,7 +611,7 @@ private ITestResult CreateTestResult(Guid executionId, Guid parentExecutionId, T
this.innerResults.TryAdd(executionId, testResult);
return testResult;
}

// Data driven inner result scenario
if (parentTestElement != null && parentTestElement.TestType.Equals(TrxLoggerConstants.UnitTestType))
{
Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.TestPlatform.ObjectModel/TestResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public TestResult(TestCase testCase)

// Default start and end time values for a test result are initialized to current timestamp
// to maintain compatibility.
this.StartTime = DateTimeOffset.Now;
this.EndTime = DateTimeOffset.Now;
this.StartTime = DateTimeOffset.UtcNow;
Copy link
Contributor

Choose a reason for hiding this comment

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

This could change the behavior for inner loop scenarios. If any client displaying time it's client responsibility to localize the date. TestPlatform will use UTC for consistence.

Copy link
Author

Choose a reason for hiding this comment

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

As we discussed, it might be. But we need to draw this and follow everywhere as UTC standard.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, Totally agreed.

this.EndTime = DateTimeOffset.UtcNow;
}

#endregion
Expand Down Expand Up @@ -255,10 +255,10 @@ protected override void ProtectedSetPropertyValue(TestProperty property, object
public class TestResultMessage
{
// Bugfix: 297759 Moving the category from the resources to the code
// so that it works on machines which has eng OS & non-eng VS and vice versa.
// so that it works on machines which has eng OS & non-eng VS and vice versa.

/// <summary>
/// Standard Output Message Category
/// Standard Output Message Category
/// </summary>
public static readonly string StandardOutCategory = "StdOutMsgs";

Expand All @@ -278,7 +278,7 @@ public class TestResultMessage
public static readonly string AdditionalInfoCategory = "AdtnlInfo";

/// <summary>
/// Initializes a new instance of the <see cref="TestResultMessage"/> class.
/// Initializes a new instance of the <see cref="TestResultMessage"/> class.
/// </summary>
/// <param name="category">Category of the message.</param>
/// <param name="text">Text of the message.</param>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Xml.Linq;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: This should go under namespace.

Copy link
Author

Choose a reason for hiding this comment

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

done


namespace Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests
{
using System;
Expand Down Expand Up @@ -692,6 +694,35 @@ public void CRLFCharactersShouldGetRetainedInTrx()
Assert.IsTrue(string.Equals(message, actualMessage), string.Format("StdOut messages do not match. Expected:{0}, Actual:{1}", message, actualMessage));
}

[TestMethod]
public void TestRunInformationShouldContainUtcDateTime()
{
this.MakeTestRunComplete();
this.ValidateDateTimeInTrx(this.testableTrxLogger.trxFile);
}

private void ValidateDateTimeInTrx(string trxFileName)
{
using (FileStream file = File.OpenRead(trxFileName))
{
using (XmlReader reader = XmlReader.Create(file))
{
XDocument document = XDocument.Load(reader);
var timesNode = document.Descendants(document.Root.GetDefaultNamespace() + "Times").FirstOrDefault();
ValidateTimeWithinUtcLimits(DateTimeOffset.Parse(timesNode.Attributes("creation").FirstOrDefault().Value));
ValidateTimeWithinUtcLimits(DateTimeOffset.Parse(timesNode.Attributes("start").FirstOrDefault().Value));
var resultNode = document.Descendants(document.Root.GetDefaultNamespace() + "UnitTestResult").FirstOrDefault();
ValidateTimeWithinUtcLimits(DateTimeOffset.Parse(resultNode.Attributes("endTime").FirstOrDefault().Value));
ValidateTimeWithinUtcLimits(DateTimeOffset.Parse(resultNode.Attributes("startTime").FirstOrDefault().Value));
}
}
}

private void ValidateTimeWithinUtcLimits(DateTimeOffset dateTime)
{
Assert.IsTrue(dateTime.UtcDateTime.Subtract(DateTime.UtcNow) < new TimeSpan(0, 0, 0, 60));
}

private string GetElementValueFromTrx(string trxFileName, string fieldName)
{
using (FileStream file = File.OpenRead(trxFileName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public void TestResultShouldInitializeEmptyMessages()
[TestMethod]
public void TestResultShouldInitializeStartAndEndTimeToCurrent()
{
Assert.IsTrue(this.result.StartTime.Subtract(DateTimeOffset.Now) < new TimeSpan(0, 0, 0, 10));
Assert.IsTrue(this.result.EndTime.Subtract(DateTimeOffset.Now) < new TimeSpan(0, 0, 0, 10));
Assert.IsTrue(this.result.StartTime.Subtract(DateTimeOffset.UtcNow) < new TimeSpan(0, 0, 0, 10));
Assert.IsTrue(this.result.EndTime.Subtract(DateTimeOffset.UtcNow) < new TimeSpan(0, 0, 0, 10));
}

#region GetSetPropertyValue Tests
Expand Down