job-queue.e2e-spec.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { JobState } from '@vendure/common/lib/generated-types';
  2. import { DefaultJobQueuePlugin, mergeConfig } from '@vendure/core';
  3. import { createTestEnvironment } from '@vendure/testing';
  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 { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  8. import { PluginWithJobQueue } from './fixtures/test-plugins/with-job-queue';
  9. import { cancelJobDocument, getRunningJobsDocument } 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: [
  24. DefaultJobQueuePlugin.init({
  25. pollInterval: 50,
  26. gracefulShutdownTimeout: 1_000,
  27. }),
  28. PluginWithJobQueue,
  29. ],
  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(getRunningJobsDocument, {
  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(cancelJobDocument, {
  109. id: jobId,
  110. });
  111. expect(cancelJob.state).toBe(JobState.CANCELLED);
  112. expect(cancelJob.isSettled).toBe(true);
  113. expect(cancelJob.settledAt).not.toBeNull();
  114. const jobs2 = await getJobsInTestQueue(JobState.CANCELLED);
  115. expect(jobs.items.length).toBe(1);
  116. expect(jobs.items[0].id).toBe(jobId);
  117. PluginWithJobQueue.jobSubject.next();
  118. });
  119. it('subscribe to result of job', async () => {
  120. const restControllerUrl = `http://localhost:${activeConfig.apiOptions.port}/run-job/subscribe`;
  121. const result = await adminClient.fetch(restControllerUrl);
  122. expect(await result.text()).toBe('42!');
  123. const jobs = await getJobsInTestQueue(JobState.RUNNING);
  124. expect(jobs.items.length).toBe(0);
  125. });
  126. it('subscribable that times out', async () => {
  127. const restControllerUrl = `http://localhost:${activeConfig.apiOptions.port}/run-job/subscribe-timeout`;
  128. const result = await adminClient.fetch(restControllerUrl);
  129. expect(result.status).toBe(200);
  130. expect(await result.text()).toBe('Job subscription timed out. The job may still be running');
  131. const jobs = await getJobsInTestQueue(JobState.RUNNING);
  132. expect(jobs.items.length).toBe(1);
  133. });
  134. it('server still running after timeout', async () => {
  135. const jobs = await getJobsInTestQueue(JobState.RUNNING);
  136. expect(jobs.items.length).toBe(1);
  137. });
  138. });
  139. function sleep(ms: number): Promise<void> {
  140. return new Promise(resolve => setTimeout(resolve, ms));
  141. }