job-queue.e2e-spec.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { DefaultJobQueuePlugin, mergeConfig } from '@vendure/core';
  2. import { createTestEnvironment } from '@vendure/testing';
  3. import gql from 'graphql-tag';
  4. import path from 'path';
  5. import { initialData } from '../../../e2e-common/e2e-initial-data';
  6. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  7. import { PluginWithJobQueue } from './fixtures/test-plugins/with-job-queue';
  8. import {
  9. CancelJobMutation,
  10. CancelJobMutationVariables,
  11. GetRunningJobsQuery,
  12. GetRunningJobsQueryVariables,
  13. JobState,
  14. } from './graphql/generated-e2e-admin-types';
  15. import { GET_RUNNING_JOBS } from './graphql/shared-definitions';
  16. describe('JobQueue', () => {
  17. const activeConfig = testConfig();
  18. if (activeConfig.dbConnectionOptions.type === 'sqljs') {
  19. it.only('skip JobQueue tests for sqljs', () => {
  20. // The tests in this suite will fail when running on sqljs because
  21. // the DB state is not persisted after shutdown. In this case it is
  22. // an acceptable tradeoff to just skip them, since the other DB engines
  23. // _will_ run in CI, and sqljs is less of a production use-case anyway.
  24. return;
  25. });
  26. }
  27. const { server, adminClient } = createTestEnvironment(
  28. mergeConfig(activeConfig, {
  29. plugins: [DefaultJobQueuePlugin, PluginWithJobQueue],
  30. }),
  31. );
  32. beforeAll(async () => {
  33. await server.init({
  34. initialData,
  35. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  36. customerCount: 1,
  37. });
  38. await adminClient.asSuperAdmin();
  39. await sleep(1000);
  40. }, TEST_SETUP_TIMEOUT_MS);
  41. afterAll(async () => {
  42. PluginWithJobQueue.jobSubject.complete();
  43. await server.destroy();
  44. });
  45. function getJobsInTestQueue(state?: JobState) {
  46. return adminClient
  47. .query<GetRunningJobsQuery, GetRunningJobsQueryVariables>(GET_RUNNING_JOBS, {
  48. options: {
  49. filter: {
  50. queueName: {
  51. eq: 'test',
  52. },
  53. ...(state
  54. ? {
  55. state: { eq: state },
  56. }
  57. : {}),
  58. },
  59. },
  60. })
  61. .then(data => data.jobs);
  62. }
  63. let testJobId: string;
  64. it('creates and starts running a job', async () => {
  65. const restControllerUrl = `http://localhost:${activeConfig.apiOptions.port}/run-job`;
  66. await adminClient.fetch(restControllerUrl);
  67. await sleep(300);
  68. const jobs = await getJobsInTestQueue();
  69. expect(jobs.items.length).toBe(1);
  70. expect(jobs.items[0].state).toBe(JobState.RUNNING);
  71. expect(PluginWithJobQueue.jobHasDoneWork).toBe(false);
  72. testJobId = jobs.items[0].id;
  73. });
  74. it(
  75. 'shutdown server before completing job',
  76. async () => {
  77. await server.destroy();
  78. await server.bootstrap();
  79. await adminClient.asSuperAdmin();
  80. await sleep(300);
  81. const jobs = await getJobsInTestQueue();
  82. expect(jobs.items.length).toBe(1);
  83. expect(jobs.items[0].state).toBe(JobState.RUNNING);
  84. expect(jobs.items[0].id).toBe(testJobId);
  85. expect(PluginWithJobQueue.jobHasDoneWork).toBe(false);
  86. },
  87. TEST_SETUP_TIMEOUT_MS,
  88. );
  89. it('complete job after restart', async () => {
  90. PluginWithJobQueue.jobSubject.next();
  91. await sleep(300);
  92. const jobs = await getJobsInTestQueue();
  93. expect(jobs.items.length).toBe(1);
  94. expect(jobs.items[0].state).toBe(JobState.COMPLETED);
  95. expect(jobs.items[0].id).toBe(testJobId);
  96. expect(PluginWithJobQueue.jobHasDoneWork).toBe(true);
  97. });
  98. it('cancels a running job', async () => {
  99. PluginWithJobQueue.jobHasDoneWork = false;
  100. const restControllerUrl = `http://localhost:${activeConfig.apiOptions.port}/run-job`;
  101. await adminClient.fetch(restControllerUrl);
  102. await sleep(300);
  103. const jobs = await getJobsInTestQueue(JobState.RUNNING);
  104. expect(jobs.items.length).toBe(1);
  105. expect(jobs.items[0].state).toBe(JobState.RUNNING);
  106. expect(PluginWithJobQueue.jobHasDoneWork).toBe(false);
  107. const jobId = jobs.items[0].id;
  108. const { cancelJob } = await adminClient.query<CancelJobMutation, CancelJobMutationVariables>(
  109. CANCEL_JOB,
  110. {
  111. id: jobId,
  112. },
  113. );
  114. expect(cancelJob.state).toBe(JobState.CANCELLED);
  115. expect(cancelJob.isSettled).toBe(true);
  116. expect(cancelJob.settledAt).not.toBeNull();
  117. const jobs2 = await getJobsInTestQueue(JobState.CANCELLED);
  118. expect(jobs.items.length).toBe(1);
  119. expect(jobs.items[0].id).toBe(jobId);
  120. });
  121. it('subscribe to result of job', async () => {
  122. const restControllerUrl = `http://localhost:${activeConfig.apiOptions.port}/run-job/subscribe`;
  123. const result = await adminClient.fetch(restControllerUrl);
  124. expect(await result.text()).toBe('42!');
  125. const jobs = await getJobsInTestQueue(JobState.RUNNING);
  126. expect(jobs.items.length).toBe(0);
  127. });
  128. });
  129. function sleep(ms: number): Promise<void> {
  130. return new Promise(resolve => setTimeout(resolve, ms));
  131. }
  132. const CANCEL_JOB = gql`
  133. mutation CancelJob($id: ID!) {
  134. cancelJob(jobId: $id) {
  135. id
  136. state
  137. isSettled
  138. settledAt
  139. }
  140. }
  141. `;