-
Notifications
You must be signed in to change notification settings - Fork 367
/
KernelTests.cs
256 lines (218 loc) · 8.79 KB
/
KernelTests.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// 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.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using FluentAssertions;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using FluentAssertions.Extensions;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Xunit;
namespace Microsoft.DotNet.Interactive.Tests
{
public class KernelTests
{
[Fact]
public void Deferred_initialization_command_is_not_executed_prior_to_first_submission()
{
var receivedCommands = new List<KernelCommand>();
using var kernel = new FakeKernel
{
Handle = (command, context) =>
{
receivedCommands.Add(command);
return Task.CompletedTask;
}
};
kernel.DeferCommand(new SubmitCode("hello"));
kernel.DeferCommand(new SubmitCode("world!"));
receivedCommands.Should().BeEmpty();
}
[Fact]
public async Task Deferred_initialization_command_is_executed_on_first_submission()
{
var receivedCommands = new List<KernelCommand>();
using var kernel = new FakeKernel
{
Handle = (command, context) =>
{
receivedCommands.Add(command);
return Task.CompletedTask;
}
};
kernel.DeferCommand(new SubmitCode("one"));
kernel.DeferCommand(new SubmitCode("two"));
await kernel.SendAsync(new SubmitCode("three"));
receivedCommands
.Select(c => c is SubmitCode submitCode ? submitCode.Code : c.ToString())
.Should()
.BeEquivalentSequenceTo("one", "two", "three");
}
[Fact]
public async Task Split_non_referencing_directives_are_executed_in_textual_order()
{
var receivedCommands = new List<string>();
using var kernel = new FakeKernel
{
Handle = (command, context) =>
{
receivedCommands.Add(((SubmitCode)command).Code);
return Task.CompletedTask;
}
};
kernel.AddDirective(new Command("#!one")
{
Handler = CommandHandler.Create(() =>
{
receivedCommands.Add("#!one");
})
});
kernel.AddDirective(new Command("#!two")
{
Handler = CommandHandler.Create(() =>
{
receivedCommands.Add("#!two");
})
});
var code1 = "var a = 1";
var code2 = "var b = 2";
var code3 = "var c = 3";
var directive1 = "#!one";
var directive2 = "#!two";
await kernel.SubmitCodeAsync($@"
{code1}
{directive1}
{code2}
{directive2}
{code3}
");
receivedCommands
.Select(c => c.Trim())
.Should()
.BeEquivalentSequenceTo(
code1,
directive1,
code2,
directive2,
code3);
}
[Fact]
public async Task Middleware_is_only_executed_once_per_command()
{
var middeware1Count = 0;
var middeware2Count = 0;
var middeware3Count = 0;
using var kernel = new FakeKernel();
kernel.AddMiddleware(async (command, context, next) =>
{
middeware1Count++;
await next(command, context);
}, "one");
kernel.AddMiddleware(async (command, context, next) =>
{
middeware2Count++;
await next(command, context);
}, "two");
kernel.AddMiddleware(async (command, context, next) =>
{
middeware3Count++;
await next(command, context);
}, "three");
await kernel.SendAsync(new SubmitCode("123"));
middeware1Count.Should().Be(1);
middeware2Count.Should().Be(1);
middeware3Count.Should().Be(1);
}
[Fact]
public async Task kernelEvents_sequence_completes_when_kernel_is_disposed ()
{
var kernel = new FakeKernel();
var events = kernel.KernelEvents.Timeout(5.Seconds()).LastOrDefaultAsync();
kernel.Dispose();
var lastEvent = await events;
lastEvent.Should().BeNull();
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void When_command_type_registered_then_kernel_registers_envelope_type_for_serialization(bool withHandler)
{
Microsoft.DotNet.Interactive.Server.KernelCommandEnvelope.ResetToDefaults();
using var kernel = new FakeKernel();
if (withHandler)
{
kernel.RegisterCommandHandler<CustomCommandTypes.FirstSubmission.MyCommand>(
(_, _) => Task.CompletedTask);
}
else
{
kernel.RegisterCommandType<CustomCommandTypes.FirstSubmission.MyCommand>();
}
var originalCommand = new CustomCommandTypes.FirstSubmission.MyCommand("xyzzy");
string envelopeJson = Microsoft.DotNet.Interactive.Server.KernelCommandEnvelope.Serialize(originalCommand);
var roundTrippedCommandEnvelope = Microsoft.DotNet.Interactive.Server.KernelCommandEnvelope.Deserialize(envelopeJson);
roundTrippedCommandEnvelope
.Command
.Should()
.BeOfType<CustomCommandTypes.FirstSubmission.MyCommand>()
.Which
.Info
.Should()
.Be(originalCommand.Info);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void When_command_type_reregistered_with_changed_type_command_then_kernel_registers_updated_envelope_type_for_serialization(bool withHandler)
{
// Notebook authors should be able to develop their custom commands experimentally and progressively,
// so we don't want any "you have to restart your kernel now" situations just because you already
// called RegisterCommandHandler once for a particular command type.
Microsoft.DotNet.Interactive.Server.KernelCommandEnvelope.ResetToDefaults();
using var kernel = new FakeKernel();
if (withHandler)
{
kernel.RegisterCommandHandler<CustomCommandTypes.FirstSubmission.MyCommand>(
(_, _) => Task.CompletedTask);
kernel.RegisterCommandHandler<CustomCommandTypes.SecondSubmission.MyCommand>(
(_, _) => Task.CompletedTask);
}
else
{
kernel.RegisterCommandType<CustomCommandTypes.FirstSubmission.MyCommand>();
kernel.RegisterCommandType<CustomCommandTypes.SecondSubmission.MyCommand>();
}
var originalCommand = new CustomCommandTypes.SecondSubmission.MyCommand("xyzzy", 42);
string envelopeJson = Microsoft.DotNet.Interactive.Server.KernelCommandEnvelope.Serialize(originalCommand);
var roundTrippedCommandEnvelope = Microsoft.DotNet.Interactive.Server.KernelCommandEnvelope.Deserialize(envelopeJson);
roundTrippedCommandEnvelope
.Command
.Should()
.BeOfType<CustomCommandTypes.SecondSubmission.MyCommand>()
.Which
.Info
.Should()
.Be(originalCommand.Info);
roundTrippedCommandEnvelope
.Command
.As<CustomCommandTypes.SecondSubmission.MyCommand>()
.AdditionalProperty
.Should()
.Be(originalCommand.AdditionalProperty);
}
[Fact]
public void Return_the_list_of_supported_commands()
{
using var kernel = new FakeKernel();
kernel.RegisterCommandType<CustomCommandTypes.FirstSubmission.MyCommand>();
kernel.RegisterCommandType<CustomCommandTypes.SecondSubmission.MyCommand>();
var supportedCommands = kernel.SupportedCommands();
supportedCommands.Should().ContainInOrder(typeof(SubmitCode),
typeof(CustomCommandTypes.FirstSubmission.MyCommand),
typeof(CustomCommandTypes.SecondSubmission.MyCommand));
}
}
}