From b40661378d5318c61748423ce17df475384e75aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Wed, 25 Jan 2023 16:47:14 +0100 Subject: [PATCH] add a test for the core scheduler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- packages/backend-app-api/package.json | 1 + .../scheduler/schedulerFactory.test.ts | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 packages/backend-app-api/src/services/implementations/scheduler/schedulerFactory.test.ts diff --git a/packages/backend-app-api/package.json b/packages/backend-app-api/package.json index f7c574d7b6..ef34fbdb25 100644 --- a/packages/backend-app-api/package.json +++ b/packages/backend-app-api/package.json @@ -64,6 +64,7 @@ "winston-transport": "^4.5.0" }, "devDependencies": { + "@backstage/backend-test-utils": "workspace:^", "@backstage/cli": "workspace:^", "@types/compression": "^1.7.0", "@types/fs-extra": "^9.0.3", diff --git a/packages/backend-app-api/src/services/implementations/scheduler/schedulerFactory.test.ts b/packages/backend-app-api/src/services/implementations/scheduler/schedulerFactory.test.ts new file mode 100644 index 0000000000..1aaf4ec71d --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/scheduler/schedulerFactory.test.ts @@ -0,0 +1,68 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + coreServices, + createBackendPlugin, +} from '@backstage/backend-plugin-api'; +import { startTestBackend } from '@backstage/backend-test-utils'; +import { schedulerFactory } from './schedulerFactory'; + +describe('schedulerFactory', () => { + it('creates sidecar database features', async () => { + expect.assertions(3); + + const subject = schedulerFactory(); + + const plugin = createBackendPlugin({ + id: 'example', + register(reg) { + reg.registerInit({ + deps: { + scheduler: subject.service, + database: coreServices.database, + }, + init: async ({ scheduler, database }) => { + await scheduler.scheduleTask({ + id: 'task1', + timeout: { seconds: 1 }, + frequency: { seconds: 1 }, + fn: async () => {}, + }); + + const client = await database.getClient(); + await expect( + client.from('backstage_backend_tasks__tasks').count(), + ).resolves.toEqual([{ 'count(*)': 1 }]); + await expect( + client.from('backstage_backend_tasks__knex_migrations').count(), + ).resolves.toEqual([{ 'count(*)': expect.any(Number) }]); + await expect( + client + .from('backstage_backend_tasks__knex_migrations_lock') + .count(), + ).resolves.toEqual([{ 'count(*)': expect.any(Number) }]); + }, + }); + }, + }); + + await startTestBackend({ + features: [plugin()], + services: [subject], + }); + }); +});