-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(extensions): make sure periodical full ui reload doesn't happen #6693
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -30,11 +30,6 @@ let providerState: extensionApi.ProviderConnectionStatus = 'stopped'; | |
let containerProviderConnection: extensionApi.ContainerProviderConnection; | ||
let containerProviderConnectionDisposable: extensionApi.Disposable; | ||
|
||
async function timeout(time: number): Promise<void> { | ||
return new Promise<void>(resolve => { | ||
setTimeout(resolve, time); | ||
}); | ||
} | ||
async function isDockerDaemonAlive(socketPath: string): Promise<boolean> { | ||
const pingUrl = { | ||
path: '/_ping', | ||
|
@@ -89,24 +84,28 @@ async function isDisguisedPodman(socketPath: string): Promise<boolean> { | |
}); | ||
} | ||
|
||
async function monitorDaemon(extensionContext: extensionApi.ExtensionContext): Promise<void> { | ||
// call us again | ||
if (!stopLoop) { | ||
try { | ||
await updateProvider(extensionContext); | ||
} catch (error) { | ||
// ignore the update of machines | ||
} | ||
await timeout(5000); | ||
monitorDaemon(extensionContext).catch((err: unknown) => { | ||
console.error('Error while monitoring docker daemon', err); | ||
if (err instanceof Error) { | ||
extensionApi.env.createTelemetryLogger().logError(err); | ||
} else { | ||
extensionApi.env.createTelemetryLogger().logError(err.toString()); | ||
function monitorDaemon(extensionContext: extensionApi.ExtensionContext): void { | ||
const loop = (): NodeJS.Timeout => | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
setTimeout(async () => { | ||
if (stopLoop) { | ||
return; | ||
} | ||
}); | ||
} | ||
|
||
try { | ||
await updateProvider(extensionContext); | ||
} catch (err) { | ||
console.error('Error while monitoring docker daemon', err); | ||
if (err instanceof Error) { | ||
extensionApi.env.createTelemetryLogger().logError(err); | ||
} else { | ||
extensionApi.env.createTelemetryLogger().logError(err.toString()); | ||
} | ||
} | ||
|
||
setTimeout(() => loop(), 5000); | ||
}, 5000); | ||
loop(); | ||
} | ||
|
||
async function updateProvider(extensionContext: extensionApi.ExtensionContext): Promise<void> { | ||
|
@@ -168,14 +167,7 @@ export async function activate(extensionContext: extensionApi.ExtensionContext): | |
} | ||
|
||
// monitor daemon | ||
monitorDaemon(extensionContext).catch((err: unknown) => { | ||
console.error('Error while monitoring docker daemon', err); | ||
if (err instanceof Error) { | ||
extensionApi.env.createTelemetryLogger().logError(err); | ||
} else { | ||
extensionApi.env.createTelemetryLogger().logError(err.toString()); | ||
} | ||
}); | ||
void monitorDaemon(extensionContext); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hello, here it looks you're bypassing the linter and do not catch error on promises, etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
function initProvider(extensionContext: extensionApi.ExtensionContext): void { | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
import * as extensionApi from '@podman-desktop/api'; | ||
import { afterEach, beforeEach, expect, it, type Mock, vi } from 'vitest'; | ||
|
||
import { Monitor } from './monitor'; | ||
|
||
const logErrorTelemetry = vi.fn(); | ||
|
||
// mock the API | ||
vi.mock('@podman-desktop/api', async () => ({ | ||
env: { | ||
createTelemetryLogger: vi.fn(() => ({ | ||
logError: logErrorTelemetry, | ||
})), | ||
}, | ||
})); | ||
|
||
let fakeFn: Mock; | ||
let stopPredicate: Mock; | ||
let monitor: Monitor; | ||
const fakeName = 'testMonitor'; | ||
|
||
beforeEach(() => { | ||
vi.useFakeTimers(); | ||
fakeFn = vi.fn(); | ||
stopPredicate = vi.fn().mockReturnValue(false); // Initially don't stop the loop | ||
monitor = new Monitor(fakeFn, { | ||
name: fakeName, | ||
stopMonitorPredicate: stopPredicate, | ||
intervalMs: 1000, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.runOnlyPendingTimers(); | ||
vi.useRealTimers(); | ||
}); | ||
|
||
it('Expect monitor to call the function at the specified interval', async () => { | ||
monitor.start(); | ||
|
||
// Initially, fakeFn should not have been called. | ||
expect(fakeFn).not.toHaveBeenCalled(); | ||
|
||
// After 1000ms, fakeFn should be called once. | ||
vi.advanceTimersByTime(1000); | ||
expect(fakeFn).toHaveBeenCalledTimes(1); | ||
|
||
// Ensure the function is not called again if the stop predicate is true. | ||
stopPredicate.mockReturnValue(true); | ||
vi.advanceTimersByTime(1000); | ||
expect(fakeFn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('Expect monitor to stop monitoring when stopLoopPredicate returns true', async () => { | ||
stopPredicate.mockReturnValue(true); | ||
monitor.start(); | ||
|
||
vi.advanceTimersByTime(1000); | ||
expect(fakeFn).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('Expect monitor to log an error when fn throws', async () => { | ||
const error = new Error('Test error'); | ||
fakeFn.mockRejectedValueOnce(error); | ||
monitor.start(); | ||
|
||
vi.advanceTimersByTime(1000); | ||
await new Promise(process.nextTick); | ||
|
||
expect(extensionApi.env.createTelemetryLogger().logError).toHaveBeenCalledWith(error); | ||
}); | ||
|
||
it('Expect monitor to log the correct error message for non-Error instances', async () => { | ||
const errorMessage = 'String error'; | ||
fakeFn.mockRejectedValueOnce(errorMessage); | ||
monitor.start(); | ||
|
||
vi.advanceTimersByTime(1000); | ||
await new Promise(process.nextTick); | ||
|
||
expect(extensionApi.env.createTelemetryLogger().logError).toHaveBeenCalledWith(errorMessage); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should not ignore the misused-promises in the project