-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
isession.spec.ts
438 lines (380 loc) · 13.8 KB
/
isession.spec.ts
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { expect } from 'chai';
import { PageConfig } from '@jupyterlab/coreutils';
import { UUID } from '@lumino/coreutils';
import { Signal } from '@lumino/signaling';
import {
KernelMessage,
SessionManager,
KernelManager,
KernelAPI
} from '@jupyterlab/services';
import { Session } from '@jupyterlab/services';
import { expectFailure, testEmission } from '@jupyterlab/testutils';
import { handleRequest, SessionTester, init } from '../utils';
init();
let kernelManager = new KernelManager();
let sessionManager = new SessionManager({ kernelManager });
/**
* Start a new session with a unique name.
*/
async function startNew(): Promise<Session.ISessionConnection> {
const session = await sessionManager.startNew({
path: UUID.uuid4(),
name: UUID.uuid4(),
type: 'test'
});
return session;
}
describe('session', () => {
let session: Session.ISessionConnection;
let defaultSession: Session.ISessionConnection;
beforeAll(async () => {
jest.setTimeout(120000);
defaultSession = await startNew();
});
afterEach(async () => {
if (session && !session.isDisposed) {
await session.shutdown();
}
});
afterAll(async () => {
await defaultSession.shutdown();
});
describe('Session.DefaultSession', () => {
describe('#disposed', () => {
it('should emit when the session is disposed', async () => {
let called = false;
session = await startNew();
session.disposed.connect(() => {
called = true;
});
await session.shutdown();
session.dispose();
expect(called).to.equal(true);
});
});
describe('#kernelChanged', () => {
it('should emit when the kernel changes', async () => {
let called: Session.ISessionConnection.IKernelChangedArgs | null = null;
const object = {};
defaultSession.kernelChanged.connect((s, args) => {
called = args;
Signal.disconnectReceiver(object);
}, object);
const previous = defaultSession.kernel;
await defaultSession.changeKernel({ name: previous.name });
expect(previous).to.not.equal(defaultSession.kernel);
expect(called).to.deep.equal({
name: 'kernel',
oldValue: previous,
newValue: defaultSession.kernel
});
previous.dispose();
});
});
describe('#statusChanged', () => {
it('should emit when the kernel status changes', async () => {
let called = false;
defaultSession.statusChanged.connect((s, status) => {
if (status === 'busy') {
called = true;
}
});
await defaultSession.kernel.requestKernelInfo();
expect(called).to.equal(true);
});
});
describe('#iopubMessage', () => {
it('should be emitted for an iopub message', async () => {
let called = false;
defaultSession.iopubMessage.connect((s, msg) => {
if (msg.header.msg_type === 'status') {
called = true;
}
});
await defaultSession.kernel.requestExecute({ code: 'a=1' }, true).done;
expect(called).to.equal(true);
});
});
describe('#unhandledMessage', () => {
it('should be emitted for an unhandled message', async () => {
const tester = new SessionTester();
const session = await tester.startSession();
const msgId = UUID.uuid4();
const emission = testEmission(session.unhandledMessage, {
find: (k, msg) => msg.header.msg_id === msgId
});
const msg = KernelMessage.createMessage({
msgType: 'kernel_info_request',
channel: 'shell',
session: tester.serverSessionId,
msgId,
content: {}
});
msg.parent_header = { session: session.kernel.clientId };
tester.send(msg);
await emission;
await tester.shutdown();
tester.dispose();
});
});
describe('#propertyChanged', () => {
it('should be emitted when the session path changes', async () => {
const newPath = UUID.uuid4();
let called = false;
const object = {};
defaultSession.propertyChanged.connect((s, type) => {
expect(defaultSession.path).to.equal(newPath);
expect(type).to.equal('path');
called = true;
Signal.disconnectReceiver(object);
}, object);
await defaultSession.setPath(newPath);
expect(called).to.equal(true);
});
});
describe('#id', () => {
it('should be a string', () => {
expect(typeof defaultSession.id).to.equal('string');
});
});
describe('#path', () => {
it('should be a string', () => {
expect(typeof defaultSession.path).to.equal('string');
});
});
describe('#name', () => {
it('should be a string', () => {
expect(typeof defaultSession.name).to.equal('string');
});
});
describe('#type', () => {
it('should be a string', () => {
expect(typeof defaultSession.name).to.equal('string');
});
});
describe('#model', () => {
it('should be an IModel', () => {
const model = defaultSession.model;
expect(typeof model.id).to.equal('string');
expect(typeof model.path).to.equal('string');
expect(typeof model.kernel.name).to.equal('string');
expect(typeof model.kernel.id).to.equal('string');
});
});
describe('#kernel', () => {
it('should be an IKernel object', () => {
expect(typeof defaultSession.kernel.id).to.equal('string');
});
});
describe('#serverSettings', () => {
it('should be the serverSettings', () => {
expect(defaultSession.serverSettings.baseUrl).to.equal(
PageConfig.getBaseUrl()
);
});
});
describe('#isDisposed', () => {
it('should be true after we dispose of the session', async () => {
const session = await startNew();
expect(session.isDisposed).to.equal(false);
session.dispose();
expect(session.isDisposed).to.equal(true);
});
it('should be safe to call multiple times', async () => {
const session = await startNew();
expect(session.isDisposed).to.equal(false);
expect(session.isDisposed).to.equal(false);
session.dispose();
expect(session.isDisposed).to.equal(true);
expect(session.isDisposed).to.equal(true);
});
});
describe('#dispose()', () => {
it('should dispose of the resources held by the session', async () => {
const session = await startNew();
session.dispose();
expect(session.isDisposed).to.equal(true);
});
it('should be safe to call twice', async () => {
const session = await startNew();
session.dispose();
expect(session.isDisposed).to.equal(true);
session.dispose();
expect(session.isDisposed).to.equal(true);
});
it('should be safe to call if the kernel is disposed', async () => {
const session = await startNew();
session.kernel.dispose();
session.dispose();
expect(session.isDisposed).to.equal(true);
});
});
describe('#setPath()', () => {
it('should set the path of the session', async () => {
const newPath = UUID.uuid4();
await defaultSession.setPath(newPath);
expect(defaultSession.path).to.equal(newPath);
});
it('should fail for improper response status', async () => {
handleRequest(defaultSession, 201, {});
await expectFailure(defaultSession.setPath(UUID.uuid4()));
});
it('should fail for error response status', async () => {
handleRequest(defaultSession, 500, {});
await expectFailure(defaultSession.setPath(UUID.uuid4()), '');
});
it('should fail for improper model', async () => {
handleRequest(defaultSession, 200, {});
await expectFailure(defaultSession.setPath(UUID.uuid4()));
});
it('should fail if the session is disposed', async () => {
const session = sessionManager.connectTo({
model: defaultSession.model
});
session.dispose();
const promise = session.setPath(UUID.uuid4());
await expectFailure(promise, 'Session is disposed');
});
});
describe('#setType()', () => {
it('should set the type of the session', async () => {
const session = await startNew();
const type = UUID.uuid4();
await session.setType(type);
expect(session.type).to.equal(type);
await session.shutdown();
});
it('should fail for improper response status', async () => {
handleRequest(defaultSession, 201, {});
await expectFailure(defaultSession.setType(UUID.uuid4()));
});
it('should fail for error response status', async () => {
handleRequest(defaultSession, 500, {});
await expectFailure(defaultSession.setType(UUID.uuid4()), '');
});
it('should fail for improper model', async () => {
handleRequest(defaultSession, 200, {});
await expectFailure(defaultSession.setType(UUID.uuid4()));
});
it('should fail if the session is disposed', async () => {
const session = sessionManager.connectTo({
model: defaultSession.model
});
session.dispose();
const promise = session.setPath(UUID.uuid4());
await expectFailure(promise, 'Session is disposed');
});
});
describe('#setName()', () => {
it('should set the name of the session', async () => {
const name = UUID.uuid4();
await defaultSession.setName(name);
expect(defaultSession.name).to.equal(name);
});
it('should fail for improper response status', async () => {
handleRequest(defaultSession, 201, {});
await expectFailure(defaultSession.setName(UUID.uuid4()));
});
it('should fail for error response status', async () => {
handleRequest(defaultSession, 500, {});
await expectFailure(defaultSession.setName(UUID.uuid4()), '');
});
it('should fail for improper model', async () => {
handleRequest(defaultSession, 200, {});
await expectFailure(defaultSession.setName(UUID.uuid4()));
});
it('should fail if the session is disposed', async () => {
const session = sessionManager.connectTo({
model: defaultSession.model
});
session.dispose();
const promise = session.setPath(UUID.uuid4());
await expectFailure(promise, 'Session is disposed');
});
});
describe('#changeKernel()', () => {
it('should create a new kernel with the new name', async () => {
session = await startNew();
const previous = session.kernel;
await previous.info;
await session.changeKernel({ name: previous.name });
expect(session.kernel.name).to.equal(previous.name);
expect(session.kernel.id).to.not.equal(previous.id);
expect(session.kernel).to.not.equal(previous);
previous.dispose();
});
it('should accept the id of the new kernel', async () => {
session = await startNew();
const previous = session.kernel;
await previous.info;
const kernel = await KernelAPI.startNew();
await session.changeKernel({ id: kernel.id });
expect(session.kernel.id).to.equal(kernel.id);
expect(session.kernel).to.not.equal(previous);
expect(session.kernel).to.not.equal(kernel);
previous.dispose();
await KernelAPI.shutdownKernel(kernel.id);
});
it('should update the session path if it has changed', async () => {
session = await startNew();
const previous = session.kernel;
await previous.info;
const model = { ...session.model, path: 'foo.ipynb' };
handleRequest(session, 200, model);
await session.changeKernel({ name: previous.name });
expect(session.kernel.name).to.equal(previous.name);
expect(session.path).to.equal(model.path);
previous.dispose();
});
});
describe('#shutdown()', () => {
it('should shut down properly', async () => {
session = await startNew();
await session.shutdown();
});
it('should emit a disposed signal', async () => {
let called = false;
session = await startNew();
session.disposed.connect(() => {
called = true;
});
await session.shutdown();
expect(called).to.equal(true);
});
it('should fail for an incorrect response status', async () => {
handleRequest(defaultSession, 200, {});
await expectFailure(defaultSession.shutdown());
});
it('should handle a 404 status', async () => {
session = await startNew();
handleRequest(session, 404, {});
await session.shutdown();
});
it('should handle a specific error status', async () => {
handleRequest(defaultSession, 410, {});
let promise = defaultSession.shutdown();
try {
await promise;
throw Error('should not get here');
} catch (err) {
const text = 'The kernel was deleted but the session was not';
expect(err.message).to.contain(text);
}
});
it('should fail for an error response status', async () => {
handleRequest(defaultSession, 500, {});
await expectFailure(defaultSession.shutdown(), '');
});
it('should fail if the session is disposed', async () => {
const session = sessionManager.connectTo({
model: defaultSession.model
});
session.dispose();
await expectFailure(session.shutdown(), 'Session is disposed');
});
});
});
});