forked from dotnet/interactive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJupyterKernelTestBase.cs
More file actions
95 lines (77 loc) · 3.54 KB
/
Copy pathJupyterKernelTestBase.cs
File metadata and controls
95 lines (77 loc) · 3.54 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 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 FluentAssertions;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive.Formatting;
using Microsoft.DotNet.Interactive.Formatting.Csv;
using Microsoft.DotNet.Interactive.Formatting.TabularData;
using Microsoft.DotNet.Interactive.Jupyter.Connection;
using Microsoft.DotNet.Interactive.Jupyter.Protocol;
using Microsoft.DotNet.Interactive.Tests.Utility;
using System;
using System.Collections.Generic;
using System.Reactive.Disposables;
using System.Threading.Tasks;
using Formatter = Microsoft.DotNet.Interactive.Formatting.Formatter;
using Message = Microsoft.DotNet.Interactive.Jupyter.Messaging.Message;
namespace Microsoft.DotNet.Interactive.Jupyter.Tests;
public abstract class JupyterKernelTestBase : IDisposable
{
protected CompositeDisposable _disposables = new();
// to re-record the tests for simulated playback with JuptyerTestData, set this to true
protected const bool RECORD_FOR_PLAYBACK = false;
protected const string PythonKernelName = "python3";
protected const string RKernelName = "ir";
public virtual void Dispose()
{
_disposables.Dispose();
}
protected CompositeKernel CreateCompositeKernelAsync(params IJupyterKernelConnectionOptions[] optionsList)
{
Formatter.SetPreferredMimeTypesFor(typeof(TabularDataResource), HtmlFormatter.MimeType, CsvFormatter.MimeType);
var csharpKernel = new CSharpKernel()
.UseKernelHelpers()
.UseValueSharing();
var kernel = new CompositeKernel { csharpKernel };
kernel.DefaultKernelName = csharpKernel.Name;
var jupyterKernelCommand = new ConnectJupyterKernelDirective();
foreach (var options in optionsList)
{
jupyterKernelCommand.AddConnectionOptions(options);
}
kernel.AddConnectDirective(jupyterKernelCommand);
_disposables.Add(kernel);
return kernel;
}
protected static List<Message> GenerateReplies(IReadOnlyCollection<Message> messages = null, string languageName = "name")
{
var replies = new List<Message>
{
// always sent as a first request
Message.CreateReply(
new KernelInfoReply("protocolVersion", "implementation", null,
new LanguageInfo(languageName, "version", "mimeType", "fileExt")),
Message.Create(new KernelInfoRequest()))
};
if (messages is not null)
{
// replies from the kernel start and end with status messages
foreach (var m in messages)
{
replies.Add(Message.Create(new Status(StatusValues.Busy), m.ParentHeader));
replies.Add(m);
replies.Add(Message.Create(new Status(StatusValues.Idle), m.ParentHeader));
}
}
return replies;
}
protected async Task<Kernel> CreateJupyterKernelAsync(TestJupyterConnectionOptions options, string kernelSpecName = null, string connectionString = null)
{
var kernel = CreateCompositeKernelAsync(options);
var result = await kernel.SubmitCodeAsync($"#!connect jupyter --kernel-name testKernel --kernel-spec {kernelSpecName ?? "testKernelSpec"} {connectionString}");
result.Events
.Should()
.NotContainErrors();
return kernel.FindKernelByName("testKernel");
}
}