ikernel.spec.ts 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { PageConfig } from '@jupyterlab/coreutils';
  4. import { UUID } from '@lumino/coreutils';
  5. import { PromiseDelegate } from '@lumino/coreutils';
  6. import {
  7. Kernel,
  8. KernelMessage,
  9. KernelSpec,
  10. KernelSpecAPI,
  11. KernelManager,
  12. KernelAPI
  13. } from '../../src';
  14. import {
  15. expectFailure,
  16. testEmission,
  17. JupyterServer,
  18. flakyIt as it
  19. } from '@jupyterlab/testutils';
  20. import { KernelTester, handleRequest } from '../utils';
  21. const server = new JupyterServer();
  22. beforeAll(async () => {
  23. await server.start();
  24. });
  25. afterAll(async () => {
  26. await server.shutdown();
  27. });
  28. describe('Kernel.IKernel', () => {
  29. let defaultKernel: Kernel.IKernelConnection;
  30. let specs: KernelSpec.ISpecModels;
  31. let kernelManager: KernelManager;
  32. beforeAll(async () => {
  33. jest.setTimeout(20000);
  34. kernelManager = new KernelManager();
  35. specs = await KernelSpecAPI.getSpecs();
  36. });
  37. beforeEach(async () => {
  38. defaultKernel = await kernelManager.startNew();
  39. await defaultKernel.info;
  40. });
  41. afterEach(async () => {
  42. await defaultKernel.shutdown();
  43. defaultKernel.dispose();
  44. });
  45. afterAll(async () => {
  46. const models = await KernelAPI.listRunning();
  47. await Promise.all(models.map(m => KernelAPI.shutdownKernel(m.id)));
  48. });
  49. describe('#disposed', () => {
  50. it('should be emitted when the kernel is disposed', async () => {
  51. await defaultKernel.info;
  52. let called = false;
  53. defaultKernel.disposed.connect((sender, args) => {
  54. expect(sender).toBe(defaultKernel);
  55. expect(args).toBeUndefined();
  56. called = true;
  57. });
  58. defaultKernel.dispose();
  59. expect(called).toBe(true);
  60. });
  61. it('should be emitted when the kernel is shut down', async () => {
  62. await defaultKernel.info;
  63. let called = false;
  64. defaultKernel.disposed.connect((sender, args) => {
  65. expect(sender).toBe(defaultKernel);
  66. expect(args).toBeUndefined();
  67. called = true;
  68. });
  69. await defaultKernel.shutdown();
  70. expect(called).toBe(true);
  71. });
  72. });
  73. describe('#statusChanged', () => {
  74. it('should be a signal following the Kernel status', async () => {
  75. let called = false;
  76. defaultKernel.statusChanged.connect(() => {
  77. if (defaultKernel.status === 'busy') {
  78. called = true;
  79. }
  80. });
  81. await defaultKernel.requestExecute({ code: 'a=1' }, true).done;
  82. expect(called).toBe(true);
  83. });
  84. });
  85. describe('#iopubMessage', () => {
  86. it('should be emitted for an iopub message', async () => {
  87. let called = false;
  88. defaultKernel.iopubMessage.connect((k, msg) => {
  89. called = true;
  90. });
  91. await defaultKernel.requestExecute({ code: 'a=1' }, true).done;
  92. expect(called).toBe(true);
  93. });
  94. it('should be emitted regardless of the sender', async () => {
  95. const tester = new KernelTester();
  96. const kernel = await tester.start();
  97. const msgId = UUID.uuid4();
  98. const emission = testEmission(kernel.iopubMessage, {
  99. find: (k, msg) => msg.header.msg_id === msgId
  100. });
  101. const msg = KernelMessage.createMessage({
  102. msgType: 'status',
  103. channel: 'iopub',
  104. session: tester.serverSessionId,
  105. msgId,
  106. content: {
  107. execution_state: 'idle'
  108. }
  109. });
  110. tester.send(msg);
  111. await emission;
  112. await tester.shutdown();
  113. tester.dispose();
  114. });
  115. });
  116. describe('#unhandledMessage', () => {
  117. let tester: KernelTester;
  118. beforeEach(() => {
  119. tester = new KernelTester();
  120. });
  121. afterEach(async () => {
  122. await tester.shutdown();
  123. tester.dispose();
  124. });
  125. it('should be emitted for an unhandled message', async () => {
  126. const kernel = await tester.start();
  127. const msgId = UUID.uuid4();
  128. const emission = testEmission(kernel.unhandledMessage, {
  129. find: (k, msg) => msg.header.msg_id === msgId
  130. });
  131. const msg = KernelMessage.createMessage({
  132. msgType: 'kernel_info_request',
  133. channel: 'shell',
  134. session: tester.serverSessionId,
  135. msgId,
  136. content: {}
  137. });
  138. msg.parent_header = { session: kernel.clientId };
  139. tester.send(msg);
  140. await emission;
  141. });
  142. it('should not be emitted for an iopub signal', async () => {
  143. const kernel = await tester.start();
  144. // We'll send two messages, first an iopub message, then a shell message.
  145. // The unhandledMessage signal should only emit once for the shell message.
  146. const msgId = UUID.uuid4();
  147. const emission = testEmission(kernel.unhandledMessage, {
  148. test: (k, msg) => {
  149. expect(msg.header.msg_id).toBe(msgId);
  150. }
  151. });
  152. // Send an iopub message.
  153. tester.sendStatus(UUID.uuid4(), 'idle');
  154. // Send a shell message.
  155. const msg = KernelMessage.createMessage({
  156. msgType: 'kernel_info_request',
  157. channel: 'shell',
  158. session: tester.serverSessionId,
  159. msgId,
  160. content: {}
  161. });
  162. msg.parent_header = { session: kernel.clientId };
  163. tester.send(msg);
  164. await emission;
  165. });
  166. it('should not be emitted for a different client session', async () => {
  167. const kernel = await tester.start();
  168. // We'll send two messages, first a message with a different session, then
  169. // one with the current client session. The unhandledMessage signal should
  170. // only emit once for the current session message.
  171. const msgId = 'message from right session';
  172. const emission = testEmission(kernel.unhandledMessage, {
  173. test: (k, msg) => {
  174. expect((msg.parent_header as KernelMessage.IHeader).session).toBe(
  175. kernel.clientId
  176. );
  177. expect(msg.header.msg_id).toBe(msgId);
  178. }
  179. });
  180. // Send a shell message with the wrong client (parent) session.
  181. const msg1 = KernelMessage.createMessage({
  182. msgType: 'kernel_info_request',
  183. channel: 'shell',
  184. session: tester.serverSessionId,
  185. msgId: 'message from wrong session',
  186. content: {}
  187. });
  188. msg1.parent_header = { session: 'wrong session' };
  189. tester.send(msg1);
  190. // Send a shell message with the right client (parent) session.
  191. const msg2 = KernelMessage.createMessage({
  192. msgType: 'kernel_info_request',
  193. channel: 'shell',
  194. session: tester.serverSessionId,
  195. msgId: msgId,
  196. content: {}
  197. });
  198. msg2.parent_header = { session: kernel.clientId };
  199. tester.send(msg2);
  200. await emission;
  201. });
  202. });
  203. describe('#anyMessage', () => {
  204. let tester: KernelTester;
  205. beforeEach(() => {
  206. tester = new KernelTester();
  207. });
  208. afterEach(async () => {
  209. await tester.shutdown();
  210. tester.dispose();
  211. });
  212. it('should be emitted for an unhandled message', async () => {
  213. const kernel = await tester.start();
  214. const msgId = UUID.uuid4();
  215. const emission = testEmission(kernel.anyMessage, {
  216. test: (k, args) => {
  217. expect(args.msg.header.msg_id).toBe(msgId);
  218. expect(args.msg.header.msg_type).toBe('kernel_info_request');
  219. expect(args.direction).toBe('recv');
  220. }
  221. });
  222. const msg = KernelMessage.createMessage({
  223. msgType: 'kernel_info_request',
  224. channel: 'shell',
  225. session: tester.serverSessionId,
  226. msgId,
  227. content: {}
  228. });
  229. msg.parent_header = { session: kernel.clientId };
  230. tester.send(msg);
  231. await emission;
  232. });
  233. it('should be emitted for an iopub message', async () => {
  234. const kernel = await tester.start();
  235. const msgId = 'idle status';
  236. const emission = testEmission(kernel.anyMessage, {
  237. test: (k, args) => {
  238. expect((args.msg.header as any).msg_id).toBe(msgId);
  239. expect(args.direction).toBe('recv');
  240. }
  241. });
  242. tester.sendStatus(msgId, 'idle');
  243. await emission;
  244. });
  245. it('should be emitted for an stdin message', async () => {
  246. const kernel = await tester.start();
  247. const emission = testEmission(kernel.anyMessage, {
  248. test: (k, { msg, direction }) => {
  249. if (!KernelMessage.isInputReplyMsg(msg)) {
  250. throw new Error('Unexpected message');
  251. }
  252. if (msg.content.status !== 'ok') {
  253. throw new Error('Message has been changed');
  254. }
  255. expect(msg.content.value).toBe('foo');
  256. expect(direction).toBe('send');
  257. }
  258. });
  259. kernel.sendInputReply({ status: 'ok', value: 'foo' });
  260. await emission;
  261. });
  262. });
  263. describe('#id', () => {
  264. it('should be a string', () => {
  265. expect(typeof defaultKernel.id).toBe('string');
  266. });
  267. });
  268. describe('#name', () => {
  269. it('should be a string', () => {
  270. expect(typeof defaultKernel.name).toBe('string');
  271. });
  272. });
  273. describe('#username', () => {
  274. it('should be a string', () => {
  275. expect(typeof defaultKernel.username).toBe('string');
  276. });
  277. });
  278. describe('#serverSettings', () => {
  279. it('should be the server settings', () => {
  280. expect(defaultKernel.serverSettings.baseUrl).toBe(
  281. PageConfig.getBaseUrl()
  282. );
  283. });
  284. });
  285. describe('#clientId', () => {
  286. it('should be a string', () => {
  287. expect(typeof defaultKernel.clientId).toBe('string');
  288. });
  289. });
  290. describe('#status', () => {
  291. beforeEach(async () => {
  292. await defaultKernel.info;
  293. });
  294. it('should get an idle status', async () => {
  295. const emission = testEmission(defaultKernel.statusChanged, {
  296. find: () => defaultKernel.status === 'idle'
  297. });
  298. await defaultKernel.requestExecute({ code: 'a=1' }).done;
  299. await emission;
  300. });
  301. it('should get a restarting status', async () => {
  302. const emission = testEmission(defaultKernel.statusChanged, {
  303. find: () => defaultKernel.status === 'restarting'
  304. });
  305. await defaultKernel.restart();
  306. await emission;
  307. });
  308. it('should get a busy status', async () => {
  309. const emission = testEmission(defaultKernel.statusChanged, {
  310. find: () => defaultKernel.status === 'busy'
  311. });
  312. await defaultKernel.requestExecute({ code: 'a=1' }, true).done;
  313. await emission;
  314. });
  315. it('should get an unknown status while disconnected', async () => {
  316. const tester = new KernelTester();
  317. const kernel = await tester.start();
  318. const emission = testEmission(kernel.statusChanged, {
  319. find: () => kernel.status === 'unknown'
  320. });
  321. await tester.close();
  322. await emission;
  323. tester.dispose();
  324. });
  325. it('should get a dead status', async () => {
  326. const tester = new KernelTester();
  327. const kernel = await tester.start();
  328. await kernel.info;
  329. const dead = testEmission(kernel.statusChanged, {
  330. find: () => kernel.status === 'dead'
  331. });
  332. tester.sendStatus(UUID.uuid4(), 'dead');
  333. await dead;
  334. tester.dispose();
  335. });
  336. });
  337. describe('#info', () => {
  338. it('should get the kernel info', async () => {
  339. const name = (await defaultKernel.info).language_info.name;
  340. const defaultSpecs = specs.kernelspecs[specs.default]!;
  341. expect(name).toBe(defaultSpecs.language);
  342. });
  343. });
  344. describe('#spec', () => {
  345. it('should resolve with the spec', async () => {
  346. const spec = await defaultKernel.spec;
  347. expect(spec!.name).toBe(specs.default);
  348. });
  349. });
  350. describe('#isDisposed', () => {
  351. it('should be true after we dispose of the kernel', async () => {
  352. const kernel = defaultKernel.clone();
  353. expect(kernel.isDisposed).toBe(false);
  354. kernel.dispose();
  355. expect(kernel.isDisposed).toBe(true);
  356. });
  357. it('should be safe to call multiple times', async () => {
  358. const kernel = defaultKernel.clone();
  359. expect(kernel.isDisposed).toBe(false);
  360. expect(kernel.isDisposed).toBe(false);
  361. kernel.dispose();
  362. expect(kernel.isDisposed).toBe(true);
  363. expect(kernel.isDisposed).toBe(true);
  364. });
  365. });
  366. describe('#dispose()', () => {
  367. it('should dispose of the resources held by the kernel', async () => {
  368. const kernel = defaultKernel.clone();
  369. const future = kernel.requestExecute({ code: 'foo' });
  370. expect(future.isDisposed).toBe(false);
  371. kernel.dispose();
  372. expect(future.isDisposed).toBe(true);
  373. });
  374. it('should be safe to call twice', async () => {
  375. const kernel = defaultKernel.clone();
  376. const future = kernel.requestExecute({ code: 'foo' });
  377. expect(future.isDisposed).toBe(false);
  378. kernel.dispose();
  379. expect(future.isDisposed).toBe(true);
  380. expect(kernel.isDisposed).toBe(true);
  381. kernel.dispose();
  382. expect(future.isDisposed).toBe(true);
  383. expect(kernel.isDisposed).toBe(true);
  384. });
  385. });
  386. describe('#sendShellMessage()', () => {
  387. let tester: KernelTester;
  388. let kernel: Kernel.IKernelConnection;
  389. beforeEach(async () => {
  390. tester = new KernelTester();
  391. kernel = await tester.start();
  392. });
  393. afterEach(async () => {
  394. await tester.shutdown();
  395. tester.dispose();
  396. });
  397. it('should send a message to the kernel', async () => {
  398. const done = new PromiseDelegate<void>();
  399. const msgId = UUID.uuid4();
  400. tester.onMessage(msg => {
  401. try {
  402. expect(msg.header.msg_id).toBe(msgId);
  403. } catch (e) {
  404. done.reject(e);
  405. throw e;
  406. }
  407. done.resolve();
  408. });
  409. const msg = KernelMessage.createMessage({
  410. msgType: 'comm_info_request',
  411. channel: 'shell',
  412. username: kernel.username,
  413. session: kernel.clientId,
  414. msgId,
  415. content: {}
  416. });
  417. kernel.sendShellMessage(msg, true);
  418. await done.promise;
  419. });
  420. it('should send a binary message', async () => {
  421. const done = new PromiseDelegate<void>();
  422. const msgId = UUID.uuid4();
  423. tester.onMessage(msg => {
  424. try {
  425. const decoder = new TextDecoder('utf8');
  426. const item = msg.buffers![0] as DataView;
  427. expect(decoder.decode(item)).toBe('hello');
  428. } catch (e) {
  429. done.reject(e);
  430. throw e;
  431. }
  432. done.resolve();
  433. });
  434. const encoder = new TextEncoder();
  435. const data = encoder.encode('hello');
  436. const msg = KernelMessage.createMessage({
  437. msgType: 'comm_info_request',
  438. channel: 'shell',
  439. username: kernel.username,
  440. session: kernel.clientId,
  441. msgId,
  442. content: {},
  443. buffers: [data, data.buffer]
  444. });
  445. kernel.sendShellMessage(msg, true);
  446. await done.promise;
  447. });
  448. it('should fail if the kernel is dead', async () => {
  449. // Create a promise that resolves when the kernel's status changes to dead
  450. const dead = testEmission(kernel.statusChanged, {
  451. find: () => kernel.status === 'dead'
  452. });
  453. tester.sendStatus(UUID.uuid4(), 'dead');
  454. await dead;
  455. expect(kernel.status).toBe('dead');
  456. const msg = KernelMessage.createMessage({
  457. msgType: 'kernel_info_request',
  458. channel: 'shell',
  459. username: kernel.username,
  460. session: kernel.clientId,
  461. content: {}
  462. });
  463. expect(() => {
  464. kernel.sendShellMessage(msg, true);
  465. }).toThrowError(/Kernel is dead/);
  466. });
  467. it('should handle out of order messages', async () => {
  468. // This test that a future.done promise resolves when a status idle and
  469. // reply come through, even if the status comes first.
  470. const msg = KernelMessage.createMessage({
  471. msgType: 'kernel_info_request',
  472. channel: 'shell',
  473. username: kernel.username,
  474. session: kernel.clientId,
  475. content: {}
  476. });
  477. const future = kernel.sendShellMessage(msg, true);
  478. tester.onMessage(msg => {
  479. // trigger onDone
  480. tester.send(
  481. KernelMessage.createMessage({
  482. msgType: 'status',
  483. channel: 'iopub',
  484. username: kernel.username,
  485. session: kernel.clientId,
  486. content: { execution_state: 'idle' },
  487. parentHeader: msg.header
  488. })
  489. );
  490. future.onIOPub = () => {
  491. tester.send(
  492. KernelMessage.createMessage({
  493. msgType: 'comm_open',
  494. channel: 'shell',
  495. username: kernel.username,
  496. session: kernel.clientId,
  497. content: {
  498. comm_id: 'abcd',
  499. target_name: 'target',
  500. data: {}
  501. },
  502. parentHeader: msg.header
  503. })
  504. );
  505. };
  506. });
  507. await future.done;
  508. });
  509. });
  510. describe('#interrupt()', () => {
  511. it('should interrupt and resolve with a valid server response', async () => {
  512. const kernel = await kernelManager.startNew();
  513. await kernel.interrupt();
  514. await kernel.shutdown();
  515. });
  516. it('should throw an error for an invalid response', async () => {
  517. handleRequest(defaultKernel, 200, {
  518. id: defaultKernel.id,
  519. name: defaultKernel.name
  520. });
  521. const interrupt = defaultKernel.interrupt();
  522. await expectFailure(interrupt, 'Invalid response: 200 OK');
  523. });
  524. it('should throw an error for an error response', async () => {
  525. handleRequest(defaultKernel, 500, {});
  526. const interrupt = defaultKernel.interrupt();
  527. await expectFailure(interrupt, '');
  528. });
  529. it('should fail if the kernel is dead', async () => {
  530. const tester = new KernelTester();
  531. const kernel = await tester.start();
  532. // Create a promise that resolves when the kernel's status changes to dead
  533. const dead = testEmission(kernel.statusChanged, {
  534. find: () => kernel.status === 'dead'
  535. });
  536. tester.sendStatus(UUID.uuid4(), 'dead');
  537. await dead;
  538. await expectFailure(kernel.interrupt(), 'Kernel is dead');
  539. tester.dispose();
  540. });
  541. });
  542. describe('#restart()', () => {
  543. beforeEach(async () => {
  544. await defaultKernel.info;
  545. });
  546. it('should restart and resolve with a valid server response', async () => {
  547. await defaultKernel.restart();
  548. });
  549. it('should fail if the kernel does not restart', async () => {
  550. handleRequest(defaultKernel, 500, {});
  551. const restart = defaultKernel.restart();
  552. await expectFailure(restart, '');
  553. });
  554. it('should throw an error for an invalid response', async () => {
  555. const { id, name } = defaultKernel;
  556. handleRequest(defaultKernel, 205, { id, name });
  557. await expectFailure(
  558. defaultKernel.restart(),
  559. 'Invalid response: 205 Reset Content'
  560. );
  561. });
  562. it('should throw an error for an error response', async () => {
  563. handleRequest(defaultKernel, 500, {});
  564. const restart = defaultKernel.restart();
  565. await expectFailure(restart);
  566. });
  567. it('should throw an error for an invalid id', async () => {
  568. handleRequest(defaultKernel, 200, {});
  569. const restart = defaultKernel.restart();
  570. await expectFailure(restart);
  571. });
  572. it('should dispose of existing comm and future objects', async () => {
  573. const comm = defaultKernel.createComm('test');
  574. const future = defaultKernel.requestExecute({ code: 'foo' });
  575. await defaultKernel.restart();
  576. await defaultKernel.info;
  577. expect(future.isDisposed).toBe(true);
  578. expect(comm.isDisposed).toBe(true);
  579. });
  580. });
  581. describe('#reconnect()', () => {
  582. it('should create a new websocket and resolve the returned promise', async () => {
  583. const oldWS = (defaultKernel as any)._ws;
  584. await defaultKernel.reconnect();
  585. expect((defaultKernel as any)._ws).not.toBe(oldWS);
  586. });
  587. it('should emit `"connecting"`, then `"connected"` status', async () => {
  588. const emission = testEmission(defaultKernel.connectionStatusChanged, {
  589. find: () => defaultKernel.connectionStatus === 'connecting',
  590. test: async () => {
  591. await testEmission(defaultKernel.connectionStatusChanged, {
  592. find: () => defaultKernel.connectionStatus === 'connected'
  593. });
  594. }
  595. });
  596. await defaultKernel.reconnect();
  597. await emission;
  598. });
  599. it('return promise should reject if the kernel is disposed or disconnected', async () => {
  600. const connection = defaultKernel.reconnect();
  601. defaultKernel.dispose();
  602. try {
  603. await connection;
  604. // If the connection did not reject, so test fails.
  605. throw new Error('Reconnection promise did not reject');
  606. } catch (e) {
  607. /* Connection promise reject - test passes */
  608. }
  609. });
  610. });
  611. describe('#shutdown()', () => {
  612. it('should shut down and resolve with a valid server response', async () => {
  613. const kernel = await kernelManager.startNew();
  614. await kernel.shutdown();
  615. });
  616. it('should throw an error for an invalid response', async () => {
  617. handleRequest(defaultKernel, 200, {
  618. id: UUID.uuid4(),
  619. name: 'foo'
  620. });
  621. const shutdown = defaultKernel.shutdown();
  622. await expectFailure(shutdown, 'Invalid response: 200 OK');
  623. });
  624. it('should handle a 404 error', async () => {
  625. const kernel = await kernelManager.startNew();
  626. handleRequest(kernel, 404, {});
  627. await kernel.shutdown();
  628. });
  629. it('should throw an error for an error response', async () => {
  630. handleRequest(defaultKernel, 500, {});
  631. const shutdown = defaultKernel.shutdown();
  632. await expectFailure(shutdown, '');
  633. });
  634. it('should still pass if the kernel is dead', async () => {
  635. const tester = new KernelTester();
  636. const kernel = await tester.start();
  637. // Create a promise that resolves when the kernel's status changes to dead
  638. const dead = testEmission(kernel.statusChanged, {
  639. find: () => kernel.status === 'dead'
  640. });
  641. tester.sendStatus(UUID.uuid4(), 'dead');
  642. await dead;
  643. await kernel.shutdown();
  644. tester.dispose();
  645. });
  646. });
  647. describe('#requestKernelInfo()', () => {
  648. it('should resolve the promise', async () => {
  649. const msg = (await defaultKernel.requestKernelInfo())!;
  650. if (msg.content.status !== 'ok') {
  651. throw new Error('Message error');
  652. }
  653. const name = msg.content.language_info.name;
  654. expect(name).toBeTruthy();
  655. });
  656. });
  657. describe('#requestComplete()', () => {
  658. it('should resolve the promise', async () => {
  659. const options: KernelMessage.ICompleteRequestMsg['content'] = {
  660. code: 'hello',
  661. cursor_pos: 4
  662. };
  663. await defaultKernel.requestComplete(options);
  664. });
  665. it('should reject the promise if the kernel is dead', async () => {
  666. const options: KernelMessage.ICompleteRequestMsg['content'] = {
  667. code: 'hello',
  668. cursor_pos: 4
  669. };
  670. const tester = new KernelTester();
  671. const kernel = await tester.start();
  672. // Create a promise that resolves when the kernel's status changes to dead
  673. const dead = testEmission(kernel.statusChanged, {
  674. find: () => kernel.status === 'dead'
  675. });
  676. tester.sendStatus(UUID.uuid4(), 'dead');
  677. await dead;
  678. await expectFailure(kernel.requestComplete(options), 'Kernel is dead');
  679. tester.dispose();
  680. });
  681. });
  682. describe('#requestInspect()', () => {
  683. it('should resolve the promise', async () => {
  684. const options: KernelMessage.IInspectRequestMsg['content'] = {
  685. code: 'hello',
  686. cursor_pos: 4,
  687. detail_level: 0
  688. };
  689. await defaultKernel.requestInspect(options);
  690. });
  691. });
  692. describe('#requestIsComplete()', () => {
  693. it('should resolve the promise', async () => {
  694. const options: KernelMessage.IIsCompleteRequestMsg['content'] = {
  695. code: 'hello'
  696. };
  697. await defaultKernel.requestIsComplete(options);
  698. });
  699. });
  700. describe('#requestHistory()', () => {
  701. it('range messages should resolve the promise', async () => {
  702. const options: KernelMessage.IHistoryRequestMsg['content'] = {
  703. output: true,
  704. raw: true,
  705. hist_access_type: 'range',
  706. session: 0,
  707. start: 1,
  708. stop: 2
  709. };
  710. await defaultKernel.requestHistory(options);
  711. });
  712. it('tail messages should resolve the promise', async () => {
  713. const options: KernelMessage.IHistoryRequestMsg['content'] = {
  714. output: true,
  715. raw: true,
  716. hist_access_type: 'tail',
  717. n: 1
  718. };
  719. await defaultKernel.requestHistory(options);
  720. });
  721. it('search messages should resolve the promise', async () => {
  722. const options: KernelMessage.IHistoryRequestMsg['content'] = {
  723. output: true,
  724. raw: true,
  725. hist_access_type: 'search',
  726. n: 1,
  727. pattern: '*',
  728. unique: true
  729. };
  730. await defaultKernel.requestHistory(options);
  731. });
  732. });
  733. describe('#sendInputReply()', () => {
  734. it('should send an input_reply message', async () => {
  735. const tester = new KernelTester();
  736. const kernel = await tester.start();
  737. const done = new PromiseDelegate<void>();
  738. tester.onMessage(msg => {
  739. expect(msg.header.msg_type).toBe('input_reply');
  740. done.resolve(undefined);
  741. });
  742. kernel.sendInputReply({ status: 'ok', value: 'test' });
  743. await done.promise;
  744. await tester.shutdown();
  745. tester.dispose();
  746. });
  747. it('should fail if the kernel is dead', async () => {
  748. const tester = new KernelTester();
  749. const kernel = await tester.start();
  750. // Create a promise that resolves when the kernel's status changes to dead
  751. const dead = testEmission(kernel.statusChanged, {
  752. find: () => kernel.status === 'dead'
  753. });
  754. tester.sendStatus(UUID.uuid4(), 'dead');
  755. await dead;
  756. expect(() => {
  757. kernel.sendInputReply({ status: 'ok', value: 'test' });
  758. }).toThrowError(/Kernel is dead/);
  759. tester.dispose();
  760. });
  761. });
  762. describe('#requestExecute()', () => {
  763. it('should send and handle incoming messages', async () => {
  764. const content: KernelMessage.IExecuteRequestMsg['content'] = {
  765. code: 'test',
  766. silent: false,
  767. store_history: true,
  768. user_expressions: {},
  769. allow_stdin: false,
  770. stop_on_error: false
  771. };
  772. const options = {
  773. username: defaultKernel.username,
  774. session: defaultKernel.clientId
  775. };
  776. let future: Kernel.IShellFuture;
  777. const tester = new KernelTester();
  778. tester.onMessage(msg => {
  779. expect(msg.channel).toBe('shell');
  780. // send a reply
  781. tester.send(
  782. KernelMessage.createMessage<KernelMessage.IExecuteReplyMsg>({
  783. ...options,
  784. msgType: 'execute_reply',
  785. channel: 'shell',
  786. content: {
  787. execution_count: 1,
  788. status: 'ok',
  789. user_expressions: {}
  790. },
  791. parentHeader: msg.header as KernelMessage.IExecuteRequestMsg['header']
  792. })
  793. );
  794. future.onReply = () => {
  795. // trigger onStdin
  796. tester.send(
  797. KernelMessage.createMessage({
  798. ...options,
  799. channel: 'stdin',
  800. msgType: 'input_request',
  801. content: {
  802. prompt: 'prompt',
  803. password: false
  804. },
  805. parentHeader: msg.header
  806. })
  807. );
  808. };
  809. future.onStdin = () => {
  810. // trigger onIOPub with a 'stream' message
  811. tester.send(
  812. KernelMessage.createMessage<KernelMessage.IStreamMsg>({
  813. ...options,
  814. channel: 'iopub',
  815. msgType: 'stream',
  816. content: { name: 'stdout', text: '' },
  817. parentHeader: msg.header
  818. })
  819. );
  820. };
  821. future.onIOPub = ioMsg => {
  822. if (ioMsg.header.msg_type === 'stream') {
  823. // trigger onDone
  824. tester.send(
  825. KernelMessage.createMessage<KernelMessage.IStatusMsg>({
  826. ...options,
  827. channel: 'iopub',
  828. msgType: 'status',
  829. content: {
  830. execution_state: 'idle'
  831. },
  832. parentHeader: msg.header
  833. })
  834. );
  835. }
  836. };
  837. });
  838. const kernel = await tester.start();
  839. future = kernel.requestExecute(content);
  840. await future.done;
  841. expect(future.isDisposed).toBe(true);
  842. await tester.shutdown();
  843. tester.dispose();
  844. });
  845. it('should not dispose of KernelFuture when disposeOnDone=false', async () => {
  846. const options: KernelMessage.IExecuteRequestMsg['content'] = {
  847. code: 'test',
  848. silent: false,
  849. store_history: true,
  850. user_expressions: {},
  851. allow_stdin: false,
  852. stop_on_error: false
  853. };
  854. const future = defaultKernel.requestExecute(options, false);
  855. await future.done;
  856. expect(future.isDisposed).toBe(false);
  857. future.dispose();
  858. expect(future.isDisposed).toBe(true);
  859. });
  860. });
  861. describe('#checkExecuteMetadata()', () => {
  862. it('should accept cell metadata as part of request', async () => {
  863. const options: KernelMessage.IExecuteRequestMsg['content'] = {
  864. code: 'test',
  865. silent: false,
  866. store_history: true,
  867. user_expressions: {},
  868. allow_stdin: false,
  869. stop_on_error: false
  870. };
  871. const metadata = { cellId: 'test' };
  872. const future = defaultKernel.requestExecute(options, false, metadata);
  873. await future.done;
  874. expect(future.msg.metadata).toEqual(metadata);
  875. });
  876. });
  877. describe('#registerMessageHook()', () => {
  878. it('should have the most recently registered hook run first', async () => {
  879. const options: KernelMessage.IExecuteRequestMsg['content'] = {
  880. code: 'test',
  881. silent: false,
  882. store_history: true,
  883. user_expressions: {},
  884. allow_stdin: false,
  885. stop_on_error: false
  886. };
  887. const calls: string[] = [];
  888. let future: Kernel.IShellFuture;
  889. let kernel: Kernel.IKernelConnection;
  890. const tester = new KernelTester();
  891. tester.onMessage(message => {
  892. // send a reply
  893. const parentHeader = message.header;
  894. const session = 'session';
  895. tester.send(
  896. KernelMessage.createMessage({
  897. parentHeader,
  898. session,
  899. channel: 'shell',
  900. msgType: 'comm_open',
  901. content: { comm_id: 'B', data: {}, target_name: 'C' }
  902. })
  903. );
  904. future.onReply = () => {
  905. // trigger onIOPub with a 'stream' message
  906. tester.send(
  907. KernelMessage.createMessage({
  908. parentHeader,
  909. session,
  910. channel: 'iopub',
  911. msgType: 'stream',
  912. content: { name: 'stdout', text: 'foo' }
  913. })
  914. );
  915. // trigger onDone
  916. tester.send(
  917. KernelMessage.createMessage({
  918. parentHeader,
  919. session,
  920. channel: 'iopub',
  921. msgType: 'status',
  922. content: { execution_state: 'idle' }
  923. })
  924. );
  925. };
  926. kernel.registerMessageHook(parentHeader.msg_id, async msg => {
  927. // Make this hook call asynchronous
  928. // tslint:disable-next-line:await-promise
  929. await calls.push('last');
  930. return true;
  931. });
  932. kernel.registerMessageHook(parentHeader.msg_id, msg => {
  933. calls.push('first');
  934. // not returning should also continue handling
  935. return void 0 as any;
  936. });
  937. future.onIOPub = () => {
  938. calls.push('iopub');
  939. };
  940. });
  941. kernel = await tester.start();
  942. future = kernel.requestExecute(options, false);
  943. await future.done;
  944. // the last hook was called for the stream and the status message.
  945. expect(calls).toEqual([
  946. 'first',
  947. 'last',
  948. 'iopub',
  949. 'first',
  950. 'last',
  951. 'iopub'
  952. ]);
  953. await tester.shutdown();
  954. tester.dispose();
  955. });
  956. it('should abort processing if a hook returns false, but the done logic should still work', async () => {
  957. const options: KernelMessage.IExecuteRequestMsg['content'] = {
  958. code: 'test',
  959. silent: false,
  960. store_history: true,
  961. user_expressions: {},
  962. allow_stdin: false,
  963. stop_on_error: false
  964. };
  965. const calls: string[] = [];
  966. const tester = new KernelTester();
  967. let future: Kernel.IShellFuture;
  968. let kernel: Kernel.IKernelConnection;
  969. tester.onMessage(message => {
  970. // send a reply
  971. const parentHeader = message.header;
  972. const session = 'session';
  973. tester.send(
  974. KernelMessage.createMessage({
  975. parentHeader,
  976. session,
  977. channel: 'shell',
  978. msgType: 'comm_open',
  979. content: { comm_id: 'B', data: {}, target_name: 'C' }
  980. })
  981. );
  982. future.onReply = () => {
  983. // trigger onIOPub with a 'stream' message
  984. tester.send(
  985. KernelMessage.createMessage({
  986. parentHeader,
  987. session,
  988. channel: 'iopub',
  989. msgType: 'stream',
  990. content: { name: 'stdout', text: 'foo' }
  991. })
  992. );
  993. // trigger onDone
  994. tester.send(
  995. KernelMessage.createMessage({
  996. parentHeader,
  997. session,
  998. channel: 'iopub',
  999. msgType: 'status',
  1000. content: { execution_state: 'idle' }
  1001. })
  1002. );
  1003. };
  1004. kernel.registerMessageHook(parentHeader.msg_id, msg => {
  1005. calls.push('last');
  1006. return true;
  1007. });
  1008. kernel.registerMessageHook(parentHeader.msg_id, msg => {
  1009. calls.push('first');
  1010. return false;
  1011. });
  1012. future.onIOPub = async () => {
  1013. // tslint:disable-next-line:await-promise
  1014. await calls.push('iopub');
  1015. };
  1016. });
  1017. kernel = await tester.start();
  1018. future = kernel.requestExecute(options, false);
  1019. await future.done;
  1020. // the last hook was called for the stream and the status message.
  1021. expect(calls).toEqual(['first', 'first']);
  1022. await tester.shutdown();
  1023. tester.dispose();
  1024. });
  1025. it('should process additions on the next run', async () => {
  1026. const options: KernelMessage.IExecuteRequestMsg['content'] = {
  1027. code: 'test',
  1028. silent: false,
  1029. store_history: true,
  1030. user_expressions: {},
  1031. allow_stdin: false,
  1032. stop_on_error: false
  1033. };
  1034. const calls: string[] = [];
  1035. const tester = new KernelTester();
  1036. let future: Kernel.IShellFuture;
  1037. let kernel: Kernel.IKernelConnection;
  1038. tester.onMessage(message => {
  1039. // send a reply
  1040. const parentHeader = message.header;
  1041. const session = 'session';
  1042. tester.send(
  1043. KernelMessage.createMessage({
  1044. parentHeader,
  1045. session,
  1046. channel: 'shell',
  1047. msgType: 'comm_open',
  1048. content: { comm_id: 'B', data: {}, target_name: 'C' }
  1049. })
  1050. );
  1051. future.onReply = () => {
  1052. // trigger onIOPub with a 'stream' message
  1053. tester.send(
  1054. KernelMessage.createMessage({
  1055. parentHeader,
  1056. session,
  1057. channel: 'iopub',
  1058. msgType: 'stream',
  1059. content: { name: 'stdout', text: 'foo' }
  1060. })
  1061. );
  1062. // trigger onDone
  1063. tester.send(
  1064. KernelMessage.createMessage({
  1065. parentHeader,
  1066. session,
  1067. channel: 'iopub',
  1068. msgType: 'status',
  1069. content: { execution_state: 'idle' }
  1070. })
  1071. );
  1072. };
  1073. kernel.registerMessageHook(parentHeader.msg_id, msg => {
  1074. calls.push('last');
  1075. kernel.registerMessageHook(parentHeader.msg_id, msg => {
  1076. calls.push('first');
  1077. return true;
  1078. });
  1079. return true;
  1080. });
  1081. future.onIOPub = () => {
  1082. calls.push('iopub');
  1083. };
  1084. });
  1085. kernel = await tester.start();
  1086. future = kernel.requestExecute(options, false);
  1087. await future.done;
  1088. expect(calls).toEqual(['last', 'iopub', 'first', 'last', 'iopub']);
  1089. await tester.shutdown();
  1090. tester.dispose();
  1091. });
  1092. it('should deactivate a hook immediately on removal', async () => {
  1093. const options: KernelMessage.IExecuteRequestMsg['content'] = {
  1094. code: 'test',
  1095. silent: false,
  1096. store_history: true,
  1097. user_expressions: {},
  1098. allow_stdin: false,
  1099. stop_on_error: false
  1100. };
  1101. const calls: string[] = [];
  1102. const tester = new KernelTester();
  1103. let future: Kernel.IShellFuture;
  1104. let kernel: Kernel.IKernelConnection;
  1105. tester.onMessage(message => {
  1106. // send a reply
  1107. const parentHeader = message.header;
  1108. const session = 'session';
  1109. tester.send(
  1110. KernelMessage.createMessage({
  1111. parentHeader,
  1112. session,
  1113. channel: 'shell',
  1114. msgType: 'comm_open',
  1115. content: { comm_id: 'B', data: {}, target_name: 'C' }
  1116. })
  1117. );
  1118. future.onReply = () => {
  1119. // trigger onIOPub with a 'stream' message
  1120. tester.send(
  1121. KernelMessage.createMessage({
  1122. parentHeader,
  1123. session,
  1124. channel: 'iopub',
  1125. msgType: 'stream',
  1126. content: { name: 'stdout', text: 'foo' }
  1127. })
  1128. );
  1129. // trigger onDone
  1130. tester.send(
  1131. KernelMessage.createMessage({
  1132. parentHeader,
  1133. session,
  1134. channel: 'iopub',
  1135. msgType: 'status',
  1136. content: { execution_state: 'idle' }
  1137. })
  1138. );
  1139. };
  1140. const toDelete = (msg: KernelMessage.IIOPubMessage) => {
  1141. calls.push('delete');
  1142. return true;
  1143. };
  1144. kernel.registerMessageHook(parentHeader.msg_id, toDelete);
  1145. kernel.registerMessageHook(parentHeader.msg_id, msg => {
  1146. if (calls.length > 0) {
  1147. // delete the hook the second time around
  1148. kernel.removeMessageHook(parentHeader.msg_id, toDelete);
  1149. }
  1150. calls.push('first');
  1151. return true;
  1152. });
  1153. future.onIOPub = () => {
  1154. calls.push('iopub');
  1155. };
  1156. });
  1157. kernel = await tester.start();
  1158. future = kernel.requestExecute(options, false);
  1159. await future.done;
  1160. expect(calls).toEqual(['first', 'delete', 'iopub', 'first', 'iopub']);
  1161. await tester.shutdown();
  1162. tester.dispose();
  1163. });
  1164. });
  1165. describe('handles messages asynchronously', () => {
  1166. // TODO: Also check that messages are canceled appropriately. In particular, when
  1167. // a kernel is restarted, then a message is sent for a comm open from the
  1168. // old session, the comm open should be canceled.
  1169. it('should run handlers in order', async () => {
  1170. const options: KernelMessage.IExecuteRequestMsg['content'] = {
  1171. code: 'test',
  1172. silent: false,
  1173. store_history: true,
  1174. user_expressions: {},
  1175. allow_stdin: true,
  1176. stop_on_error: false
  1177. };
  1178. const tester = new KernelTester();
  1179. const kernel = await tester.start();
  1180. const future = kernel.requestExecute(options, false);
  1181. // The list of emissions from the anyMessage signal.
  1182. const msgSignal: string[][] = [];
  1183. const msgSignalExpected: string[][] = [];
  1184. // The list of message processing calls
  1185. const calls: string[][] = [];
  1186. const callsExpected: string[][] = [];
  1187. function pushIopub(msgId: string) {
  1188. callsExpected.push([msgId, 'future hook a']);
  1189. callsExpected.push([msgId, 'future hook b']);
  1190. callsExpected.push([msgId, 'kernel hook a']);
  1191. callsExpected.push([msgId, 'kernel hook b']);
  1192. callsExpected.push([msgId, 'iopub']);
  1193. msgSignalExpected.push([msgId, 'iopub']);
  1194. }
  1195. function pushCommOpen(msgId: string) {
  1196. pushIopub(msgId);
  1197. callsExpected.push([msgId, 'comm open']);
  1198. }
  1199. function pushCommMsg(msgId: string) {
  1200. pushIopub(msgId);
  1201. callsExpected.push([msgId, 'comm msg']);
  1202. }
  1203. function pushCommClose(msgId: string) {
  1204. pushIopub(msgId);
  1205. callsExpected.push([msgId, 'comm close']);
  1206. }
  1207. function pushStdin(msgId: string) {
  1208. callsExpected.push([msgId, 'stdin']);
  1209. msgSignalExpected.push([msgId, 'stdin']);
  1210. }
  1211. function pushReply(msgId: string) {
  1212. callsExpected.push([msgId, 'reply']);
  1213. msgSignalExpected.push([msgId, 'shell']);
  1214. }
  1215. const anyMessageDone = new PromiseDelegate();
  1216. const handlingBlock = new PromiseDelegate();
  1217. tester.onMessage(message => {
  1218. tester.onMessage(() => {
  1219. return;
  1220. });
  1221. tester.parentHeader = message.header;
  1222. pushIopub(tester.sendStatus('busy', 'busy'));
  1223. pushIopub(tester.sendStream('stdout', { name: 'stdout', text: 'foo' }));
  1224. pushCommOpen(
  1225. tester.sendCommOpen('comm open', {
  1226. target_name: 'commtarget',
  1227. comm_id: 'commid',
  1228. data: {}
  1229. })
  1230. );
  1231. pushIopub(
  1232. tester.sendDisplayData('display 1', { data: {}, metadata: {} })
  1233. );
  1234. pushCommMsg(
  1235. tester.sendCommMsg('comm 1', { comm_id: 'commid', data: {} })
  1236. );
  1237. pushCommMsg(
  1238. tester.sendCommMsg('comm 2', { comm_id: 'commid', data: {} })
  1239. );
  1240. pushCommClose(
  1241. tester.sendCommClose('comm close', { comm_id: 'commid', data: {} })
  1242. );
  1243. pushStdin(
  1244. tester.sendInputRequest('stdin', { prompt: '', password: false })
  1245. );
  1246. pushIopub(
  1247. tester.sendDisplayData('display 2', {
  1248. data: {},
  1249. metadata: {},
  1250. transient: { display_id: 'displayid' }
  1251. })
  1252. );
  1253. pushIopub(
  1254. tester.sendUpdateDisplayData('update display', {
  1255. data: {},
  1256. metadata: {},
  1257. transient: { display_id: 'displayid' }
  1258. })
  1259. );
  1260. pushIopub(
  1261. tester.sendExecuteResult('execute result', {
  1262. execution_count: 1,
  1263. data: {},
  1264. metadata: {}
  1265. })
  1266. );
  1267. pushIopub(tester.sendStatus('idle', 'idle'));
  1268. pushReply(
  1269. tester.sendExecuteReply('execute reply', {
  1270. status: 'ok',
  1271. execution_count: 1,
  1272. user_expressions: {}
  1273. })
  1274. );
  1275. tester.parentHeader = undefined;
  1276. });
  1277. kernel.anyMessage.connect((k, args) => {
  1278. msgSignal.push([args.msg.header.msg_id, args.msg.channel]);
  1279. if (args.msg.header.msg_id === 'execute reply') {
  1280. anyMessageDone.resolve(undefined);
  1281. }
  1282. });
  1283. kernel.registerMessageHook(future.msg.header.msg_id, async msg => {
  1284. // Make this hook call asynchronous
  1285. // tslint:disable-next-line:await-promise
  1286. await calls.push([msg.header.msg_id, 'kernel hook b']);
  1287. return true;
  1288. });
  1289. kernel.registerMessageHook(future.msg.header.msg_id, async msg => {
  1290. calls.push([msg.header.msg_id, 'kernel hook a']);
  1291. return true;
  1292. });
  1293. kernel.registerCommTarget('commtarget', async (comm, msg) => {
  1294. // tslint:disable-next-line:await-promise
  1295. await calls.push([msg.header.msg_id, 'comm open']);
  1296. comm.onMsg = async msg => {
  1297. // tslint:disable-next-line:await-promise
  1298. await calls.push([msg.header.msg_id, 'comm msg']);
  1299. };
  1300. comm.onClose = async msg => {
  1301. // tslint:disable-next-line:await-promise
  1302. await calls.push([msg.header.msg_id, 'comm close']);
  1303. };
  1304. });
  1305. future.registerMessageHook(async msg => {
  1306. // tslint:disable-next-line:await-promise
  1307. await calls.push([msg.header.msg_id, 'future hook b']);
  1308. return true;
  1309. });
  1310. future.registerMessageHook(async msg => {
  1311. // Delay processing until after we've checked the anyMessage results.
  1312. await handlingBlock.promise;
  1313. // tslint:disable-next-line:await-promise
  1314. await calls.push([msg.header.msg_id, 'future hook a']);
  1315. return true;
  1316. });
  1317. future.onIOPub = async msg => {
  1318. // tslint:disable-next-line:await-promise
  1319. await calls.push([msg.header.msg_id, 'iopub']);
  1320. };
  1321. future.onStdin = async msg => {
  1322. // tslint:disable-next-line:await-promise
  1323. await calls.push([msg.header.msg_id, 'stdin']);
  1324. };
  1325. future.onReply = async msg => {
  1326. // tslint:disable-next-line:await-promise
  1327. await calls.push([msg.header.msg_id, 'reply']);
  1328. };
  1329. // Give the kernel time to receive and queue up the messages.
  1330. await anyMessageDone.promise;
  1331. // At this point, the synchronous anyMessage signal should have been
  1332. // emitted for every message, but no actual message handling should have
  1333. // happened.
  1334. expect(msgSignal).toEqual(msgSignalExpected);
  1335. expect(calls).toEqual([]);
  1336. // Release the lock on message processing.
  1337. handlingBlock.resolve(undefined);
  1338. await future.done;
  1339. expect(calls).toEqual(callsExpected);
  1340. await tester.shutdown();
  1341. tester.dispose();
  1342. });
  1343. });
  1344. });