service.spec.ts 6.7 KB

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