Skip to content

Commit 13d3954

Browse files
committed
Sets start dates, trying to get durations to show correctly
1 parent 0fd432e commit 13d3954

4 files changed

Lines changed: 32 additions & 10 deletions

File tree

ReleaseNotes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.4.6
2+
3+
- [Feature] Sets start dates
4+
15
# 0.4.5
26

37
- [Fix] Fix for API version when patching a test run as completed

src/AzurePipelines.TestLogger/LoggerQueue.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ internal class LoggerQueue
1818
private readonly Task _consumeTask;
1919
private readonly CancellationTokenSource _consumeTaskCancellationSource = new CancellationTokenSource();
2020

21-
2221
private readonly IApiClient _apiClient;
2322
private readonly string _buildId;
2423
private readonly string _agentName;
2524
private readonly string _jobName;
2625

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

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

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

297301
// Mark the overall test run as completed
298302
string testRunRequest = $@"{{
299303
""state"": ""Completed"",
300-
""completedDate"": ""{ DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") }""
304+
""completedDate"": ""{ completedDate }""
301305
}}";
302306
await _apiClient.SendAsync(new HttpMethod("PATCH"), $"/{RunId}", "5.0-preview.2", testRunRequest, cancellationToken);
303307
}
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
namespace AzurePipelines.TestLogger
1+
using System;
2+
3+
namespace AzurePipelines.TestLogger
24
{
35
internal class TestResultParent
46
{
57
public int Id { get; }
68

79
public long Duration { get; set; }
810

11+
public DateTime StartedDate { get; }
12+
913
public TestResultParent(int id)
14+
: this(id, DateTime.UtcNow)
15+
{
16+
}
17+
18+
public TestResultParent(int id, DateTime startedDate)
1019
{
1120
Id = id;
21+
StartedDate = startedDate;
1222
}
1323
}
1424
}

tests/AzurePipelines.TestLogger.Tests/LoggerQueueTests.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void CreateTestRunWithoutFilename()
3434
$@"{{
3535
""name"": ""Unknown Test Source (OS: { System.Runtime.InteropServices.RuntimeInformation.OSDescription }, Job: bar, Agent: foo)"",
3636
""build"": {{""id"":""987""}},
37+
""startedDate"": ""{ loggerQueue.StartedDate.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") }"",
3738
""isAutomated"": true
3839
}}")
3940
});
@@ -63,6 +64,7 @@ public void CreateTestRunWithFilename()
6364
$@"{{
6465
""name"": ""Fizz.Buzz (OS: { System.Runtime.InteropServices.RuntimeInformation.OSDescription }, Job: bar, Agent: foo)"",
6566
""build"": {{""id"":""987""}},
67+
""startedDate"": ""{ loggerQueue.StartedDate.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") }"",
6668
""isAutomated"": true
6769
}}")
6870
});
@@ -205,27 +207,29 @@ public void CreateParents()
205207
HttpMethod.Post,
206208
"/ep",
207209
"5.0-preview.5",
208-
@"[
209-
{
210+
$@"[
211+
{{
210212
""testCaseTitle"": ""FooFixture"",
211213
""automatedTestName"": ""FooFixture"",
212214
""resultGroupType"": ""generic"",
213215
""outcome"": ""Passed"",
214216
""state"": ""InProgress"",
217+
""startedDate"": ""{ loggerQueue.Parents["FooFixture"].StartedDate.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") }"",
215218
""automatedTestType"": ""UnitTest"",
216219
""automatedTestTypeId"": ""13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b"",
217220
""automatedTestStorage"": ""Fizz.Buzz""
218-
},
219-
{
221+
}},
222+
{{
220223
""testCaseTitle"": ""FutzFixture.NestedFixture"",
221224
""automatedTestName"": ""FutzFixture.NestedFixture"",
222225
""resultGroupType"": ""generic"",
223226
""outcome"": ""Passed"",
224227
""state"": ""InProgress"",
228+
""startedDate"": ""{ loggerQueue.Parents["FutzFixture.NestedFixture"].StartedDate.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") }"",
225229
""automatedTestType"": ""UnitTest"",
226230
""automatedTestTypeId"": ""13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b"",
227231
""automatedTestStorage"": ""Fizz.Buzz""
228-
}
232+
}}
229233
]")
230234
});
231235
loggerQueue.Parents.Keys.ShouldBe(new[] { "FitzFixture", "FooFixture", "FutzFixture.NestedFixture" }, true);

0 commit comments

Comments
 (0)