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
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
import { setupInstantiationService, withTestNotebook as _withTestNotebook } from 'vs/workbench/contrib/notebook/test/browser/testNotebookEditor';
import { Emitter, Event } from 'vs/base/common/event';
import { INotebookKernelService, IResolvedNotebookKernel, NotebookKernelType } from 'vs/workbench/contrib/notebook/common/notebookKernelService';
import { NotebookKernelService } from 'vs/workbench/contrib/notebook/browser/notebookKernelServiceImpl';
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
import { mock } from 'vs/base/test/common/mock';
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
import { PLAINTEXT_LANGUAGE_ID } from 'vs/editor/common/languages/modesRegistry';
suite('NotebookKernelService', () => {
let instantiationService: TestInstantiationService;
let kernelService: INotebookKernelService;
let disposables: DisposableStore;
let onDidAddNotebookDocument: Emitter<NotebookTextModel>;
setup(function () {
disposables = new DisposableStore();
onDidAddNotebookDocument = new Emitter();
disposables.add(onDidAddNotebookDocument);
instantiationService = setupInstantiationService(disposables);
instantiationService.stub(INotebookService, new class extends mock<INotebookService>() {
override onDidAddNotebookDocument = onDidAddNotebookDocument.event;
override onWillRemoveNotebookDocument = Event.None;
override getNotebookTextModels() { return []; }
});
kernelService = instantiationService.createInstance(NotebookKernelService);
instantiationService.set(INotebookKernelService, kernelService);
});
teardown(() => {
disposables.dispose();
});
test('notebook priorities', function () {
const u1 = URI.parse('foo:///one');
const u2 = URI.parse('foo:///two');
const k1 = new TestNotebookKernel({ label: 'z' });
const k2 = new TestNotebookKernel({ label: 'a' });
kernelService.registerKernel(k1);
kernelService.registerKernel(k2);
// equal priorities -> sort by name
let info = kernelService.getMatchingKernel({ uri: u1, viewType: 'foo' });
assert.ok(info.all[0] === k2);
assert.ok(info.all[1] === k1);
// update priorities for u1 notebook
kernelService.updateKernelNotebookAffinity(k2, u1, 2);
kernelService.updateKernelNotebookAffinity(k2, u2, 1);
// updated
info = kernelService.getMatchingKernel({ uri: u1, viewType: 'foo' });
assert.ok(info.all[0] === k2);
assert.ok(info.all[1] === k1);
// NOT updated
info = kernelService.getMatchingKernel({ uri: u2, viewType: 'foo' });
assert.ok(info.all[0] === k2);
assert.ok(info.all[1] === k1);
// reset
kernelService.updateKernelNotebookAffinity(k2, u1, undefined);
info = kernelService.getMatchingKernel({ uri: u1, viewType: 'foo' });
assert.ok(info.all[0] === k2);
assert.ok(info.all[1] === k1);
});
test('new kernel with higher affinity wins, https://github.com/microsoft/vscode/issues/122028', function () {
const notebook = URI.parse('foo:///one');
const kernel = new TestNotebookKernel();
kernelService.registerKernel(kernel);
let info = kernelService.getMatchingKernel({ uri: notebook, viewType: 'foo' });
assert.strictEqual(info.all.length, 1);
assert.ok(info.all[0] === kernel);
const betterKernel = new TestNotebookKernel();
kernelService.registerKernel(betterKernel);
info = kernelService.getMatchingKernel({ uri: notebook, viewType: 'foo' });
assert.strictEqual(info.all.length, 2);
kernelService.updateKernelNotebookAffinity(betterKernel, notebook, 2);
info = kernelService.getMatchingKernel({ uri: notebook, viewType: 'foo' });
assert.strictEqual(info.all.length, 2);
assert.ok(info.all[0] === betterKernel);
assert.ok(info.all[1] === kernel);
});
test('onDidChangeSelectedNotebooks not fired on initial notebook open #121904', function () {
const uri = URI.parse('foo:///one');
const jupyter = { uri, viewType: 'jupyter' };
const dotnet = { uri, viewType: 'dotnet' };
const jupyterKernel = new TestNotebookKernel({ viewType: jupyter.viewType });
const dotnetKernel = new TestNotebookKernel({ viewType: dotnet.viewType });
kernelService.registerKernel(jupyterKernel);
kernelService.registerKernel(dotnetKernel);
kernelService.selectKernelForNotebook(jupyterKernel, jupyter);
kernelService.selectKernelForNotebook(dotnetKernel, dotnet);
let info = kernelService.getMatchingKernel(dotnet);
assert.strictEqual(info.selected === dotnetKernel, true);
info = kernelService.getMatchingKernel(jupyter);
assert.strictEqual(info.selected === jupyterKernel, true);
});
test('onDidChangeSelectedNotebooks not fired on initial notebook open #121904, p2', async function () {
const uri = URI.parse('foo:///one');
const jupyter = { uri, viewType: 'jupyter' };
const dotnet = { uri, viewType: 'dotnet' };
const jupyterKernel = new TestNotebookKernel({ viewType: jupyter.viewType });
const dotnetKernel = new TestNotebookKernel({ viewType: dotnet.viewType });
kernelService.registerKernel(jupyterKernel);
kernelService.registerKernel(dotnetKernel);
kernelService.selectKernelForNotebook(jupyterKernel, jupyter);
kernelService.selectKernelForNotebook(dotnetKernel, dotnet);
{
// open as jupyter -> bind event
const p1 = Event.toPromise(kernelService.onDidChangeSelectedNotebooks);
const d1 = instantiationService.createInstance(NotebookTextModel, jupyter.viewType, jupyter.uri, [], {}, {});
onDidAddNotebookDocument.fire(d1);
const event = await p1;
assert.strictEqual(event.newKernel, jupyterKernel.id);
}
{
// RE-open as dotnet -> bind event
const p2 = Event.toPromise(kernelService.onDidChangeSelectedNotebooks);
const d2 = instantiationService.createInstance(NotebookTextModel, dotnet.viewType, dotnet.uri, [], {}, {});
onDidAddNotebookDocument.fire(d2);
const event2 = await p2;
assert.strictEqual(event2.newKernel, dotnetKernel.id);
}
});
});
class TestNotebookKernel implements IResolvedNotebookKernel {
type: NotebookKernelType.Resolved = NotebookKernelType.Resolved;
id: string = Math.random() + 'kernel';
label: string = 'test-label';
viewType = '*';
onDidChange = Event.None;
extension: ExtensionIdentifier = new ExtensionIdentifier('test');
localResourceRoot: URI = URI.file('/test');
description?: string | undefined;
detail?: string | undefined;
preloadUris: URI[] = [];
preloadProvides: string[] = [];
supportedLanguages: string[] = [];
executeNotebookCellsRequest(): Promise<void> {
throw new Error('Method not implemented.');
}
cancelNotebookCellExecution(): Promise<void> {
throw new Error('Method not implemented.');
}
constructor(opts?: { languages?: string[]; label?: string; viewType?: string }) {
this.supportedLanguages = opts?.languages ?? [PLAINTEXT_LANGUAGE_ID];
this.label = opts?.label ?? this.label;
this.viewType = opts?.viewType ?? this.viewType;
}
}
|