job-queue.e2e-spec.ts 5.6 KB

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