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

UnitTesting and Razor External Access wrappers for ISB services #48776

Merged
merged 5 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 16 additions & 2 deletions eng/targets/GeneratePkgDef.targets
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

Metadata:
- ItemSpec: service name
- Audience ('Local'|'Process'): Microsoft.VisualStudio.Shell.ServiceBroker.ServiceAudience enum value
- ProfferingPackageId (guid, optional): GUID of the proffering package or empty for ServiceHub services

4) PkgDefBindingRedirect
Expand Down Expand Up @@ -87,6 +88,19 @@
<PkgDefFileContent Include="@(_FileContentEntries->'%(FullFilePath)')" />
</ItemGroup>
</Target>

<!--
Initializes metadata of PkgDefBrokeredService items.
-->
<Target Name="_InitializeBrokeredServiceEntries"
BeforeTargets="GeneratePkgDef">
<ItemGroup>
<PkgDefBrokeredService Update="@(PkgDefBrokeredService)">
<_Audience>dword:00000003</_Audience> <!-- Local -->
<_Audience Condition="'%(PkgDefBrokeredService.Audience)' == 'Process'">dword:00000001</_Audience>
</PkgDefBrokeredService>
</ItemGroup>
</Target>

<!--
Initializes metadata of PkgDefPackageRegistration items.
Expand Down Expand Up @@ -214,7 +228,7 @@
@="11AD60FC-6D87-4674-8F88-9ABE79176CBE"
"IsServiceHub"=dword:00000001
"ServiceLocation"="$PackageFolder$"
"audience"=dword:00000003
"audience"=%(PkgDefBrokeredService._Audience)
]]>
</RawValue>
</_PkgDefEntry>
Expand All @@ -223,7 +237,7 @@
<RawValue>
<![CDATA[[$RootKey$\BrokeredServices\%(PkgDefBrokeredService.Identity)]
@="%(PkgDefBrokeredService.ProfferingPackageId)"
"audience"=dword:00000003
"audience"=%(PkgDefBrokeredService._Audience)
]]>
</RawValue>
</_PkgDefEntry>
Expand Down
1 change: 1 addition & 0 deletions eng/targets/Services.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
-->
<ItemGroup>
<ServiceHubService Include="roslynRemoteHost" ClassName="Microsoft.CodeAnalysis.Remote.RemoteHostService" />
<ServiceHubService Include="Microsoft.VisualStudio.LanguageServices.WorkspaceSolutionProvider" ClassName="Microsoft.CodeAnalysis.Remote.RemoteWorkspaceSolutionProviderService+Factory" IsBrokered="true" Audience="Process" />
<ServiceHubService Include="Microsoft.VisualStudio.LanguageServices.AssetSynchronization" ClassName="Microsoft.CodeAnalysis.Remote.RemoteAssetSynchronizationService+Factory" IsBrokered="true" />
<ServiceHubService Include="Microsoft.VisualStudio.LanguageServices.AsynchronousOperationListener" ClassName="Microsoft.CodeAnalysis.Remote.RemoteAsynchronousOperationListenerService+Factory" IsBrokered="true" />
<ServiceHubService Include="Microsoft.VisualStudio.LanguageServices.DiagnosticAnalyzer" ClassName="Microsoft.CodeAnalysis.Remote.RemoteDiagnosticAnalyzerService+Factory" IsBrokered="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<ItemGroup>
<ProjectReference Include="..\..\..\Compilers\Core\Portable\Microsoft.CodeAnalysis.csproj" />
<ProjectReference Include="..\..\..\Workspaces\Core\Portable\Microsoft.CodeAnalysis.Workspaces.csproj" />
<ProjectReference Include="..\..\..\Workspaces\Remote\Core\Microsoft.CodeAnalysis.Remote.Workspaces.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
33 changes: 0 additions & 33 deletions src/Tools/ExternalAccess/Razor/RazorRemoteHostClient.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Remote;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
internal readonly struct RazorRemoteCallbackWrapper<T>
where T : class
{
internal readonly RemoteCallback<T> UnderlyingObject;

public RazorRemoteCallbackWrapper(T callback)
=> UnderlyingObject = new RemoteCallback<T>(callback);

public ValueTask InvokeAsync(Func<T, CancellationToken, ValueTask> invocation, CancellationToken cancellationToken)
=> UnderlyingObject.InvokeAsync(invocation, cancellationToken);

public ValueTask<TResult> InvokeAsync<TResult>(Func<T, CancellationToken, ValueTask<TResult>> invocation, CancellationToken cancellationToken)
=> UnderlyingObject.InvokeAsync(invocation, cancellationToken);
}
}
102 changes: 102 additions & 0 deletions src/Tools/ExternalAccess/Razor/Remote/RazorRemoteHostClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using MessagePack;
using MessagePack.Formatters;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Remote;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
internal readonly struct RazorRemoteHostClient
{
private readonly ServiceHubRemoteHostClient _client;
private readonly RazorServiceDescriptorsWrapper _serviceDescriptors;
private readonly RazorRemoteServiceCallbackDispatcherRegistry _callbackDispatchers;

internal RazorRemoteHostClient(ServiceHubRemoteHostClient client, RazorServiceDescriptorsWrapper serviceDescriptors, RazorRemoteServiceCallbackDispatcherRegistry callbackDispatchers)
{
_client = client;
_serviceDescriptors = serviceDescriptors;
_callbackDispatchers = callbackDispatchers;
}

[Obsolete]
public static async Task<RazorRemoteHostClient?> CreateAsync(Workspace workspace, CancellationToken cancellationToken = default)
{
var client = await RemoteHostClient.TryGetClientAsync(workspace.Services, cancellationToken).ConfigureAwait(false);
var descriptors = new RazorServiceDescriptorsWrapper("dummy", _ => throw ExceptionUtilities.Unreachable, ImmutableArray<IMessagePackFormatter>.Empty, ImmutableArray<IFormatterResolver>.Empty, Array.Empty<(Type, Type?)>());
return client == null ? null : new RazorRemoteHostClient((ServiceHubRemoteHostClient)client, descriptors, RazorRemoteServiceCallbackDispatcherRegistry.Empty);
}

[Obsolete]
public async Task<Optional<T>> TryRunRemoteAsync<T>(string targetName, Solution? solution, IReadOnlyList<object?> arguments, CancellationToken cancellationToken)
=> await _client.RunRemoteAsync<T>(WellKnownServiceHubService.Razor, targetName, solution, arguments, callbackTarget: null, cancellationToken).ConfigureAwait(false);

public static async Task<RazorRemoteHostClient?> TryGetClientAsync(HostWorkspaceServices services, RazorServiceDescriptorsWrapper serviceDescriptors, RazorRemoteServiceCallbackDispatcherRegistry callbackDispatchers, CancellationToken cancellationToken = default)
{
var client = await RemoteHostClient.TryGetClientAsync(services, cancellationToken).ConfigureAwait(false);
if (client is null)
return null;

return new RazorRemoteHostClient((ServiceHubRemoteHostClient)client, serviceDescriptors, callbackDispatchers);
}

public RazorRemoteServiceConnectionWrapper<TService> CreateConnection<TService>(object? callbackTarget) where TService : class
=> new(_client.CreateConnection<TService>(_serviceDescriptors.UnderlyingObject, _callbackDispatchers, callbackTarget));

// no solution, no callback:

public ValueTask<bool> TryInvokeAsync<TService>(Func<TService, CancellationToken, ValueTask> invocation, CancellationToken cancellationToken) where TService : class
=> _client.TryInvokeAsync(invocation, cancellationToken);

public ValueTask<Optional<TResult>> TryInvokeAsync<TService, TResult>(Func<TService, CancellationToken, ValueTask<TResult>> invocation, CancellationToken cancellationToken) where TService : class
=> _client.TryInvokeAsync(invocation, cancellationToken);

// no solution, callback:

public ValueTask<bool> TryInvokeAsync<TService>(Func<TService, RazorRemoteServiceCallbackIdWrapper, CancellationToken, ValueTask> invocation, object callbackTarget, CancellationToken cancellationToken) where TService : class
=> _client.TryInvokeAsync<TService>(
(service, callbackId, cancellationToken) => invocation(service, new RazorRemoteServiceCallbackIdWrapper(callbackId), cancellationToken),
callbackTarget,
cancellationToken);

public ValueTask<Optional<TResult>> TryInvokeAsync<TService, TResult>(Func<TService, RazorRemoteServiceCallbackIdWrapper, CancellationToken, ValueTask<TResult>> invocation, object callbackTarget, CancellationToken cancellationToken) where TService : class
=> _client.TryInvokeAsync<TService, TResult>(
(service, callbackId, cancellationToken) => invocation(service, new RazorRemoteServiceCallbackIdWrapper(callbackId), cancellationToken),
callbackTarget,
cancellationToken);

// solution, no callback:

public ValueTask<bool> TryInvokeAsync<TService>(Solution solution, Func<TService, object, CancellationToken, ValueTask> invocation, CancellationToken cancellationToken) where TService : class
=> _client.TryInvokeAsync(solution, invocation, cancellationToken);

public ValueTask<Optional<TResult>> TryInvokeAsync<TService, TResult>(Solution solution, Func<TService, object, CancellationToken, ValueTask<TResult>> invocation, CancellationToken cancellationToken) where TService : class
=> _client.TryInvokeAsync(solution, invocation, cancellationToken);

// solution, callback:

public ValueTask<bool> TryInvokeAsync<TService>(Solution solution, Func<TService, object, RazorRemoteServiceCallbackIdWrapper, CancellationToken, ValueTask> invocation, object callbackTarget, CancellationToken cancellationToken) where TService : class
=> _client.TryInvokeAsync<TService>(
solution,
(service, solutionInfo, callbackId, cancellationToken) => invocation(service, solutionInfo, new RazorRemoteServiceCallbackIdWrapper(callbackId), cancellationToken),
callbackTarget,
cancellationToken);

public ValueTask<Optional<TResult>> TryInvokeAsync<TService, TResult>(Solution solution, Func<TService, object, RazorRemoteServiceCallbackIdWrapper, CancellationToken, ValueTask<TResult>> invocation, object callbackTarget, CancellationToken cancellationToken) where TService : class
=> _client.TryInvokeAsync<TService, TResult>(
solution,
(service, solutionInfo, callbackId, cancellationToken) => invocation(service, solutionInfo, new RazorRemoteServiceCallbackIdWrapper(callbackId), cancellationToken),
callbackTarget,
cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.Remote;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
internal abstract class RazorRemoteServiceCallbackDispatcher : IRemoteServiceCallbackDispatcher
{
private readonly RemoteServiceCallbackDispatcher _dispatcher = new();

public object GetCallback(RazorRemoteServiceCallbackIdWrapper callbackId)
=> _dispatcher.GetCallback(callbackId.UnderlyingObject);

RemoteServiceCallbackDispatcher.Handle IRemoteServiceCallbackDispatcher.CreateHandle(object? instance)
=> _dispatcher.CreateHandle(instance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Remote;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
internal sealed class RazorRemoteServiceCallbackDispatcherRegistry : IRemoteServiceCallbackDispatcherProvider
{
public static readonly RazorRemoteServiceCallbackDispatcherRegistry Empty = new(Array.Empty<(Type, RazorRemoteServiceCallbackDispatcher)>());

private readonly ImmutableDictionary<Type, RazorRemoteServiceCallbackDispatcher> _lazyDispatchers;

public RazorRemoteServiceCallbackDispatcherRegistry(IEnumerable<(Type serviceType, RazorRemoteServiceCallbackDispatcher dispatcher)> lazyDispatchers)
{
_lazyDispatchers = lazyDispatchers.ToImmutableDictionary(e => e.serviceType, e => e.dispatcher);
}

IRemoteServiceCallbackDispatcher IRemoteServiceCallbackDispatcherProvider.GetDispatcher(Type serviceType)
=> _lazyDispatchers[serviceType];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.Serialization;
using Microsoft.CodeAnalysis.Remote;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
[DataContract]
internal readonly struct RazorRemoteServiceCallbackIdWrapper
{
[DataMember(Order = 0)]
internal RemoteServiceCallbackId UnderlyingObject { get; }

public RazorRemoteServiceCallbackIdWrapper(RemoteServiceCallbackId underlyingObject)
=> UnderlyingObject = underlyingObject;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Remote;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor
{
internal readonly struct RazorRemoteServiceConnectionWrapper<TService> : IDisposable
where TService : class
{
internal RemoteServiceConnection<TService> UnderlyingObject { get; }

internal RazorRemoteServiceConnectionWrapper(RemoteServiceConnection<TService> underlyingObject)
=> UnderlyingObject = underlyingObject;

public void Dispose()
=> UnderlyingObject.Dispose();

// no solution, no callback

public ValueTask<bool> TryInvokeAsync(Func<TService, CancellationToken, ValueTask> invocation, CancellationToken cancellationToken)
=> UnderlyingObject.TryInvokeAsync(invocation, cancellationToken);

public ValueTask<Optional<TResult>> TryInvokeAsync<TResult>(Func<TService, CancellationToken, ValueTask<TResult>> invocation, CancellationToken cancellationToken)
=> UnderlyingObject.TryInvokeAsync(invocation, cancellationToken);

// no solution, callback

public ValueTask<bool> TryInvokeAsync(Func<TService, RazorRemoteServiceCallbackIdWrapper, CancellationToken, ValueTask> invocation, CancellationToken cancellationToken)
=> UnderlyingObject.TryInvokeAsync(
(service, callbackId, cancellationToken) => invocation(service, new RazorRemoteServiceCallbackIdWrapper(callbackId), cancellationToken),
cancellationToken);

public ValueTask<Optional<TResult>> TryInvokeAsync<TResult>(Func<TService, RazorRemoteServiceCallbackIdWrapper, CancellationToken, ValueTask<TResult>> invocation, CancellationToken cancellationToken)
=> UnderlyingObject.TryInvokeAsync(
(service, callbackId, cancellationToken) => invocation(service, new RazorRemoteServiceCallbackIdWrapper(callbackId), cancellationToken),
cancellationToken);

// solution, no callback

public ValueTask<bool> TryInvokeAsync(Solution solution, Func<TService, object, CancellationToken, ValueTask> invocation, CancellationToken cancellationToken)
=> UnderlyingObject.TryInvokeAsync(solution, invocation, cancellationToken);

public ValueTask<Optional<TResult>> TryInvokeAsync<TResult>(Solution solution, Func<TService, object, CancellationToken, ValueTask<TResult>> invocation, CancellationToken cancellationToken)
=> UnderlyingObject.TryInvokeAsync(solution, invocation, cancellationToken);

// solution, callback

public ValueTask<bool> TryInvokeAsync(Solution solution, Func<TService, object, RazorRemoteServiceCallbackIdWrapper, CancellationToken, ValueTask> invocation, CancellationToken cancellationToken)
=> UnderlyingObject.TryInvokeAsync(
solution,
(service, solutionInfo, callbackId, cancellationToken) => invocation(service, solutionInfo, new RazorRemoteServiceCallbackIdWrapper(callbackId), cancellationToken),
cancellationToken);

public ValueTask<Optional<TResult>> TryInvokeAsync<TResult>(Solution solution, Func<TService, object, RazorRemoteServiceCallbackIdWrapper, CancellationToken, ValueTask<TResult>> invocation, CancellationToken cancellationToken)
=> UnderlyingObject.TryInvokeAsync(
solution,
(service, solutionInfo, callbackId, cancellationToken) => invocation(service, solutionInfo, new RazorRemoteServiceCallbackIdWrapper(callbackId), cancellationToken),
cancellationToken);
}
}
Loading