parallel-transactions.e2e-spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { LanguageCode } from '@vendure/common/lib/generated-types';
  2. import gql from 'graphql-tag';
  3. import path from 'path';
  4. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  5. import { initialData } from '../../../e2e-common/e2e-initial-data';
  6. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  7. import { createTestEnvironment } from '../../testing/lib/create-test-environment';
  8. import { SlowMutationPlugin } from './fixtures/test-plugins/slow-mutation-plugin';
  9. import {
  10. addOptionGroupToProductDocument,
  11. createProductDocument,
  12. createProductOptionGroupDocument,
  13. createProductVariantsDocument,
  14. } from './graphql/shared-definitions';
  15. describe('Parallel transactions', () => {
  16. const { server, adminClient, shopClient } = createTestEnvironment({
  17. ...testConfig(),
  18. plugins: [SlowMutationPlugin],
  19. });
  20. beforeAll(async () => {
  21. await server.init({
  22. initialData,
  23. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  24. customerCount: 2,
  25. });
  26. await adminClient.asSuperAdmin();
  27. }, TEST_SETUP_TIMEOUT_MS);
  28. afterAll(async () => {
  29. await server.destroy();
  30. });
  31. it('does not fail on many concurrent, slow transactions', async () => {
  32. const CONCURRENCY_LIMIT = 20;
  33. const slowMutations = Array.from({ length: CONCURRENCY_LIMIT }).map(i =>
  34. adminClient.query(SLOW_MUTATION, { delay: 50 }),
  35. );
  36. const result = await Promise.all(slowMutations);
  37. expect(result).toEqual(Array.from({ length: CONCURRENCY_LIMIT }).map(() => ({ slowMutation: true })));
  38. }, 100000);
  39. it('does not fail on attempted deadlock', async () => {
  40. const CONCURRENCY_LIMIT = 4;
  41. const slowMutations = Array.from({ length: CONCURRENCY_LIMIT }).map(i =>
  42. adminClient.query(ATTEMPT_DEADLOCK),
  43. );
  44. const result = await Promise.all(slowMutations);
  45. expect(result).toEqual(
  46. Array.from({ length: CONCURRENCY_LIMIT }).map(() => ({ attemptDeadlock: true })),
  47. );
  48. }, 100000);
  49. // A real-world error-case originally reported in https://github.com/vendure-ecommerce/vendure/issues/527
  50. it('does not deadlock on concurrent creating ProductVariants', async () => {
  51. const CONCURRENCY_LIMIT = 4;
  52. const { createProduct } = await adminClient.query(createProductDocument, {
  53. input: {
  54. translations: [
  55. { languageCode: LanguageCode.en, name: 'Test', slug: 'test', description: 'test' },
  56. ],
  57. },
  58. });
  59. const sizes = Array.from({ length: CONCURRENCY_LIMIT }).map(i => `size-${i as string}`);
  60. const { createProductOptionGroup } = await adminClient.query(createProductOptionGroupDocument, {
  61. input: {
  62. code: 'size',
  63. options: sizes.map(size => ({
  64. code: size,
  65. translations: [{ languageCode: LanguageCode.en, name: size }],
  66. })),
  67. translations: [{ languageCode: LanguageCode.en, name: 'size' }],
  68. },
  69. });
  70. await adminClient.query(addOptionGroupToProductDocument, {
  71. productId: createProduct.id,
  72. optionGroupId: createProductOptionGroup.id,
  73. });
  74. const createVariantMutations = createProductOptionGroup.options
  75. .filter((_, index) => index < CONCURRENCY_LIMIT)
  76. .map((option, i) => {
  77. return adminClient.query(createProductVariantsDocument, {
  78. input: [
  79. {
  80. sku: `VARIANT-${i}`,
  81. productId: createProduct.id,
  82. optionIds: [option.id],
  83. translations: [{ languageCode: LanguageCode.en, name: `Variant ${i}` }],
  84. price: 1000,
  85. taxCategoryId: 'T_1',
  86. facetValueIds: ['T_1', 'T_2'],
  87. featuredAssetId: 'T_1',
  88. assetIds: ['T_1'],
  89. },
  90. ],
  91. });
  92. });
  93. const results = await Promise.all(createVariantMutations);
  94. expect(results.length).toBe(CONCURRENCY_LIMIT);
  95. }, 100000);
  96. });
  97. const SLOW_MUTATION = gql`
  98. mutation SlowMutation($delay: Int!) {
  99. slowMutation(delay: $delay)
  100. }
  101. `;
  102. const ATTEMPT_DEADLOCK = gql`
  103. mutation AttemptDeadlock {
  104. attemptDeadlock
  105. }
  106. `;