forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivation.unit.test.ts
More file actions
114 lines (99 loc) · 5.13 KB
/
activation.unit.test.ts
File metadata and controls
114 lines (99 loc) · 5.13 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { anything, deepEqual, instance, mock, verify, when } from 'ts-mockito';
import { EventEmitter } from 'vscode';
import { IExtensionSingleActivationService } from '../../client/activation/types';
import { PythonExecutionFactory } from '../../client/common/process/pythonExecutionFactory';
import { IPythonExecutionFactory } from '../../client/common/process/types';
import { sleep } from '../../client/common/utils/async';
import { Activation } from '../../client/datascience/activation';
import { JupyterDaemonModule } from '../../client/datascience/constants';
import { ActiveEditorContextService } from '../../client/datascience/context/activeEditorContext';
import { NativeEditor } from '../../client/datascience/interactive-ipynb/nativeEditor';
import { JupyterInterpreterService } from '../../client/datascience/jupyter/interpreter/jupyterInterpreterService';
import { KernelDaemonPreWarmer } from '../../client/datascience/kernel-launcher/kernelDaemonPreWarmer';
import { NativeEditorProvider } from '../../client/datascience/notebookStorage/nativeEditorProvider';
import {
INotebookAndInteractiveWindowUsageTracker,
INotebookEditor,
INotebookEditorProvider
} from '../../client/datascience/types';
import { PythonEnvironment } from '../../client/pythonEnvironments/info';
import { FakeClock } from '../common';
import { createPythonInterpreter } from '../utils/interpreters';
suite('DataScience - Activation', () => {
let activator: IExtensionSingleActivationService;
let notebookEditorProvider: INotebookEditorProvider;
let jupyterInterpreterService: JupyterInterpreterService;
let executionFactory: IPythonExecutionFactory;
let openedEventEmitter: EventEmitter<INotebookEditor>;
let interpreterEventEmitter: EventEmitter<PythonEnvironment>;
let contextService: ActiveEditorContextService;
let fakeTimer: FakeClock;
const interpreter = createPythonInterpreter();
setup(async () => {
fakeTimer = new FakeClock();
openedEventEmitter = new EventEmitter<INotebookEditor>();
interpreterEventEmitter = new EventEmitter<PythonEnvironment>();
const tracker = mock<INotebookAndInteractiveWindowUsageTracker>();
notebookEditorProvider = mock(NativeEditorProvider);
jupyterInterpreterService = mock(JupyterInterpreterService);
executionFactory = mock(PythonExecutionFactory);
contextService = mock(ActiveEditorContextService);
const daemonPool = mock(KernelDaemonPreWarmer);
when(notebookEditorProvider.onDidOpenNotebookEditor).thenReturn(openedEventEmitter.event);
when(jupyterInterpreterService.onDidChangeInterpreter).thenReturn(interpreterEventEmitter.event);
when(executionFactory.createDaemon(anything())).thenResolve();
when(contextService.activate()).thenResolve();
when(daemonPool.activate(anything())).thenResolve();
activator = new Activation(
instance(notebookEditorProvider),
instance(jupyterInterpreterService),
instance(executionFactory),
[],
instance(contextService),
instance(daemonPool),
instance(tracker)
);
when(jupyterInterpreterService.getSelectedInterpreter()).thenResolve(interpreter);
when(jupyterInterpreterService.getSelectedInterpreter(anything())).thenResolve(interpreter);
when(jupyterInterpreterService.setInitialInterpreter()).thenResolve(interpreter);
await activator.activate();
});
teardown(() => fakeTimer.uninstall());
async function testCreatingDaemonWhenOpeningANotebook() {
fakeTimer.install();
const notebook: INotebookEditor = mock(NativeEditor);
// Open a notebook, (fire the event).
openedEventEmitter.fire(notebook);
// Wait for debounce to complete.
await fakeTimer.wait();
verify(executionFactory.createDaemon(anything())).once();
verify(
executionFactory.createDaemon(
deepEqual({ daemonModule: JupyterDaemonModule, pythonPath: interpreter.path })
)
).once();
}
test('Create a daemon when a notebook is opened', async () => testCreatingDaemonWhenOpeningANotebook());
test('Create a daemon when changing interpreter after a notebook has beeen opened', async () => {
await testCreatingDaemonWhenOpeningANotebook();
// Trigger changes to interpreter.
interpreterEventEmitter.fire(interpreter);
// Wait for debounce to complete.
await fakeTimer.wait();
verify(
executionFactory.createDaemon(
deepEqual({ daemonModule: JupyterDaemonModule, pythonPath: interpreter.path })
)
).twice();
});
test('Changing interpreter without opening a notebook does not result in a daemon being created', async () => {
// Trigger changes to interpreter.
interpreterEventEmitter.fire(interpreter);
// Assume a debounce is required and wait.
await sleep(10);
verify(executionFactory.createDaemon(anything())).never();
});
});