-
Notifications
You must be signed in to change notification settings - Fork 368
/
InProcessTestServer.cs
63 lines (50 loc) · 2.11 KB
/
InProcessTestServer.cs
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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.CommandLine.IO;
using System.CommandLine.Parsing;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.TestHost;
using Microsoft.DotNet.Interactive.App.CommandLine;
using Microsoft.DotNet.Interactive.Connection;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.DotNet.Interactive.App.Tests;
internal class InProcessTestServer : IDisposable
{
private Lazy<TestServer> _host;
private readonly ServiceCollection _serviceCollection = new();
public static async Task<InProcessTestServer> StartServer(string args, Action<IServiceCollection> servicesSetup = null)
{
var server = new InProcessTestServer();
var completionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var parser = CommandLineParser.Create(
server._serviceCollection,
(startupOptions, invocationContext) =>
{
servicesSetup?.Invoke(server._serviceCollection);
var builder = Program.ConstructWebHostBuilder(
startupOptions,
server._serviceCollection);
server._host = new Lazy<TestServer>(() => new TestServer(builder));
completionSource.SetResult(true);
});
await parser.InvokeAsync(args, new TestConsole());
await completionSource.Task;
return server;
}
private InProcessTestServer()
{
}
public FrontendEnvironment FrontendEnvironment => _host.Value.Services.GetRequiredService<Kernel>().FrontendEnvironment;
public HttpClient HttpClient => _host.Value.CreateClient();
public Kernel Kernel => _host.Value.Services.GetService<Kernel>();
public void Dispose()
{
KernelCommandEnvelope.RegisterDefaults();
KernelEventEnvelope.RegisterDefaults();
Kernel?.Dispose();
_host.Value.Dispose();
_host = null;
}
}