From 109551950a344517d29b742ddae028478d1f1c1f Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Mon, 26 Apr 2021 15:51:17 -0400 Subject: [PATCH] try and get jest to not core dump Signed-off-by: Sebastian Malton --- src/common/__tests__/cluster-store.test.ts | 6 ++++- src/common/base-store.ts | 6 ++--- src/common/cluster-store.ts | 27 +++++++++++-------- src/common/ipc/ipc.ts | 8 +++--- .../__tests__/extension-discovery.test.ts | 2 ++ src/main/logger.ts | 4 +-- 6 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/common/__tests__/cluster-store.test.ts b/src/common/__tests__/cluster-store.test.ts index 2dc36b795c..6fb5f40659 100644 --- a/src/common/__tests__/cluster-store.test.ts +++ b/src/common/__tests__/cluster-store.test.ts @@ -43,13 +43,17 @@ jest.mock("electron", () => { }, ipcMain: { handle: jest.fn(), - on: jest.fn() + on: jest.fn(), + removeAllListeners: jest.fn(), + off: jest.fn(), + send: jest.fn(), } }; }); describe("empty config", () => { beforeEach(async () => { + ClusterStore.getInstance(false)?.unregisterIpcListener(); ClusterStore.resetInstance(); const mockOpts = { "tmp": { diff --git a/src/common/base-store.ts b/src/common/base-store.ts index 91d3ef8312..4597059dca 100644 --- a/src/common/base-store.ts +++ b/src/common/base-store.ts @@ -124,8 +124,8 @@ export abstract class BaseStore extends Singleton { } unregisterIpcListener() { - ipcRenderer.removeAllListeners(this.syncMainChannel); - ipcRenderer.removeAllListeners(this.syncRendererChannel); + ipcRenderer?.removeAllListeners(this.syncMainChannel); + ipcRenderer?.removeAllListeners(this.syncRendererChannel); } disableSync() { @@ -167,7 +167,7 @@ export abstract class BaseStore extends Singleton { /** * toJSON is called when syncing the store to the filesystem. It should - * produce a JSON serializable object representaion of the current state. + * produce a JSON serializable object representation of the current state. * * It is recommended that a round trip is valid. Namely, calling * `this.fromStore(this.toJSON())` shouldn't change the state. diff --git a/src/common/cluster-store.ts b/src/common/cluster-store.ts index 56deb35758..57cf900f57 100644 --- a/src/common/cluster-store.ts +++ b/src/common/cluster-store.ts @@ -1,5 +1,5 @@ import path from "path"; -import { app, ipcRenderer, remote, webFrame } from "electron"; +import { app, ipcMain, ipcRenderer, remote, webFrame } from "electron"; import { unlink } from "fs-extra"; import { action, comparer, computed, observable, reaction, toJS } from "mobx"; import { BaseStore } from "./base-store"; @@ -12,6 +12,7 @@ import { saveToAppFiles } from "./utils/saveToAppFiles"; import { KubeConfig } from "@kubernetes/client-node"; import { handleRequest, requestMain, subscribeToBroadcast, unsubscribeAllFromBroadcast } from "./ipc"; import { ResourceType } from "../renderer/components/cluster-settings/components/cluster-metrics-setting"; +import { disposer, noop } from "./utils"; export interface ClusterIconUpload { clusterId: string; @@ -111,6 +112,7 @@ export class ClusterStore extends BaseStore { @observable clusters = observable.map(); private static stateRequestChannel = "cluster:states"; + protected disposer = disposer(); constructor() { super({ @@ -143,7 +145,7 @@ export class ClusterStore extends BaseStore { cluster.setState(clusterState.state); } }); - } else { + } else if (ipcMain) { handleRequest(ClusterStore.stateRequestChannel, (): clusterStateSync[] => { const states: clusterStateSync[] = []; @@ -160,13 +162,16 @@ export class ClusterStore extends BaseStore { } protected pushStateToViewsAutomatically() { - if (!ipcRenderer) { - reaction(() => this.enabledClustersList, () => { - this.pushState(); - }); - reaction(() => this.connectedClustersList, () => { - this.pushState(); - }); + if (ipcMain) { + this.disposer.push( + reaction(() => this.enabledClustersList, () => { + this.pushState(); + }), + reaction(() => this.connectedClustersList, () => { + this.pushState(); + }), + () => unsubscribeAllFromBroadcast("cluster:state"), + ); } } @@ -180,7 +185,7 @@ export class ClusterStore extends BaseStore { unregisterIpcListener() { super.unregisterIpcListener(); - unsubscribeAllFromBroadcast("cluster:state"); + this.disposer(); } pushState() { @@ -288,7 +293,7 @@ export class ClusterStore extends BaseStore { // remove only custom kubeconfigs (pasted as text) if (cluster.kubeConfigPath == ClusterStore.getCustomKubeConfigPath(clusterId)) { - unlink(cluster.kubeConfigPath).catch(() => null); + await unlink(cluster.kubeConfigPath).catch(noop); } } } diff --git a/src/common/ipc/ipc.ts b/src/common/ipc/ipc.ts index b104b31f4a..ebb3520fa3 100644 --- a/src/common/ipc/ipc.ts +++ b/src/common/ipc/ipc.ts @@ -28,7 +28,7 @@ export async function broadcastMessage(channel: string, ...args: any[]) { if (ipcRenderer) { ipcRenderer.send(channel, ...args); - } else { + } else if (ipcMain) { ipcMain.emit(channel, ...args); } @@ -55,7 +55,7 @@ export async function broadcastMessage(channel: string, ...args: any[]) { export function subscribeToBroadcast(channel: string, listener: (...args: any[]) => any) { if (ipcRenderer) { ipcRenderer.on(channel, listener); - } else { + } else if (ipcMain) { ipcMain.on(channel, listener); } @@ -65,7 +65,7 @@ export function subscribeToBroadcast(channel: string, listener: (...args: any[]) export function unsubscribeFromBroadcast(channel: string, listener: (...args: any[]) => any) { if (ipcRenderer) { ipcRenderer.off(channel, listener); - } else { + } else if (ipcMain) { ipcMain.off(channel, listener); } } @@ -73,7 +73,7 @@ export function unsubscribeFromBroadcast(channel: string, listener: (...args: an export function unsubscribeAllFromBroadcast(channel: string) { if (ipcRenderer) { ipcRenderer.removeAllListeners(channel); - } else { + } else if (ipcMain) { ipcMain.removeAllListeners(channel); } } diff --git a/src/extensions/__tests__/extension-discovery.test.ts b/src/extensions/__tests__/extension-discovery.test.ts index d75b560e46..d07dfe975c 100644 --- a/src/extensions/__tests__/extension-discovery.test.ts +++ b/src/extensions/__tests__/extension-discovery.test.ts @@ -6,6 +6,8 @@ import { ExtensionDiscovery } from "../extension-discovery"; import os from "os"; import { Console } from "console"; +jest.setTimeout(60_000); + jest.mock("../../common/ipc"); jest.mock("chokidar", () => ({ watch: jest.fn() diff --git a/src/main/logger.ts b/src/main/logger.ts index 0ddc7bb1f7..f39c7618ad 100644 --- a/src/main/logger.ts +++ b/src/main/logger.ts @@ -1,6 +1,6 @@ import { app, remote } from "electron"; import winston from "winston"; -import { isDebugging } from "../common/vars"; +import { isDebugging, isTestEnv } from "../common/vars"; const logLevel = process.env.LOG_LEVEL ? process.env.LOG_LEVEL : isDebugging ? "debug" : "info"; const consoleOptions: winston.transports.ConsoleTransportOptions = { @@ -23,7 +23,7 @@ const logger = winston.createLogger({ ), transports: [ new winston.transports.Console(consoleOptions), - new winston.transports.File(fileOptions), + ...(isTestEnv ? [] : [new winston.transports.File(fileOptions)]), ], });