2017-05-27 06:47:29 -07:00
|
|
|
// Copyright (c) .NET Foundation. All rights reserved.
|
|
|
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
|
|
2025-11-20 21:13:26 +10:30
|
|
|
#nullable disable
|
|
|
|
|
|
2017-05-27 06:47:29 -07:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using NuGet.Protocol.Plugins;
|
2019-07-01 15:57:59 -07:00
|
|
|
using NuGet.Versioning;
|
2017-05-27 06:47:29 -07:00
|
|
|
|
|
|
|
|
namespace NuGet.Test.TestExtensions.TestablePlugin
|
|
|
|
|
{
|
|
|
|
|
internal sealed class TestablePlugin : IRequestHandler, IDisposable
|
|
|
|
|
{
|
|
|
|
|
private CancellationTokenSource _cancellationTokenSource;
|
|
|
|
|
private bool _isDisposed;
|
|
|
|
|
private IPlugin _plugin;
|
|
|
|
|
private readonly BlockingCollection<Response> _responses;
|
|
|
|
|
|
|
|
|
|
public CancellationToken CancellationToken { get; private set; }
|
|
|
|
|
|
|
|
|
|
internal TestablePlugin(BlockingCollection<Response> responses)
|
|
|
|
|
{
|
|
|
|
|
_responses = responses;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (!_isDisposed)
|
|
|
|
|
{
|
|
|
|
|
if (_plugin != null)
|
|
|
|
|
{
|
|
|
|
|
_plugin.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_cancellationTokenSource != null)
|
|
|
|
|
{
|
|
|
|
|
using (_cancellationTokenSource)
|
|
|
|
|
{
|
|
|
|
|
_cancellationTokenSource.Cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
|
|
|
|
|
_isDisposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-01 15:57:59 -07:00
|
|
|
internal async Task StartAsync(bool causeProtocolException, CancellationToken cancellationToken)
|
2017-05-27 06:47:29 -07:00
|
|
|
{
|
|
|
|
|
_cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
|
|
|
|
CancellationToken = _cancellationTokenSource.Token;
|
|
|
|
|
|
2019-07-01 15:57:59 -07:00
|
|
|
IRequestHandlers requestHandlers = CreateRequestHandlers();
|
|
|
|
|
ConnectionOptions options = ConnectionOptions.CreateDefault();
|
|
|
|
|
|
|
|
|
|
if (causeProtocolException)
|
|
|
|
|
{
|
|
|
|
|
options = new ConnectionOptions(
|
|
|
|
|
new SemanticVersion(100, 0, 0),
|
|
|
|
|
new SemanticVersion(100, 0, 0),
|
|
|
|
|
options.HandshakeTimeout,
|
|
|
|
|
options.RequestTimeout);
|
|
|
|
|
}
|
2017-05-27 06:47:29 -07:00
|
|
|
|
|
|
|
|
_plugin = await PluginFactory.CreateFromCurrentProcessAsync(requestHandlers, options, CancellationToken);
|
|
|
|
|
|
|
|
|
|
if (_plugin.Connection.ProtocolVersion != ProtocolConstants.CurrentVersion)
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Task.Delay(Timeout.InfiniteTimeSpan, CancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task HandleResponseAsync(
|
|
|
|
|
IConnection connection,
|
|
|
|
|
Message message,
|
|
|
|
|
IResponseHandler responseHandler,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
2022-12-12 10:06:21 -08:00
|
|
|
var response = _responses.Take(cancellationToken);
|
2017-05-27 06:47:29 -07:00
|
|
|
|
|
|
|
|
if (message.Type == MessageType.Request)
|
|
|
|
|
{
|
|
|
|
|
switch (message.Method)
|
|
|
|
|
{
|
|
|
|
|
case MessageMethod.Initialize:
|
|
|
|
|
{
|
2026-05-13 17:34:03 -07:00
|
|
|
var initializeRequest = MessageUtilities.DeserializePayload<InitializeRequest>(message);
|
2017-05-27 06:47:29 -07:00
|
|
|
|
|
|
|
|
_plugin.Connection.Options.SetRequestTimeout(initializeRequest.RequestTimeout);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MessageMethod.Close:
|
|
|
|
|
_cancellationTokenSource.Cancel();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await responseHandler.SendResponseAsync(message, response.Payload, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IRequestHandlers CreateRequestHandlers()
|
|
|
|
|
{
|
|
|
|
|
var handlers = new RequestHandlers();
|
|
|
|
|
|
|
|
|
|
handlers.TryAdd(MessageMethod.Initialize, this);
|
|
|
|
|
handlers.TryAdd(MessageMethod.GetOperationClaims, this);
|
|
|
|
|
|
|
|
|
|
return handlers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnShuttingDown(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
_cancellationTokenSource.Cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|