fix racy test

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2022-11-24 16:22:17 +01:00
parent d7209f1a97
commit 2b8d1d37f0
2 changed files with 17 additions and 21 deletions
@@ -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<number, {}> = {
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<number, {}> = {
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<PluginTaskScheduler> as PluginTaskScheduler,
applyDatabaseMigrations,
});
const wrapped1 = providers.wrap(provider1, {
burstInterval: { seconds: 1 },
@@ -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<void> | undefined;
private numberOfProvidersToConnect = 0;
private readonly readySignal = new Deferred<void>();
@@ -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;