Skip to content

Commit

Permalink
Sets start dates, trying to get durations to show correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
daveaglick committed Dec 14, 2018
1 parent 0fd432e commit 13d3954
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
4 changes: 4 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.4.6

- [Feature] Sets start dates

# 0.4.5

- [Fix] Fix for API version when patching a test run as completed
Expand Down
12 changes: 8 additions & 4 deletions src/AzurePipelines.TestLogger/LoggerQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ internal class LoggerQueue
private readonly Task _consumeTask;
private readonly CancellationTokenSource _consumeTaskCancellationSource = new CancellationTokenSource();


private readonly IApiClient _apiClient;
private readonly string _buildId;
private readonly string _agentName;
private readonly string _jobName;

// Internal for testing
internal Dictionary<string, TestResultParent> Parents { get; } = new Dictionary<string, TestResultParent>();
internal DateTime StartedDate { get; } = DateTime.UtcNow;
internal int RunId { get; set; }
internal string Source { get; set; }
internal string TestRunEndpoint { get; set; }
Expand Down Expand Up @@ -139,6 +139,7 @@ internal async Task<int> CreateTestRun(CancellationToken cancellationToken)
{
{ "name", runName },
{ "build", new Dictionary<string, object> { { "id", _buildId } } },
{ "startedDate", StartedDate.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") },
{ "isAutomated", true }
};
string responseString = await _apiClient.SendAsync(HttpMethod.Post, null, "5.0-preview.2", request.ToJson(), cancellationToken);
Expand Down Expand Up @@ -179,6 +180,7 @@ internal async Task CreateParents(IEnumerable<IGrouping<string, ITestResult>> te
.ToArray();

// Batch an add operation and record the new parent IDs
DateTime startedDate = DateTime.UtcNow;
if (parentsToAdd.Length > 0)
{
string request = "[ " + string.Join(", ", parentsToAdd.Select(x =>
Expand All @@ -190,6 +192,7 @@ internal async Task CreateParents(IEnumerable<IGrouping<string, ITestResult>> te
{ "resultGroupType", "generic" },
{ "outcome", "Passed" }, // Start with a passed outcome initially
{ "state", "InProgress" },
{ "startedDate", startedDate.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") },
{ "automatedTestType", "UnitTest" },
{ "automatedTestTypeId", "13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" } // This is used in the sample response and also appears in web searches
};
Expand All @@ -211,7 +214,7 @@ internal async Task CreateParents(IEnumerable<IGrouping<string, ITestResult>> te
for (int c = 0; c < parents.Length; c++)
{
int id = ((JsonObject)parents[c]).ValueAsInt("id");
Parents.Add(parentsToAdd[c], new TestResultParent(id));
Parents.Add(parentsToAdd[c], new TestResultParent(id, startedDate));
}
}
}
Expand Down Expand Up @@ -285,19 +288,20 @@ private string GetTestResultJson(ITestResult testResult)

private async Task SendTestsCompleted(CancellationToken cancellationToken)
{
string completedDate = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
// Mark all parents as completed
string parentRequest = "[ " + string.Join(", ", Parents.Values.Select(x =>
$@"{{
""id"": { x.Id },
""state"": ""Completed"",
""completedDate"": ""{ DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") }""
""completedDate"": ""{ completedDate }""
}}")) + " ]";
await _apiClient.SendAsync(new HttpMethod("PATCH"), TestRunEndpoint, "5.0-preview.5", parentRequest, cancellationToken);

// Mark the overall test run as completed
string testRunRequest = $@"{{
""state"": ""Completed"",
""completedDate"": ""{ DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") }""
""completedDate"": ""{ completedDate }""
}}";
await _apiClient.SendAsync(new HttpMethod("PATCH"), $"/{RunId}", "5.0-preview.2", testRunRequest, cancellationToken);
}
Expand Down
12 changes: 11 additions & 1 deletion src/AzurePipelines.TestLogger/TestResultParent.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
namespace AzurePipelines.TestLogger
using System;

namespace AzurePipelines.TestLogger
{
internal class TestResultParent
{
public int Id { get; }

public long Duration { get; set; }

public DateTime StartedDate { get; }

public TestResultParent(int id)
: this(id, DateTime.UtcNow)
{
}

public TestResultParent(int id, DateTime startedDate)
{
Id = id;
StartedDate = startedDate;
}
}
}
14 changes: 9 additions & 5 deletions tests/AzurePipelines.TestLogger.Tests/LoggerQueueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void CreateTestRunWithoutFilename()
$@"{{
""name"": ""Unknown Test Source (OS: { System.Runtime.InteropServices.RuntimeInformation.OSDescription }, Job: bar, Agent: foo)"",
""build"": {{""id"":""987""}},
""startedDate"": ""{ loggerQueue.StartedDate.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") }"",
""isAutomated"": true
}}")
});
Expand Down Expand Up @@ -63,6 +64,7 @@ public void CreateTestRunWithFilename()
$@"{{
""name"": ""Fizz.Buzz (OS: { System.Runtime.InteropServices.RuntimeInformation.OSDescription }, Job: bar, Agent: foo)"",
""build"": {{""id"":""987""}},
""startedDate"": ""{ loggerQueue.StartedDate.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") }"",
""isAutomated"": true
}}")
});
Expand Down Expand Up @@ -205,27 +207,29 @@ public void CreateParents()
HttpMethod.Post,
"/ep",
"5.0-preview.5",
@"[
{
$@"[
{{
""testCaseTitle"": ""FooFixture"",
""automatedTestName"": ""FooFixture"",
""resultGroupType"": ""generic"",
""outcome"": ""Passed"",
""state"": ""InProgress"",
""startedDate"": ""{ loggerQueue.Parents["FooFixture"].StartedDate.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") }"",
""automatedTestType"": ""UnitTest"",
""automatedTestTypeId"": ""13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b"",
""automatedTestStorage"": ""Fizz.Buzz""
},
{
}},
{{
""testCaseTitle"": ""FutzFixture.NestedFixture"",
""automatedTestName"": ""FutzFixture.NestedFixture"",
""resultGroupType"": ""generic"",
""outcome"": ""Passed"",
""state"": ""InProgress"",
""startedDate"": ""{ loggerQueue.Parents["FutzFixture.NestedFixture"].StartedDate.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") }"",
""automatedTestType"": ""UnitTest"",
""automatedTestTypeId"": ""13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b"",
""automatedTestStorage"": ""Fizz.Buzz""
}
}}
]")
});
loggerQueue.Parents.Keys.ShouldBe(new[] { "FitzFixture", "FooFixture", "FutzFixture.NestedFixture" }, true);
Expand Down

0 comments on commit 13d3954

Please sign in to comment.