job-queue.e2e-spec.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { DefaultJobQueuePlugin, mergeConfig } from '@vendure/core';
  2. import { createTestEnvironment } from '@vendure/testing';
  3. import path from 'path';
  4. import { initialData } from '../../../e2e-common/e2e-initial-data';
  5. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  6. import { PluginWithJobQueue } from './fixtures/test-plugins/with-job-queue';
  7. import { GetRunningJobs, JobState } from './graphql/generated-e2e-admin-types';
  8. import { GET_RUNNING_JOBS } from './graphql/shared-definitions';
  9. describe('JobQueue', () => {
  10. if (testConfig.dbConnectionOptions.type === 'sqljs') {
  11. it.only('skip JobQueue tests for sqljs', () => {
  12. // The tests in this suite will fail when running on sqljs because
  13. // the DB state is not persisted after shutdown. In this case it is
  14. // an acceptable tradeoff to just skip them, since the other DB engines
  15. // _will_ run in CI, and sqljs is less of a production use-case anyway.
  16. return;
  17. });
  18. }
  19. const { server, adminClient } = createTestEnvironment(
  20. mergeConfig(testConfig, {
  21. plugins: [DefaultJobQueuePlugin, PluginWithJobQueue],
  22. }),
  23. );
  24. beforeAll(async () => {
  25. await server.init({
  26. initialData,
  27. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  28. customerCount: 1,
  29. });
  30. await adminClient.asSuperAdmin();
  31. await sleep(1000);
  32. }, TEST_SETUP_TIMEOUT_MS);
  33. afterAll(async () => {
  34. await server.destroy();
  35. });
  36. function getJobsInTestQueue() {
  37. return adminClient
  38. .query<GetRunningJobs.Query, GetRunningJobs.Variables>(GET_RUNNING_JOBS, {
  39. options: {
  40. filter: {
  41. queueName: {
  42. eq: 'test',
  43. },
  44. },
  45. },
  46. })
  47. .then((data) => data.jobs);
  48. }
  49. let testJobId: string;
  50. it('creates and starts running a job', async () => {
  51. const restControllerUrl = `http://localhost:${testConfig.port}/run-job`;
  52. await adminClient.fetch(restControllerUrl);
  53. await sleep(300);
  54. const jobs = await getJobsInTestQueue();
  55. expect(jobs.items.length).toBe(1);
  56. expect(jobs.items[0].state).toBe(JobState.RUNNING);
  57. expect(PluginWithJobQueue.jobHasDoneWork).toBe(false);
  58. testJobId = jobs.items[0].id;
  59. });
  60. it(
  61. 'shutdown server before completing job',
  62. async () => {
  63. await server.destroy();
  64. await server.bootstrap();
  65. await adminClient.asSuperAdmin();
  66. await sleep(300);
  67. const jobs = await getJobsInTestQueue();
  68. expect(jobs.items.length).toBe(1);
  69. expect(jobs.items[0].state).toBe(JobState.RUNNING);
  70. expect(jobs.items[0].id).toBe(testJobId);
  71. expect(PluginWithJobQueue.jobHasDoneWork).toBe(false);
  72. },
  73. TEST_SETUP_TIMEOUT_MS,
  74. );
  75. it('complete job after restart', async () => {
  76. PluginWithJobQueue.jobSubject.complete();
  77. await sleep(300);
  78. const jobs = await getJobsInTestQueue();
  79. expect(jobs.items.length).toBe(1);
  80. expect(jobs.items[0].state).toBe(JobState.COMPLETED);
  81. expect(jobs.items[0].id).toBe(testJobId);
  82. expect(PluginWithJobQueue.jobHasDoneWork).toBe(true);
  83. });
  84. });
  85. function sleep(ms: number): Promise<void> {
  86. return new Promise((resolve) => setTimeout(resolve, ms));
  87. }