-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathRetryLifecycleCallbacks.cs
More file actions
72 lines (54 loc) · 3.33 KB
/
RetryLifecycleCallbacks.cs
File metadata and controls
72 lines (54 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under dual-license. See LICENSE.PLATFORMTOOLS.txt file in the project root for full license information.
using Microsoft.Testing.Extensions.Policy.Resources;
using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Extensions.RetryFailedTests.Serializers;
using Microsoft.Testing.Platform.Extensions.TestHost;
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.IPC;
using Microsoft.Testing.Platform.IPC.Models;
using Microsoft.Testing.Platform.IPC.Serializers;
using Microsoft.Testing.Platform.Logging;
using Microsoft.Testing.Platform.Services;
namespace Microsoft.Testing.Extensions.Policy;
[UnsupportedOSPlatform("browser")]
internal sealed class RetryLifecycleCallbacks : ITestHostApplicationLifetime, IDisposable
{
private readonly IServiceProvider _serviceProvider;
private readonly ICommandLineOptions _commandLineOptions;
public RetryLifecycleCallbacks(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
_commandLineOptions = _serviceProvider.GetCommandLineOptions();
}
public NamedPipeClient? Client { get; private set; }
public string[]? FailedTestsIDToRetry { get; private set; }
public string Uid => nameof(RetryLifecycleCallbacks);
public string Version => AppVersion.DefaultSemVer;
public string DisplayName => ExtensionResources.RetryFailedTestsExtensionDisplayName;
public string Description => ExtensionResources.RetryFailedTestsExtensionDescription;
public async Task BeforeRunAsync(CancellationToken cancellationToken)
{
if (!_commandLineOptions.TryGetOptionArgumentList(RetryCommandLineOptionsProvider.RetryFailedTestsPipeNameOptionName, out string[]? pipeName))
{
throw ApplicationStateGuard.Unreachable();
}
ILogger<RetryLifecycleCallbacks> logger = _serviceProvider.GetLoggerFactory().CreateLogger<RetryLifecycleCallbacks>();
ArgumentGuard.Ensure(pipeName.Length == 1, nameof(pipeName), "Pipe name expected");
logger.LogDebug($"Connecting to pipe '{pipeName[0]}'");
Client = new(pipeName[0]);
Client.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse));
Client.RegisterSerializer(new FailedTestRequestSerializer(), typeof(FailedTestRequest));
Client.RegisterSerializer(new GetListOfFailedTestsRequestSerializer(), typeof(GetListOfFailedTestsRequest));
Client.RegisterSerializer(new GetListOfFailedTestsResponseSerializer(), typeof(GetListOfFailedTestsResponse));
Client.RegisterSerializer(new TotalTestsRunRequestSerializer(), typeof(TotalTestsRunRequest));
await Client.ConnectAsync(cancellationToken).ConfigureAwait(false);
GetListOfFailedTestsResponse result = await Client.RequestReplyAsync<GetListOfFailedTestsRequest, GetListOfFailedTestsResponse>(new GetListOfFailedTestsRequest(), cancellationToken).ConfigureAwait(false);
FailedTestsIDToRetry = result.FailedTestIds;
}
public Task<bool> IsEnabledAsync()
=> Task.FromResult(_commandLineOptions.IsOptionSet(RetryCommandLineOptionsProvider.RetryFailedTestsPipeNameOptionName));
public Task AfterRunAsync(int exitCode, CancellationToken cancellationToken)
=> Task.CompletedTask;
public void Dispose() => Client?.Dispose();
}