Skip to content

Commit

Permalink
fix: make informers constants mockable (podman-desktop#6168)
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Martin <[email protected]>
  • Loading branch information
feloy authored Feb 27, 2024
1 parent 7df5b59 commit e69e5eb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
22 changes: 22 additions & 0 deletions packages/main/src/plugin/kubernetes-context-state-constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**********************************************************************
* 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
***********************************************************************/

export const connectTimeout = 1000;
export const backoffInitialValue = 1000;
export const backoffLimit = 60_000;
export const backoffJitter = 300;
13 changes: 11 additions & 2 deletions packages/main/src/plugin/kubernetes-context-state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ vi.mock('@kubernetes/client-node', async importOriginal => {
};
});

vi.mock('./kubernetes-context-state-constants.js', () => {
return {
connectTimeout: 1,
backoffInitialValue: 1000,
backoffLimit: 1000,
backoffJitter: 1,
};
});

const originalConsoleDebug = console.debug;
const consoleDebugMock = vi.fn();

Expand Down Expand Up @@ -211,7 +220,7 @@ test('should send info of resources in all reachable contexts and nothing in non
deployments: [{}, {}, {}, {}, {}],
},
} as ContextState);
await new Promise(resolve => setTimeout(resolve, 1200));
await new Promise(resolve => setTimeout(resolve, 2));
expect(apiSenderSendMock).toHaveBeenCalledWith('kubernetes-contexts-state-update', expectedMap);

// => removing contexts, should remving clusters from sent info
Expand Down Expand Up @@ -282,7 +291,7 @@ test('should send info of resources in all reachable contexts and nothing in non
deployments: [{}, {}, {}, {}],
},
} as ContextState);
await new Promise(resolve => setTimeout(resolve, 1200));
await new Promise(resolve => setTimeout(resolve, 2));
expect(apiSenderSendMock).toHaveBeenCalledWith('kubernetes-contexts-state-update', expectedMap);
});

Expand Down
12 changes: 9 additions & 3 deletions packages/main/src/plugin/kubernetes-context-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ import type {
import { AppsV1Api, CoreV1Api, KubeConfig, makeInformer } from '@kubernetes/client-node';
import type { KubeContext } from './kubernetes-context.js';
import type { ApiSenderType } from './api.js';
import {
backoffInitialValue,
backoffJitter,
backoffLimit,
connectTimeout,
} from './kubernetes-context-state-constants.js';

// ContextInternalState stores informers for a kube context
interface ContextInternalState {
Expand Down Expand Up @@ -201,7 +207,7 @@ export class ContextsManager {
return this.createInformer<V1Pod>(kc, context, path, listFn, {
resource: 'pods',
timer: this.podTimer,
backoff: new Backoff(1000, 60_000, 300),
backoff: new Backoff(backoffInitialValue, backoffLimit, backoffJitter),
onAdd: obj => this.setStateAndDispatch(context.name, state => state.resources.pods.push(obj)),
onDelete: obj =>
this.setStateAndDispatch(
Expand All @@ -228,7 +234,7 @@ export class ContextsManager {
return this.createInformer<V1Deployment>(kc, context, path, listFn, {
resource: 'deployments',
timer: this.deploymentTimer,
backoff: new Backoff(1000, 60_000, 300),
backoff: new Backoff(backoffInitialValue, backoffLimit, backoffJitter),
onAdd: obj => this.setStateAndDispatch(context.name, state => state.resources.deployments.push(obj)),
onDelete: obj =>
this.setStateAndDispatch(
Expand Down Expand Up @@ -319,7 +325,7 @@ export class ContextsManager {
clearTimeout(this.timeoutId);
this.timeoutId = setTimeout(() => {
this.apiSender.send(`kubernetes-contexts-state-update`, this.states.getPublished());
}, 1000);
}, connectTimeout);
}

public getContextsState(): Map<string, ContextState> {
Expand Down

0 comments on commit e69e5eb

Please sign in to comment.