service.spec.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. it('should throw an error if the session is not set', async () => {
  113. await expect(async () => await service.start()).rejects.toThrow(
  114. 'No active debugger session'
  115. );
  116. });
  117. });
  118. describe('#stop()', () => {
  119. it('should stop the service if the session is set', async () => {
  120. service.session = session;
  121. await service.start();
  122. await service.stop();
  123. expect(service.isStarted).toEqual(false);
  124. });
  125. });
  126. describe('#session', () => {
  127. it('should emit the sessionChanged signal when setting the session', () => {
  128. const sessionChangedEvents: (IDebugger.ISession | null)[] = [];
  129. service.sessionChanged.connect((_, newSession) => {
  130. sessionChangedEvents.push(newSession);
  131. });
  132. service.session = session;
  133. expect(sessionChangedEvents.length).toEqual(1);
  134. expect(sessionChangedEvents[0]).toEqual(session);
  135. });
  136. });
  137. describe('protocol', () => {
  138. const code = [
  139. 'i = 0',
  140. 'i += 1',
  141. 'i += 1',
  142. 'j = i**2',
  143. 'j += 1',
  144. 'print(i, j)'
  145. ].join('\n');
  146. let breakpoints: IDebugger.IBreakpoint[];
  147. let sourceId: string;
  148. beforeEach(async () => {
  149. service.session = session;
  150. await service.restoreState(true);
  151. const breakpointLines: number[] = [3, 5];
  152. sourceId = service.getCodeId(code);
  153. breakpoints = breakpointLines.map((l: number, index: number) => {
  154. return {
  155. id: index,
  156. line: l,
  157. verified: true,
  158. source: {
  159. path: sourceId
  160. }
  161. };
  162. });
  163. await service.updateBreakpoints(code, breakpoints);
  164. });
  165. describe('#updateBreakpoints', () => {
  166. it('should update the breakpoints', () => {
  167. const { model } = service;
  168. const bpList = model.breakpoints.getBreakpoints(sourceId);
  169. expect(bpList).toEqual(breakpoints);
  170. });
  171. });
  172. describe('#restoreState', () => {
  173. it('should restore the breakpoints', async () => {
  174. const { model } = service;
  175. model.breakpoints.restoreBreakpoints(
  176. new Map<string, IDebugger.IBreakpoint[]>()
  177. );
  178. const bpList1 = model.breakpoints.getBreakpoints(sourceId);
  179. expect(bpList1.length).toEqual(0);
  180. await service.restoreState(true);
  181. const bpList2 = model.breakpoints.getBreakpoints(sourceId);
  182. expect(bpList2).toEqual(breakpoints);
  183. });
  184. });
  185. describe('#restart', () => {
  186. it('should restart the debugger and send the breakpoints again', async () => {
  187. await service.restart();
  188. const { model } = service;
  189. model.breakpoints.restoreBreakpoints(
  190. new Map<string, IDebugger.IBreakpoint[]>()
  191. );
  192. await service.restoreState(true);
  193. const bpList = model.breakpoints.getBreakpoints(sourceId);
  194. breakpoints[0].id = 2;
  195. breakpoints[1].id = 3;
  196. expect(bpList).toEqual(breakpoints);
  197. });
  198. });
  199. describe('#hasStoppedThreads', () => {
  200. it('should return false if the model is null', () => {
  201. const hasStoppedThreads = service.hasStoppedThreads();
  202. expect(hasStoppedThreads).toBe(false);
  203. });
  204. it('should return true when the execution has stopped', async () => {
  205. const { model } = service;
  206. const variablesChanged = signalToPromise(model.variables.changed);
  207. // trigger a manual execute request
  208. connection!.kernel!.requestExecute({ code });
  209. // wait for the first stopped event and variables changed
  210. await variablesChanged;
  211. const hasStoppedThreads = service.hasStoppedThreads();
  212. expect(hasStoppedThreads).toBe(true);
  213. await service.restart();
  214. });
  215. });
  216. });
  217. });