forked from jupyterlab/jupyterlab
/
ikernel.spec.ts
1527 lines (1352 loc) · 45.2 KB
/
ikernel.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 '@phosphor/coreutils';
import { PromiseDelegate } from '@phosphor/coreutils';
import { Kernel, KernelMessage } from '@jupyterlab/services';
import { expectFailure, testEmission } from '@jupyterlab/testutils';
import { KernelTester, handleRequest } from '../utils';
describe('Kernel.IKernel', () => {
let defaultKernel: Kernel.IKernel;
let specs: Kernel.ISpecModels;
beforeAll(async () => {
jest.setTimeout(60000);
specs = await Kernel.getSpecs();
});
beforeEach(async () => {
defaultKernel = await Kernel.startNew();
await defaultKernel.ready;
});
afterEach(async () => {
await defaultKernel.shutdown();
defaultKernel.dispose();
});
afterAll(async () => {
await Kernel.shutdownAll();
});
describe('#terminated', () => {
it('should be emitted when the kernel is shut down', async () => {
let called = false;
defaultKernel.terminated.connect((sender, args) => {
expect(sender).to.equal(defaultKernel);
expect(args).to.be.undefined;
called = true;
});
await defaultKernel.shutdown();
expect(called).to.equal(true);
});
});
describe('#statusChanged', () => {
it('should be a signal following the Kernel status', async () => {
let called = false;
defaultKernel.statusChanged.connect(() => {
if (defaultKernel.status === 'busy') {
called = true;
}
});
await defaultKernel.requestExecute({ code: 'a=1' }, true).done;
expect(called).to.equal(true);
});
});
describe('#iopubMessage', () => {
it('should be emitted for an iopub message', async () => {
let called = false;
defaultKernel.iopubMessage.connect((k, msg) => {
called = true;
});
await defaultKernel.requestExecute({ code: 'a=1' }, true).done;
expect(called).to.equal(true);
});
it('should be emitted regardless of the sender', async () => {
const tester = new KernelTester();
const kernel = await tester.start();
const msgId = UUID.uuid4();
const emission = testEmission(kernel.iopubMessage, {
find: (k, msg) => msg.header.msg_id === msgId
});
const msg = KernelMessage.createMessage({
msgType: 'status',
channel: 'iopub',
session: tester.serverSessionId,
msgId,
content: {
execution_state: 'idle'
}
});
tester.send(msg);
await emission;
await tester.shutdown();
tester.dispose();
});
});
describe('#unhandledMessage', () => {
let tester: KernelTester;
beforeEach(() => {
tester = new KernelTester();
});
afterEach(async () => {
await tester.shutdown();
tester.dispose();
});
it('should be emitted for an unhandled message', async () => {
const kernel = await tester.start();
const msgId = UUID.uuid4();
const emission = testEmission(kernel.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: kernel.clientId };
tester.send(msg);
await emission;
});
it('should not be emitted for an iopub signal', async () => {
const kernel = await tester.start();
// We'll send two messages, first an iopub message, then a shell message.
// The unhandledMessage signal should only emit once for the shell message.
const msgId = UUID.uuid4();
const emission = testEmission(kernel.unhandledMessage, {
test: (k, msg) => {
expect(msg.header.msg_id).to.equal(msgId);
}
});
// Send an iopub message.
tester.sendStatus(UUID.uuid4(), 'idle');
// Send a shell message.
const msg = KernelMessage.createMessage({
msgType: 'kernel_info_request',
channel: 'shell',
session: tester.serverSessionId,
msgId,
content: {}
});
msg.parent_header = { session: kernel.clientId };
tester.send(msg);
await emission;
});
it('should not be emitted for a different client session', async () => {
const kernel = await tester.start();
// We'll send two messages, first a message with a different session, then
// one with the current client session. The unhandledMessage signal should
// only emit once for the current session message.
const msgId = 'message from right session';
const emission = testEmission(kernel.unhandledMessage, {
test: (k, msg) => {
expect((msg.parent_header as KernelMessage.IHeader).session).to.equal(
kernel.clientId
);
expect(msg.header.msg_id).to.equal(msgId);
}
});
// Send a shell message with the wrong client (parent) session.
const msg1 = KernelMessage.createMessage({
msgType: 'kernel_info_request',
channel: 'shell',
session: tester.serverSessionId,
msgId: 'message from wrong session',
content: {}
});
msg1.parent_header = { session: 'wrong session' };
tester.send(msg1);
// Send a shell message with the right client (parent) session.
const msg2 = KernelMessage.createMessage({
msgType: 'kernel_info_request',
channel: 'shell',
session: tester.serverSessionId,
msgId: msgId,
content: {}
});
msg2.parent_header = { session: kernel.clientId };
tester.send(msg2);
await emission;
});
});
describe('#anyMessage', () => {
let tester: KernelTester;
beforeEach(() => {
tester = new KernelTester();
});
afterEach(async () => {
await tester.shutdown();
tester.dispose();
});
it('should be emitted for an unhandled message', async () => {
const kernel = await tester.start();
const msgId = UUID.uuid4();
const emission = testEmission(kernel.anyMessage, {
test: (k, args) => {
expect(args.msg.header.msg_id).to.equal(msgId);
expect(args.msg.header.msg_type).to.equal('kernel_info_request');
expect(args.direction).to.equal('recv');
}
});
const msg = KernelMessage.createMessage({
msgType: 'kernel_info_request',
channel: 'shell',
session: tester.serverSessionId,
msgId,
content: {}
});
msg.parent_header = { session: kernel.clientId };
tester.send(msg);
await emission;
});
it('should be emitted for an iopub message', async () => {
const kernel = await tester.start();
const msgId = 'idle status';
const emission = testEmission(kernel.anyMessage, {
test: (k, args) => {
expect((args.msg.header as any).msg_id).to.equal(msgId);
expect(args.direction).to.equal('recv');
}
});
tester.sendStatus(msgId, 'idle');
await emission;
});
it('should be emitted for an stdin message', async () => {
const kernel = await tester.start();
const emission = testEmission(kernel.anyMessage, {
test: (k, { msg, direction }) => {
if (!KernelMessage.isInputReplyMsg(msg)) {
throw new Error('Unexpected message');
}
if (msg.content.status !== 'ok') {
throw new Error('Message has been changed');
}
expect(msg.content.value).to.equal('foo');
expect(direction).to.equal('send');
}
});
kernel.sendInputReply({ status: 'ok', value: 'foo' });
await emission;
});
});
describe('#id', () => {
it('should be a string', () => {
expect(typeof defaultKernel.id).to.equal('string');
});
});
describe('#name', () => {
it('should be a string', () => {
expect(typeof defaultKernel.name).to.equal('string');
});
});
describe('#model', () => {
it('should be an IModel', () => {
const model = defaultKernel.model;
expect(typeof model.name).to.equal('string');
expect(typeof model.id).to.equal('string');
});
});
describe('#username', () => {
it('should be a string', () => {
expect(typeof defaultKernel.username).to.equal('string');
});
});
describe('#serverSettings', () => {
it('should be the server settings', () => {
expect(defaultKernel.serverSettings.baseUrl).to.equal(
PageConfig.getBaseUrl()
);
});
});
describe('#clientId', () => {
it('should be a string', () => {
expect(typeof defaultKernel.clientId).to.equal('string');
});
});
describe('#status', () => {
it('should get an idle status', async () => {
const emission = testEmission(defaultKernel.statusChanged, {
find: () => defaultKernel.status === 'idle'
});
await defaultKernel.requestExecute({ code: 'a=1' }).done;
await emission;
});
it('should get a restarting status', async () => {
const emission = testEmission(defaultKernel.statusChanged, {
find: () => defaultKernel.status === 'restarting'
});
await defaultKernel.restart();
await emission;
});
it('should get a busy status', async () => {
const emission = testEmission(defaultKernel.statusChanged, {
find: () => defaultKernel.status === 'busy'
});
await defaultKernel.requestExecute({ code: 'a=1' }, true).done;
await emission;
});
it('should get a reconnecting status', async () => {
const tester = new KernelTester();
const kernel = await tester.start();
await kernel.ready;
const emission = testEmission(kernel.statusChanged, {
find: () => kernel.status === 'reconnecting'
});
await tester.close();
await emission;
tester.dispose();
});
it('should get a dead status', async () => {
const tester = new KernelTester();
const kernel = await tester.start();
await kernel.ready;
const dead = testEmission(kernel.statusChanged, {
find: () => kernel.status === 'dead'
});
tester.sendStatus(UUID.uuid4(), 'dead');
await dead;
tester.dispose();
});
it('should not emit an invalid status', async () => {
const tester = new KernelTester();
const kernel = await tester.start();
await kernel.ready;
const emission = testEmission(kernel.statusChanged, {
test: (k, status) => {
expect(status).to.equal('busy');
expect(kernel.status).to.equal('busy');
}
});
// This invalid status is not emitted.
tester.sendStatus(UUID.uuid4(), 'invalid-status' as Kernel.Status);
// This valid status is emitted.
tester.sendStatus(UUID.uuid4(), 'busy');
await emission;
tester.dispose();
});
});
describe('#info', () => {
it('should get the kernel info', () => {
const name = defaultKernel.info.language_info.name;
const defaultSpecs = specs.kernelspecs[specs.default];
expect(name).to.equal(defaultSpecs.language);
});
});
describe('#getSpec()', () => {
it('should resolve with the spec', async () => {
const spec = await defaultKernel.getSpec();
expect(spec.name).to.equal(specs.default);
});
});
describe('#isReady', () => {
it('should test whether the kernel is ready', async () => {
const kernel = await Kernel.startNew();
expect(kernel.isReady).to.equal(false);
await kernel.ready;
expect(kernel.isReady).to.equal(true);
await kernel.shutdown();
});
});
describe('#ready', () => {
it('should resolve when the kernel is ready', async () => {
await defaultKernel.ready;
});
});
describe('#isDisposed', () => {
it('should be true after we dispose of the kernel', () => {
const kernel = Kernel.connectTo(defaultKernel.model);
expect(kernel.isDisposed).to.equal(false);
kernel.dispose();
expect(kernel.isDisposed).to.equal(true);
});
it('should be safe to call multiple times', () => {
const kernel = Kernel.connectTo(defaultKernel.model);
expect(kernel.isDisposed).to.equal(false);
expect(kernel.isDisposed).to.equal(false);
kernel.dispose();
expect(kernel.isDisposed).to.equal(true);
expect(kernel.isDisposed).to.equal(true);
});
});
describe('#dispose()', () => {
it('should dispose of the resources held by the kernel', () => {
const kernel = Kernel.connectTo(defaultKernel.model);
const future = kernel.requestExecute({ code: 'foo' });
expect(future.isDisposed).to.equal(false);
kernel.dispose();
expect(future.isDisposed).to.equal(true);
});
it('should be safe to call twice', () => {
const kernel = Kernel.connectTo(defaultKernel.model);
const future = kernel.requestExecute({ code: 'foo' });
expect(future.isDisposed).to.equal(false);
kernel.dispose();
expect(future.isDisposed).to.equal(true);
expect(kernel.isDisposed).to.equal(true);
kernel.dispose();
expect(future.isDisposed).to.equal(true);
expect(kernel.isDisposed).to.equal(true);
});
});
describe('#sendShellMessage()', () => {
let tester: KernelTester;
let kernel: Kernel.IKernel;
beforeEach(async () => {
tester = new KernelTester();
kernel = await tester.start();
});
afterEach(async () => {
await tester.shutdown();
tester.dispose();
});
it('should send a message to the kernel', async () => {
const done = new PromiseDelegate<void>();
const msgId = UUID.uuid4();
tester.onMessage(msg => {
try {
expect(msg.header.msg_id).to.equal(msgId);
} catch (e) {
done.reject(e);
throw e;
}
done.resolve();
});
const msg = KernelMessage.createMessage({
msgType: 'comm_info_request',
channel: 'shell',
username: kernel.username,
session: kernel.clientId,
msgId,
content: {}
});
kernel.sendShellMessage(msg, true);
await done.promise;
});
it('should send a binary message', async () => {
const done = new PromiseDelegate<void>();
const msgId = UUID.uuid4();
tester.onMessage(msg => {
try {
const decoder = new TextDecoder('utf8');
const item = msg.buffers[0] as DataView;
expect(decoder.decode(item)).to.equal('hello');
} catch (e) {
done.reject(e);
throw e;
}
done.resolve();
});
const encoder = new TextEncoder();
const data = encoder.encode('hello');
const msg = KernelMessage.createMessage({
msgType: 'comm_info_request',
channel: 'shell',
username: kernel.username,
session: kernel.clientId,
msgId,
content: {},
buffers: [data, data.buffer]
});
kernel.sendShellMessage(msg, true);
await done.promise;
});
it('should fail if the kernel is dead', async () => {
// Create a promise that resolves when the kernel's status changes to dead
const dead = testEmission(kernel.statusChanged, {
find: () => kernel.status === 'dead'
});
tester.sendStatus(UUID.uuid4(), 'dead');
await dead;
const msg = KernelMessage.createMessage({
msgType: 'kernel_info_request',
channel: 'shell',
username: kernel.username,
session: kernel.clientId,
content: {}
});
expect(() => {
kernel.sendShellMessage(msg, true);
expect(false).to.equal(true);
}).to.throw(/Kernel is dead/);
});
it('should handle out of order messages', async () => {
// This test that a future.done promise resolves when a status idle and
// reply come through, even if the status comes first.
const msg = KernelMessage.createMessage({
msgType: 'kernel_info_request',
channel: 'shell',
username: kernel.username,
session: kernel.clientId,
content: {}
});
const future = kernel.sendShellMessage(msg, true);
tester.onMessage(msg => {
// trigger onDone
tester.send(
KernelMessage.createMessage({
msgType: 'status',
channel: 'iopub',
username: kernel.username,
session: kernel.clientId,
content: { execution_state: 'idle' },
parentHeader: msg.header
})
);
future.onIOPub = () => {
tester.send(
KernelMessage.createMessage({
msgType: 'comm_open',
channel: 'shell',
username: kernel.username,
session: kernel.clientId,
content: {
comm_id: 'abcd',
target_name: 'target',
data: {}
},
parentHeader: msg.header
})
);
};
});
await future.done;
});
});
describe('#interrupt()', () => {
it('should interrupt and resolve with a valid server response', async () => {
const kernel = await Kernel.startNew();
await kernel.ready;
await kernel.interrupt();
await kernel.shutdown();
});
it('should throw an error for an invalid response', async () => {
handleRequest(defaultKernel, 200, {
id: defaultKernel.id,
name: defaultKernel.name
});
const interrupt = defaultKernel.interrupt();
await expectFailure(interrupt, 'Invalid response: 200 OK');
});
it('should throw an error for an error response', async () => {
handleRequest(defaultKernel, 500, {});
const interrupt = defaultKernel.interrupt();
await expectFailure(interrupt, '');
});
it('should fail if the kernel is dead', async () => {
const tester = new KernelTester();
const kernel = await tester.start();
// Create a promise that resolves when the kernel's status changes to dead
const dead = testEmission(kernel.statusChanged, {
find: () => kernel.status === 'dead'
});
tester.sendStatus(UUID.uuid4(), 'dead');
await dead;
await expectFailure(kernel.interrupt(), 'Kernel is dead');
tester.dispose();
});
});
describe('#restart()', () => {
it('should restart and resolve with a valid server response', async () => {
await defaultKernel.restart();
await defaultKernel.ready;
});
it('should fail if the kernel does not restart', async () => {
handleRequest(defaultKernel, 500, {});
const restart = defaultKernel.restart();
await expectFailure(restart, '');
});
it('should throw an error for an invalid response', async () => {
const kernel = defaultKernel;
handleRequest(kernel, 205, { id: kernel.id, name: kernel.name });
await expectFailure(
kernel.restart(),
'Invalid response: 205 Reset Content'
);
});
it('should throw an error for an error response', async () => {
handleRequest(defaultKernel, 500, {});
const restart = defaultKernel.restart();
await expectFailure(restart);
});
it('should throw an error for an invalid id', async () => {
handleRequest(defaultKernel, 200, {});
const restart = defaultKernel.restart();
await expectFailure(restart);
});
it('should dispose of existing comm and future objects', async () => {
const kernel = defaultKernel;
const comm = kernel.connectToComm('test');
const future = kernel.requestExecute({ code: 'foo' });
await kernel.restart();
await kernel.ready;
expect(future.isDisposed).to.equal(true);
expect(comm.isDisposed).to.equal(true);
});
});
describe('#reconnect()', () => {
it('should reconnect the websocket', () => {
return defaultKernel.reconnect();
});
it('should emit `"reconnecting"`, then `"connected"` status', async () => {
let connectedEmission: Promise<void>;
const emission = testEmission(defaultKernel.statusChanged, {
find: () => defaultKernel.status === 'reconnecting',
test: () => {
connectedEmission = testEmission(defaultKernel.statusChanged, {
find: () => defaultKernel.status === 'connected'
});
}
});
await defaultKernel.reconnect();
await emission;
await connectedEmission;
});
});
describe('#shutdown()', () => {
it('should shut down and resolve with a valid server response', async () => {
const kernel = await Kernel.startNew();
await kernel.ready;
await kernel.shutdown();
});
it('should throw an error for an invalid response', async () => {
handleRequest(defaultKernel, 200, {
id: UUID.uuid4(),
name: 'foo'
});
const shutdown = defaultKernel.shutdown();
await expectFailure(shutdown, 'Invalid response: 200 OK');
});
it('should handle a 404 error', async () => {
const kernel = await Kernel.startNew();
handleRequest(kernel, 404, {});
await kernel.shutdown();
});
it('should throw an error for an error response', async () => {
handleRequest(defaultKernel, 500, {});
const shutdown = defaultKernel.shutdown();
await expectFailure(shutdown, '');
});
it('should still pass if the kernel is dead', async () => {
const tester = new KernelTester();
const kernel = await tester.start();
// Create a promise that resolves when the kernel's status changes to dead
const dead = testEmission(kernel.statusChanged, {
find: () => kernel.status === 'dead'
});
tester.sendStatus(UUID.uuid4(), 'dead');
await dead;
await kernel.shutdown();
tester.dispose();
});
it('should dispose of all kernel instances', async () => {
const kernel0 = await Kernel.startNew();
const kernel1 = Kernel.connectTo(kernel0.model);
await kernel0.ready;
await kernel1.ready;
await kernel0.shutdown();
expect(kernel0.isDisposed).to.equal(true);
expect(kernel1.isDisposed).to.equal(true);
});
});
describe('#requestKernelInfo()', () => {
it('should resolve the promise', async () => {
const msg = await defaultKernel.requestKernelInfo();
if (msg.content.status !== 'ok') {
throw new Error('Message error');
}
const name = msg.content.language_info.name;
expect(name).to.be.ok;
});
});
describe('#requestComplete()', () => {
it('should resolve the promise', async () => {
const options: KernelMessage.ICompleteRequestMsg['content'] = {
code: 'hello',
cursor_pos: 4
};
await defaultKernel.requestComplete(options);
});
it('should reject the promise if the kernel is dead', async () => {
const options: KernelMessage.ICompleteRequestMsg['content'] = {
code: 'hello',
cursor_pos: 4
};
const tester = new KernelTester();
const kernel = await tester.start();
// Create a promise that resolves when the kernel's status changes to dead
const dead = testEmission(kernel.statusChanged, {
find: () => kernel.status === 'dead'
});
tester.sendStatus(UUID.uuid4(), 'dead');
await dead;
await expectFailure(kernel.requestComplete(options), 'Kernel is dead');
tester.dispose();
});
});
describe('#requestInspect()', () => {
it('should resolve the promise', async () => {
const options: KernelMessage.IInspectRequestMsg['content'] = {
code: 'hello',
cursor_pos: 4,
detail_level: 0
};
await defaultKernel.requestInspect(options);
});
});
describe('#requestIsComplete()', () => {
it('should resolve the promise', async () => {
const options: KernelMessage.IIsCompleteRequestMsg['content'] = {
code: 'hello'
};
await defaultKernel.requestIsComplete(options);
});
});
describe('#requestHistory()', () => {
it('range messages should resolve the promise', async () => {
const options: KernelMessage.IHistoryRequestMsg['content'] = {
output: true,
raw: true,
hist_access_type: 'range',
session: 0,
start: 1,
stop: 2
};
await defaultKernel.requestHistory(options);
});
it('tail messages should resolve the promise', async () => {
const options: KernelMessage.IHistoryRequestMsg['content'] = {
output: true,
raw: true,
hist_access_type: 'tail',
n: 1
};
await defaultKernel.requestHistory(options);
});
it('search messages should resolve the promise', async () => {
const options: KernelMessage.IHistoryRequestMsg['content'] = {
output: true,
raw: true,
hist_access_type: 'search',
n: 1,
pattern: '*',
unique: true
};
await defaultKernel.requestHistory(options);
});
});
describe('#sendInputReply()', () => {
it('should send an input_reply message', async () => {
const tester = new KernelTester();
const kernel = await tester.start();
const done = new PromiseDelegate<void>();
tester.onMessage(msg => {
expect(msg.header.msg_type).to.equal('input_reply');
done.resolve(null);
});
kernel.sendInputReply({ status: 'ok', value: 'test' });
await done.promise;
await tester.shutdown();
tester.dispose();
});
it('should fail if the kernel is dead', async () => {
const tester = new KernelTester();
const kernel = await tester.start();
// Create a promise that resolves when the kernel's status changes to dead
const dead = testEmission(kernel.statusChanged, {
find: () => kernel.status === 'dead'
});
tester.sendStatus(UUID.uuid4(), 'dead');
await dead;
expect(() => {
kernel.sendInputReply({ status: 'ok', value: 'test' });
}).to.throw(/Kernel is dead/);
tester.dispose();
});
});
describe('#requestExecute()', () => {
it('should send and handle incoming messages', async () => {
const content: KernelMessage.IExecuteRequestMsg['content'] = {
code: 'test',
silent: false,
store_history: true,
user_expressions: {},
allow_stdin: false,
stop_on_error: false
};
const options = {
username: defaultKernel.username,
session: defaultKernel.clientId
};
let future: Kernel.IShellFuture;
const tester = new KernelTester();
tester.onMessage(msg => {
expect(msg.channel).to.equal('shell');
// send a reply
tester.send(
KernelMessage.createMessage<KernelMessage.IExecuteReplyMsg>({
...options,
msgType: 'execute_reply',
channel: 'shell',
content: {
execution_count: 1,
status: 'ok',
user_expressions: {}
},
parentHeader: msg.header as KernelMessage.IExecuteRequestMsg['header']
})
);
future.onReply = () => {
// trigger onStdin
tester.send(
KernelMessage.createMessage({
...options,
channel: 'stdin',
msgType: 'input_request',
content: {
prompt: 'prompt',
password: false
},
parentHeader: msg.header
})
);
};
future.onStdin = () => {
// trigger onIOPub with a 'stream' message
tester.send(
KernelMessage.createMessage<KernelMessage.IStreamMsg>({
...options,
channel: 'iopub',
msgType: 'stream',
content: { name: 'stdout', text: '' },
parentHeader: msg.header
})
);
};
future.onIOPub = ioMsg => {
if (ioMsg.header.msg_type === 'stream') {
// trigger onDone
tester.send(
KernelMessage.createMessage<KernelMessage.IStatusMsg>({
...options,
channel: 'iopub',
msgType: 'status',
content: {
execution_state: 'idle'
},
parentHeader: msg.header
})
);
}
};
});
const kernel = await tester.start();
future = kernel.requestExecute(content);
await future.done;
expect(future.isDisposed).to.equal(true);
await tester.shutdown();
tester.dispose();
});
it('should not dispose of KernelFuture when disposeOnDone=false', async () => {
const options: KernelMessage.IExecuteRequestMsg['content'] = {
code: 'test',
silent: false,
store_history: true,
user_expressions: {},
allow_stdin: false,
stop_on_error: false
};
const future = defaultKernel.requestExecute(options, false);
await future.done;
expect(future.isDisposed).to.equal(false);
future.dispose();
expect(future.isDisposed).to.equal(true);
});
});
describe('#checkExecuteMetadata()', () => {
it('should accept cell metadata as part of request', async () => {
let options: KernelMessage.IExecuteRequestMsg['content'] = {
code: 'test',
silent: false,
store_history: true,
user_expressions: {},
allow_stdin: false,
stop_on_error: false
};
let metadata = { cellId: 'test' };
let future = defaultKernel.requestExecute(options, false, metadata);
await future.done;
expect((future.msg.metadata = metadata));
});
});
describe('#registerMessageHook()', () => {
it('should have the most recently registered hook run first', async () => {
const options: KernelMessage.IExecuteRequestMsg['content'] = {