From 2b8d1d37f041d40ef81cfbb13f5692c33663882f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Thu, 24 Nov 2022 16:22:17 +0100 Subject: [PATCH] fix racy test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .../src/module/WrapperProviders.test.ts | 23 +++++-------------- .../src/module/WrapperProviders.ts | 15 ++++++++---- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/plugins/catalog-backend-module-incremental-ingestion/src/module/WrapperProviders.test.ts b/plugins/catalog-backend-module-incremental-ingestion/src/module/WrapperProviders.test.ts index 1aa0bfe2eb..0d910f664d 100644 --- a/plugins/catalog-backend-module-incremental-ingestion/src/module/WrapperProviders.test.ts +++ b/plugins/catalog-backend-module-incremental-ingestion/src/module/WrapperProviders.test.ts @@ -18,23 +18,11 @@ import { getVoidLogger } from '@backstage/backend-common'; import { PluginTaskScheduler } from '@backstage/backend-tasks'; import { TestDatabases } from '@backstage/backend-test-utils'; import { ConfigReader } from '@backstage/config'; -import { Knex } from 'knex'; -import '../database/migrations'; import { IncrementalEntityProvider } from '../types'; import { WrapperProviders } from './WrapperProviders'; -const applyDatabaseMigrations = jest.fn(); -jest.mock('../database/migrations', () => { - const actual = jest.requireActual('../database/migrations'); - return { - applyDatabaseMigrations: (knex: Knex) => { - applyDatabaseMigrations(knex); - return actual.applyDatabaseMigrations(knex); - }, - }; -}); - describe('WrapperProviders', () => { + const applyDatabaseMigrations = jest.fn(); const databases = TestDatabases.create({ ids: ['POSTGRES_13', 'POSTGRES_9', 'SQLITE_3'], }); @@ -45,7 +33,7 @@ describe('WrapperProviders', () => { }; beforeEach(() => { - jest.clearAllMocks(); + jest.resetAllMocks(); }); it.each(databases.eachSupportedId())( @@ -56,7 +44,7 @@ describe('WrapperProviders', () => { const provider1: IncrementalEntityProvider = { getProviderName: () => 'provider1', around: burst => burst(0), - next: async (cursor, _context) => { + next: async (_context, cursor) => { return !cursor ? { done: false, entities: [], cursor: 1 } : { done: true }; @@ -66,8 +54,8 @@ describe('WrapperProviders', () => { const provider2: IncrementalEntityProvider = { getProviderName: () => 'provider2', around: burst => burst(0), - next: async (cursor, _context) => { - return cursor === 0 + next: async (_context, cursor) => { + return !cursor ? { done: false, entities: [], cursor: 1 } : { done: true }; }, @@ -79,6 +67,7 @@ describe('WrapperProviders', () => { client, scheduler: scheduler as Partial as PluginTaskScheduler, + applyDatabaseMigrations, }); const wrapped1 = providers.wrap(provider1, { burstInterval: { seconds: 1 }, diff --git a/plugins/catalog-backend-module-incremental-ingestion/src/module/WrapperProviders.ts b/plugins/catalog-backend-module-incremental-ingestion/src/module/WrapperProviders.ts index 7b68051c6b..44da5067fd 100644 --- a/plugins/catalog-backend-module-incremental-ingestion/src/module/WrapperProviders.ts +++ b/plugins/catalog-backend-module-incremental-ingestion/src/module/WrapperProviders.ts @@ -24,7 +24,6 @@ import { } from '@backstage/plugin-catalog-node'; import express from 'express'; import { Knex } from 'knex'; -import once from 'lodash/once'; import { Duration } from 'luxon'; import { IncrementalIngestionDatabaseManager } from '../database/IncrementalIngestionDatabaseManager'; import { applyDatabaseMigrations } from '../database/migrations'; @@ -36,13 +35,12 @@ import { } from '../types'; import { Deferred } from '../util'; -const applyDatabaseMigrationsOnce = once(applyDatabaseMigrations); - /** * Helps in the creation of the catalog entity providers that wrap the * incremental ones. */ export class WrapperProviders { + private migrate: Promise | undefined; private numberOfProvidersToConnect = 0; private readonly readySignal = new Deferred(); @@ -52,6 +50,7 @@ export class WrapperProviders { logger: Logger; client: Knex; scheduler: PluginTaskScheduler; + applyDatabaseMigrations?: typeof applyDatabaseMigrations; }, ) {} @@ -91,7 +90,15 @@ export class WrapperProviders { ); try { - await applyDatabaseMigrationsOnce(this.options.client); + if (!this.migrate) { + this.migrate = Promise.resolve().then(async () => { + const apply = + this.options.applyDatabaseMigrations ?? applyDatabaseMigrations; + await apply(this.options.client); + }); + } + + await this.migrate; const { burstInterval, burstLength, restLength } = providerOptions;