-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #382 from johanneswuerbach/fix-error-handling
fix(humanitec): continue polling on error
- Loading branch information
Showing
4 changed files
with
156 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@frontside/backstage-plugin-humanitec-backend': patch | ||
--- | ||
|
||
Continue polling when an error is returned. |
126 changes: 126 additions & 0 deletions
126
plugins/humanitec-backend/src/service/app-info-service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { setTimeout } from 'node:timers/promises'; | ||
import * as common from '@frontside/backstage-plugin-humanitec-common'; | ||
|
||
import { AppInfoService } from './app-info-service'; | ||
|
||
const fetchInterval = 50; | ||
|
||
let returnError = false; | ||
const fakeAppInfo = { fake: 'res' } | ||
const fakeError = new Error('fake error'); | ||
|
||
jest.mock('@frontside/backstage-plugin-humanitec-common', () => ({ | ||
createHumanitecClient: jest.fn(), | ||
fetchAppInfo: jest.fn(async () => { | ||
if (returnError) { | ||
throw fakeError; | ||
} | ||
|
||
return fakeAppInfo; | ||
}), | ||
})) | ||
|
||
describe('AppInfoService', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
returnError = false; | ||
}); | ||
|
||
it('single subscriber', async () => { | ||
const service = new AppInfoService('token', fetchInterval); | ||
const subscriber = jest.fn(); | ||
|
||
const close = service.addSubscriber('orgId', 'appId', subscriber); | ||
|
||
await setTimeout(50); | ||
|
||
expect(subscriber).toHaveBeenCalledTimes(1); | ||
expect(subscriber).toHaveBeenLastCalledWith({ id: 0, data: fakeAppInfo }); | ||
expect(common.createHumanitecClient).toHaveBeenCalledTimes(1); | ||
|
||
await setTimeout(fetchInterval); | ||
|
||
expect(subscriber).toHaveBeenCalledTimes(2); | ||
expect(subscriber).toHaveBeenLastCalledWith({ id: 1, data: fakeAppInfo }); | ||
expect(common.createHumanitecClient).toHaveBeenCalledTimes(2); | ||
|
||
close(); | ||
|
||
await setTimeout(fetchInterval * 2); | ||
|
||
expect(subscriber).toHaveBeenCalledTimes(2); | ||
expect(common.createHumanitecClient).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('single subscriber, recovers after an erro', async () => { | ||
returnError = true | ||
|
||
const service = new AppInfoService('token', fetchInterval); | ||
const subscriber = jest.fn(); | ||
|
||
const close = service.addSubscriber('orgId', 'appId', subscriber); | ||
|
||
await setTimeout(50); | ||
|
||
expect(subscriber).toHaveBeenCalledTimes(1); | ||
expect(subscriber).toHaveBeenLastCalledWith({ id: 0, error: fakeError }); | ||
expect(common.createHumanitecClient).toHaveBeenCalledTimes(1); | ||
|
||
returnError = false; | ||
|
||
await setTimeout(fetchInterval); | ||
|
||
expect(subscriber).toHaveBeenCalledTimes(2); | ||
expect(subscriber).toHaveBeenLastCalledWith({ id: 1, data: fakeAppInfo }); | ||
expect(common.createHumanitecClient).toHaveBeenCalledTimes(2); | ||
|
||
close(); | ||
|
||
await setTimeout(fetchInterval * 2); | ||
|
||
expect(subscriber).toHaveBeenCalledTimes(2); | ||
expect(common.createHumanitecClient).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('two subscribers', async () => { | ||
const service = new AppInfoService('token', fetchInterval); | ||
const subscriber1 = jest.fn(); | ||
const subscriber2 = jest.fn(); | ||
|
||
const close1 = service.addSubscriber('orgId', 'appId', subscriber1); | ||
const close2 = service.addSubscriber('orgId', 'appId', subscriber2); | ||
|
||
await setTimeout(10); | ||
|
||
expect(subscriber1).toHaveBeenCalledTimes(1); | ||
expect(subscriber2).toHaveBeenCalledTimes(1); | ||
expect(subscriber1).toHaveBeenLastCalledWith({ id: 0, data: fakeAppInfo }); | ||
expect(subscriber2).toHaveBeenLastCalledWith({ id: 0, data: fakeAppInfo }); | ||
expect(common.createHumanitecClient).toHaveBeenCalledTimes(1); | ||
|
||
await setTimeout(fetchInterval); | ||
|
||
expect(subscriber1).toHaveBeenCalledTimes(2); | ||
expect(subscriber1).toHaveBeenLastCalledWith({ id: 1, data: fakeAppInfo }); | ||
expect(subscriber2).toHaveBeenCalledTimes(2); | ||
expect(subscriber2).toHaveBeenLastCalledWith({ id: 1, data: fakeAppInfo }); | ||
expect(common.createHumanitecClient).toHaveBeenCalledTimes(2); | ||
|
||
close1(); | ||
|
||
await setTimeout(fetchInterval); | ||
|
||
expect(subscriber1).toHaveBeenCalledTimes(2); | ||
expect(subscriber2).toHaveBeenCalledTimes(3); | ||
expect(subscriber2).toHaveBeenLastCalledWith({ id: 2, data: fakeAppInfo }); | ||
expect(common.createHumanitecClient).toHaveBeenCalledTimes(3); | ||
|
||
close2(); | ||
|
||
await setTimeout(fetchInterval); | ||
|
||
expect(subscriber1).toHaveBeenCalledTimes(2); | ||
expect(subscriber2).toHaveBeenCalledTimes(3); | ||
expect(common.createHumanitecClient).toHaveBeenCalledTimes(3); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters