session.spec.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. expectFailure,
  5. flakyIt as it,
  6. JupyterServer
  7. } from '@jupyterlab/testutils';
  8. import { toArray } from '@lumino/algorithm';
  9. import { UUID } from '@lumino/coreutils';
  10. import { Session, SessionAPI } from '../../src';
  11. import {
  12. createSessionModel,
  13. getRequestHandler,
  14. init,
  15. makeSettings
  16. } from '../utils';
  17. init();
  18. const server = new JupyterServer();
  19. beforeAll(async () => {
  20. await server.start();
  21. });
  22. afterAll(async () => {
  23. await server.shutdown();
  24. });
  25. describe('session', () => {
  26. let session: Session.IModel;
  27. beforeAll(async () => {
  28. const sessions = await SessionAPI.listRunning();
  29. await Promise.all(sessions.map(s => SessionAPI.shutdownSession(s.id)));
  30. });
  31. afterEach(async () => {
  32. const sessions = await SessionAPI.listRunning();
  33. await Promise.all(sessions.map(s => SessionAPI.shutdownSession(s.id)));
  34. });
  35. describe('Session.listRunning()', () => {
  36. it('should yield a list of valid session models', async () => {
  37. expect(toArray(await SessionAPI.listRunning()).length).toBe(0);
  38. const session = await SessionAPI.startSession({
  39. name: UUID.uuid4(),
  40. path: UUID.uuid4(),
  41. type: 'test'
  42. });
  43. expect(toArray(await SessionAPI.listRunning())).toEqual([session]);
  44. });
  45. it('should throw an error for an invalid model', async () => {
  46. const data = { id: '1234', path: 'test' };
  47. const serverSettings = getRequestHandler(200, data);
  48. const list = SessionAPI.listRunning(serverSettings);
  49. await expectFailure(list);
  50. });
  51. it('should throw an error for another invalid model', async () => {
  52. const data = [{ id: '1234', kernel: { id: '', name: '' }, path: '' }];
  53. const serverSettings = getRequestHandler(200, data);
  54. const list = SessionAPI.listRunning(serverSettings);
  55. await expectFailure(list);
  56. });
  57. it('should fail for wrong response status', async () => {
  58. const serverSettings = getRequestHandler(201, [createSessionModel()]);
  59. const list = SessionAPI.listRunning(serverSettings);
  60. await expectFailure(list);
  61. });
  62. it('should fail for error response status', async () => {
  63. const serverSettings = getRequestHandler(500, {});
  64. const list = SessionAPI.listRunning(serverSettings);
  65. await expectFailure(list, '');
  66. });
  67. });
  68. describe('SessionAPI.startNew', () => {
  69. it('should start a session', async () => {
  70. session = await SessionAPI.startSession({
  71. path: UUID.uuid4(),
  72. name: UUID.uuid4(),
  73. type: 'test'
  74. });
  75. expect(session.id).toBeTruthy();
  76. });
  77. it('should accept ajax options', async () => {
  78. const serverSettings = makeSettings();
  79. session = await SessionAPI.startSession(
  80. {
  81. path: UUID.uuid4(),
  82. name: UUID.uuid4(),
  83. type: 'test'
  84. },
  85. serverSettings
  86. );
  87. expect(session.id).toBeTruthy();
  88. });
  89. it('should fail for wrong response status', async () => {
  90. const sessionModel = createSessionModel();
  91. const serverSettings = getRequestHandler(200, sessionModel);
  92. const sessionPromise = SessionAPI.startSession(
  93. sessionModel as any,
  94. serverSettings
  95. );
  96. await expectFailure(sessionPromise);
  97. });
  98. it('should fail for error response status', async () => {
  99. const serverSettings = getRequestHandler(500, {});
  100. const sessionModel = createSessionModel();
  101. const sessionPromise = SessionAPI.startSession(
  102. sessionModel as any,
  103. serverSettings
  104. );
  105. await expectFailure(sessionPromise, '');
  106. });
  107. it('should fail for wrong response model', async () => {
  108. const sessionModel = createSessionModel();
  109. (sessionModel as any).path = 1;
  110. const serverSettings = getRequestHandler(201, sessionModel);
  111. const sessionPromise = SessionAPI.startSession(
  112. sessionModel as any,
  113. serverSettings
  114. );
  115. const msg = `Property 'path' is not of type 'string'`;
  116. await expectFailure(sessionPromise, msg);
  117. });
  118. it('should handle a deprecated response model', async () => {
  119. const sessionModel = createSessionModel();
  120. const data = {
  121. id: sessionModel.id,
  122. kernel: sessionModel.kernel,
  123. notebook: { path: sessionModel.path }
  124. };
  125. const serverSettings = getRequestHandler(201, data);
  126. const model = await SessionAPI.startSession(
  127. sessionModel as any,
  128. serverSettings
  129. );
  130. expect(model).toHaveProperty('id');
  131. expect(model.path).toBeTruthy();
  132. });
  133. });
  134. describe('Session.shutdown()', () => {
  135. it('should shut down a kernel by id', async () => {
  136. session = await SessionAPI.startSession({
  137. path: UUID.uuid4(),
  138. name: UUID.uuid4(),
  139. type: 'test'
  140. });
  141. await SessionAPI.shutdownSession(session.id);
  142. });
  143. it('should handle a 404 status', () => {
  144. return SessionAPI.shutdownSession(UUID.uuid4());
  145. });
  146. });
  147. });