service.spec.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { Session, KernelSpecManager, KernelSpec } from '@jupyterlab/services';
  4. import {
  5. createSession,
  6. signalToPromise,
  7. JupyterServer
  8. } from '@jupyterlab/testutils';
  9. import { UUID, JSONExt } from '@lumino/coreutils';
  10. import { Debugger } from '../src/debugger';
  11. import { IDebugger } from '../src/tokens';
  12. import { KERNELSPECS, handleRequest } from './utils';
  13. /**
  14. * A Test class to mock a KernelSpecManager
  15. */
  16. class TestKernelSpecManager extends KernelSpecManager {
  17. intercept: KernelSpec.ISpecModels | null = null;
  18. /**
  19. * Request the kernel specs
  20. */
  21. protected async requestSpecs(): Promise<void> {
  22. if (this.intercept) {
  23. handleRequest(this, 200, this.intercept);
  24. }
  25. return super.requestSpecs();
  26. }
  27. }
  28. const server = new JupyterServer();
  29. beforeAll(async () => {
  30. jest.setTimeout(20000);
  31. await server.start();
  32. });
  33. afterAll(async () => {
  34. await server.shutdown();
  35. });
  36. describe('Debugging support', () => {
  37. const specs = JSONExt.deepCopy(KERNELSPECS) as KernelSpec.ISpecModels;
  38. let specsManager: TestKernelSpecManager;
  39. let service: Debugger.Service;
  40. let config: IDebugger.IConfig;
  41. let xpython: Session.ISessionConnection;
  42. let ipykernel: Session.ISessionConnection;
  43. beforeAll(async () => {
  44. xpython = await createSession({
  45. name: '',
  46. type: 'test',
  47. path: UUID.uuid4()
  48. });
  49. await xpython.changeKernel({ name: 'xpython' });
  50. ipykernel = await createSession({
  51. name: '',
  52. type: 'test',
  53. path: UUID.uuid4()
  54. });
  55. await ipykernel.changeKernel({ name: 'python3' });
  56. specsManager = new TestKernelSpecManager({ standby: 'never' });
  57. specsManager.intercept = specs;
  58. await specsManager.refreshSpecs();
  59. config = new Debugger.Config();
  60. service = new Debugger.Service({ specsManager, config });
  61. });
  62. afterAll(async () => {
  63. await Promise.all([xpython.shutdown(), ipykernel.shutdown()]);
  64. service.dispose();
  65. specsManager.dispose();
  66. });
  67. describe('#isAvailable', () => {
  68. it('should return true for kernels that have support for debugging', async () => {
  69. const enabled = await service.isAvailable(xpython);
  70. expect(enabled).toBe(true);
  71. });
  72. it('should return false for kernels that do not have support for debugging', async () => {
  73. const enabled = await service.isAvailable(ipykernel);
  74. expect(enabled).toBe(false);
  75. });
  76. });
  77. });
  78. describe('DebuggerService', () => {
  79. const specsManager = new KernelSpecManager();
  80. let connection: Session.ISessionConnection;
  81. let config: IDebugger.IConfig;
  82. let session: IDebugger.ISession;
  83. let service: IDebugger;
  84. beforeEach(async () => {
  85. connection = await createSession({
  86. name: '',
  87. type: 'test',
  88. path: UUID.uuid4()
  89. });
  90. await connection.changeKernel({ name: 'xpython' });
  91. session = new Debugger.Session({ connection });
  92. config = new Debugger.Config();
  93. service = new Debugger.Service({ specsManager, config });
  94. });
  95. afterEach(async () => {
  96. await connection.shutdown();
  97. connection.dispose();
  98. session.dispose();
  99. (service as Debugger.Service).dispose();
  100. });
  101. describe('#constructor()', () => {
  102. it('should create a new instance', () => {
  103. expect(service).toBeInstanceOf(Debugger.Service);
  104. });
  105. });
  106. describe('#start()', () => {
  107. it('should start the service if the session is set', async () => {
  108. service.session = session;
  109. await service.start();
  110. expect(service.isStarted).toEqual(true);
  111. });
  112. });
  113. describe('#stop()', () => {
  114. it('should stop the service if the session is set', async () => {
  115. service.session = session;
  116. await service.start();
  117. await service.stop();
  118. expect(service.isStarted).toEqual(false);
  119. });
  120. });
  121. describe('#session', () => {
  122. it('should emit the sessionChanged signal when setting the session', () => {
  123. const sessionChangedEvents: (IDebugger.ISession | null)[] = [];
  124. service.sessionChanged.connect((_, newSession) => {
  125. sessionChangedEvents.push(newSession);
  126. });
  127. service.session = session;
  128. expect(sessionChangedEvents.length).toEqual(1);
  129. expect(sessionChangedEvents[0]).toEqual(session);
  130. });
  131. });
  132. describe('protocol', () => {
  133. const code = [
  134. 'i = 0',
  135. 'i += 1',
  136. 'i += 1',
  137. 'j = i**2',
  138. 'j += 1',
  139. 'print(i, j)'
  140. ].join('\n');
  141. let breakpoints: IDebugger.IBreakpoint[];
  142. let sourceId: string;
  143. beforeEach(async () => {
  144. service.session = session;
  145. await service.restoreState(true);
  146. const breakpointLines: number[] = [3, 5];
  147. sourceId = service.getCodeId(code);
  148. breakpoints = breakpointLines.map((l: number, index: number) => {
  149. return {
  150. id: index,
  151. line: l,
  152. verified: true,
  153. source: {
  154. path: sourceId
  155. }
  156. };
  157. });
  158. await service.updateBreakpoints(code, breakpoints);
  159. });
  160. describe('#updateBreakpoints', () => {
  161. it('should update the breakpoints', () => {
  162. const { model } = service;
  163. const bpList = model.breakpoints.getBreakpoints(sourceId);
  164. expect(bpList).toEqual(breakpoints);
  165. });
  166. });
  167. describe('#restoreState', () => {
  168. it('should restore the breakpoints', async () => {
  169. const { model } = service;
  170. model.breakpoints.restoreBreakpoints(
  171. new Map<string, IDebugger.IBreakpoint[]>()
  172. );
  173. const bpList1 = model.breakpoints.getBreakpoints(sourceId);
  174. expect(bpList1.length).toEqual(0);
  175. await service.restoreState(true);
  176. const bpList2 = model.breakpoints.getBreakpoints(sourceId);
  177. expect(bpList2).toEqual(breakpoints);
  178. });
  179. });
  180. describe('#restart', () => {
  181. it('should restart the debugger and send the breakpoints again', async () => {
  182. await service.restart();
  183. const { model } = service;
  184. model.breakpoints.restoreBreakpoints(
  185. new Map<string, IDebugger.IBreakpoint[]>()
  186. );
  187. await service.restoreState(true);
  188. const bpList = model.breakpoints.getBreakpoints(sourceId);
  189. breakpoints[0].id = 2;
  190. breakpoints[1].id = 3;
  191. expect(bpList).toEqual(breakpoints);
  192. });
  193. });
  194. describe('#hasStoppedThreads', () => {
  195. it('should return false if the model is null', () => {
  196. const hasStoppedThreads = service.hasStoppedThreads();
  197. expect(hasStoppedThreads).toBe(false);
  198. });
  199. it('should return true when the execution has stopped', async () => {
  200. const { model } = service;
  201. const variablesChanged = signalToPromise(model.variables.changed);
  202. // trigger a manual execute request
  203. connection!.kernel!.requestExecute({ code });
  204. // wait for the first stopped event and variables changed
  205. await variablesChanged;
  206. const hasStoppedThreads = service.hasStoppedThreads();
  207. expect(hasStoppedThreads).toBe(true);
  208. await service.restart();
  209. });
  210. });
  211. });
  212. });