job-queue.e2e-spec.ts 5.4 KB

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