job-queue.e2e-spec.ts 5.1 KB

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