Skip to content

Commit

Permalink
fix: ensure errors are handled
Browse files Browse the repository at this point in the history
Signed-off-by: axel7083 <[email protected]>
  • Loading branch information
axel7083 committed Mar 11, 2024
1 parent eae02e9 commit b06e6cf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
***********************************************************************/

import { beforeEach, expect, afterEach, test, vi } from 'vitest';
import { MonitoringManager } from './MonitoringManager';
import { MonitoringManager } from './monitoringManager';
import { containerEngine, type ContainerStatsInfo, type Webview } from '@podman-desktop/api';
import { Messages } from '@shared/Messages';

Expand Down Expand Up @@ -176,3 +176,24 @@ test('expect old stats to be removed', async () => {
expect(stats.length).toBe(1);
expect(stats[0].stats.length).toBe(3);
});

test('expect stats to be disposed if stats result is an error', async () => {
const manager = new MonitoringManager(webviewMock);
let mCallback: (stats: ContainerStatsInfo) => void;
const fakeDisposable = vi.fn();
vi.mocked(containerEngine.statsContainer).mockImplementation(async (_engineId, _id, callback) => {
mCallback = callback;
return { dispose: fakeDisposable };
});

await manager.monitor('randomContainerId', 'dummyEngineId');
await vi.waitFor(() => {
expect(mCallback).toBeDefined();
});

mCallback({ cause: 'container is stopped'} as unknown as ContainerStatsInfo);

const stats = manager.getStats();
expect(stats.length).toBe(0);
expect(fakeDisposable).toHaveBeenCalled();
});
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ export class MonitoringManager extends Publisher<StatsHistory[]> implements Disp
const disposable = await containerEngine.statsContainer(
engineId,
containerId,
(statsInfo) => this.push(containerId, statsInfo),
(statsInfo) => {
if('cause' in statsInfo) {
console.error('Cannot stats container', statsInfo.cause);
disposable.dispose();
} else {
this.push(containerId, statsInfo);
}
},
);
this.#disposables.push(disposable);
return disposable;
Expand Down

0 comments on commit b06e6cf

Please sign in to comment.