config-loader: RemoteConfigSource test + fix

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-04-03 12:02:45 +02:00
parent b95575173d
commit b5369fa23a
4 changed files with 91 additions and 9 deletions
+1
View File
@@ -52,6 +52,7 @@
"yup": "^0.32.9"
},
"devDependencies": {
"@backstage/backend-test-utils": "workspace:^",
"@backstage/cli": "workspace:^",
"@types/json-schema-merge-allof": "^0.6.0",
"@types/mock-fs": "^4.10.0",
@@ -0,0 +1,85 @@
/*
* 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 { rest } from 'msw';
import { setupRequestMockHandlers } from '@backstage/backend-test-utils';
import { setupServer } from 'msw/node';
import { RemoteConfigSource } from './RemoteConfigSource';
import { readN } from './__testUtils__/testUtils';
describe('RemoteConfigSource', () => {
const worker = setupServer();
setupRequestMockHandlers(worker);
it('should load config from a remote URL', async () => {
worker.use(
rest.get('http://localhost/config.yaml', (_req, res, ctx) =>
res(
ctx.body(`
app:
title: Example App
substituted: \${VALUE}
escaped: \$\${VALUE}
`),
),
),
);
const source = RemoteConfigSource.create({
url: 'http://localhost/config.yaml',
substitutionFunc: async () => 'x',
});
await expect(readN(source, 1)).resolves.toEqual([
[
{
context: 'http://localhost/config.yaml',
data: {
app: {
title: 'Example App',
substituted: 'x',
escaped: '${VALUE}',
},
},
},
],
]);
});
it('should reload config from a remote URL', async () => {
let fetched = false;
worker.use(
rest.get('http://localhost/config.yaml', (_req, res, ctx) => {
if (!fetched) {
fetched = true;
return res(ctx.body('x: 1'));
}
return res(ctx.body('x: 2'));
}),
);
const source = RemoteConfigSource.create({
url: 'http://localhost/config.yaml',
reloadIntervalSeconds: 0,
});
await expect(readN(source, 2)).resolves.toEqual([
[{ context: 'http://localhost/config.yaml', data: { x: 1 } }],
[{ context: 'http://localhost/config.yaml', data: { x: 2 } }],
]);
});
});
@@ -101,7 +101,8 @@ export class RemoteConfigSource implements ConfigSource {
if (options?.signal?.aborted) {
return;
}
const loadStart = Date.now();
await this.#wait(options?.signal);
try {
const newData = await this.#load(options?.signal);
@@ -112,9 +113,6 @@ export class RemoteConfigSource implements ConfigSource {
} catch (error) {
console.error(`Failed to read config from ${this.#url}, ${error}`);
}
const loadTime = Date.now() - loadStart;
await this.#wait(loadTime, options?.signal);
}
}
@@ -140,12 +138,9 @@ export class RemoteConfigSource implements ConfigSource {
return data;
}
async #wait(loadTimeMs: number, signal?: AbortSignal) {
async #wait(signal?: AbortSignal) {
return new Promise<void>(resolve => {
const timeoutId = setTimeout(
onDone,
Math.max(0, this.#reloadIntervalSeconds * 1000 - loadTimeMs),
);
const timeoutId = setTimeout(onDone, this.#reloadIntervalSeconds * 1000);
signal?.addEventListener('abort', onDone);
function onDone() {
+1
View File
@@ -3923,6 +3923,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@backstage/config-loader@workspace:packages/config-loader"
dependencies:
"@backstage/backend-test-utils": "workspace:^"
"@backstage/cli": "workspace:^"
"@backstage/cli-common": "workspace:^"
"@backstage/config": "workspace:^"