PromucFlow_constructor/app/client/src/utils/WorkerUtil.test.ts
Ilia d6f249b42d
chore: add blank line eslint rule (#36369)
## Description
Added ESLint rule to force blank lines between statements. 


Fixes #`Issue Number`  
_or_  
Fixes `Issue URL`
> [!WARNING]  
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._

## Automation

/ok-to-test tags="@tag.All"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!CAUTION]
> 🔴 🔴 🔴 Some tests have failed.
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/10924926728>
> Commit: 34f57714a1575ee04e94e03cbcaf95e57a96c86c
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=10924926728&attempt=1&selectiontype=test&testsstatus=failed&specsstatus=fail"
target="_blank">Cypress dashboard</a>.
> Tags: @tag.All
> Spec: 
> The following are new failures, please fix them before merging the PR:
<ol>
> <li>cypress/e2e/Regression/ClientSide/Anvil/AnvilModal_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilCheckboxGroupWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilCurrencyInputWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilIconButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilInlineButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilInputWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilParagraphWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilPhoneInputWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilStatsWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilSwitchGroupWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilSwitchWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilTableWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilToolbarButtonWidgetSnapshot_spec.ts
>
<li>cypress/e2e/Regression/ClientSide/Anvil/Widgets/AnvilZoneSectionWidgetSnapshot_spec.ts</ol>
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/identified-flaky-tests-65890b3c81d7400d08fa9ee3?branch=master"
target="_blank">List of identified flaky tests</a>.
> <hr>Wed, 18 Sep 2024 16:33:36 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No

---------

Co-authored-by: Valera Melnikov <valera@appsmith.com>
2024-09-18 19:35:28 +03:00

267 lines
8.3 KiB
TypeScript

import { GracefulWorkerService } from "./WorkerUtil";
import { runSaga } from "redux-saga";
const MessageType = "message";
interface extraWorkerProperties {
callback: CallableFunction;
noop: CallableFunction;
delayMilliSeconds: number;
running: boolean;
}
type WorkerClass = Worker & extraWorkerProperties;
class MockWorkerClass implements WorkerClass {
// Implement interface
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onmessage: any;
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onmessageerror: any;
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
dispatchEvent: any;
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onerror: any;
callback: CallableFunction;
noop: CallableFunction;
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
messages: Array<any>;
delayMilliSeconds: number;
instance: WorkerClass | undefined;
responses: Set<number>;
running: boolean;
resetInstance() {
this.instance = undefined;
}
constructor() {
/* eslint-disable @typescript-eslint/no-empty-function */
this.noop = () => {};
this.callback = this.noop;
this.messages = [];
this.delayMilliSeconds = 0;
this.responses = new Set<number>();
this.instance = this;
this.running = true;
}
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
addEventListener(msgType: string, callback: any) {
expect(msgType).toEqual(MessageType);
this.callback = callback;
}
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
removeEventListener(msgType: string, callback: any) {
expect(msgType).toEqual(MessageType);
expect(callback).toEqual(this.callback);
this.callback = this.noop;
}
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
postMessage(message: any) {
expect(this.running).toEqual(true);
expect(this.callback).not.toEqual(this.noop);
this.messages.push(message);
const counter = setTimeout(() => {
const response = {
messageId: message.messageId,
messageType: "RESPONSE",
body: { data: message.body.data },
};
this.sendEvent({ data: response });
this.responses.delete(counter);
}, this.delayMilliSeconds);
this.responses.add(counter);
}
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
sendEvent(ev: any) {
expect(this.running).toEqual(true);
expect(this.callback).not.toEqual(this.noop);
this.callback(ev);
}
terminate() {
this.running = false;
expect(this.callback).toEqual(this.noop);
this.responses.forEach((counter) => {
clearTimeout(counter);
});
this.responses = new Set<number>();
}
}
describe("GracefulWorkerService", () => {
test("Worker should start", async () => {
const MockWorker = new MockWorkerClass();
const w = new GracefulWorkerService(MockWorker);
// wait for worker to start
await runSaga({}, w.start);
if (MockWorker.instance === undefined) {
expect(MockWorker.instance).toBeDefined();
return;
}
expect(MockWorker.instance.callback).not.toEqual(MockWorker.instance.noop);
});
test("Independent requests should respond independently irrespective of order", async () => {
const MockWorker = new MockWorkerClass();
const w = new GracefulWorkerService(MockWorker);
await runSaga({}, w.start);
const message1 = { tree: "hello" };
const message2 = { tree: "world" };
// Send requests in order
const result1 = await runSaga({}, w.request, "test", message1);
const result2 = await runSaga({}, w.request, "test", message2);
// wait for responses out of order
const resp2 = await result2.toPromise();
const resp1 = await result1.toPromise();
expect(resp1).toEqual(message1);
expect(resp2).toEqual(message2);
});
test("Request should wait for ready", async () => {
const MockWorker = new MockWorkerClass();
const w = new GracefulWorkerService(MockWorker);
const message = { hello: "world" };
// Send a request before starting
const result = await runSaga({}, w.request, "test", message);
// trigger start after the worker is already waiting
runSaga({}, w.start);
const resp = await result.toPromise();
expect(resp).toEqual(message);
});
test("Worker should wait to drain in-flight requests before shutdown", async () => {
const MockWorker = new MockWorkerClass();
const w = new GracefulWorkerService(MockWorker);
const message = { hello: "world" };
await runSaga({}, w.start);
const start = performance.now();
// Need this to work with eslint
if (MockWorker.instance === undefined) {
expect(MockWorker.instance).toBeDefined();
return;
}
// Typical run takes less than 10ms
// we add a delay of 100ms to check if shutdown waited for pending requests.
MockWorker.instance.delayMilliSeconds = 100;
const result = await runSaga({}, w.request, "test", message);
// wait for shutdown
await (await runSaga({}, w.shutdown)).toPromise();
// Shutdown shouldn't happen till we get a response
expect(performance.now() - start).toBeGreaterThanOrEqual(
MockWorker.instance.delayMilliSeconds,
);
const resp = await result.toPromise();
expect(resp).toEqual(message);
});
test("Worker restart should work", async () => {
const MockWorker = new MockWorkerClass();
let w = new GracefulWorkerService(MockWorker);
const message1 = { tree: "hello" };
await runSaga({}, w.start);
// Need this to work with eslint
if (MockWorker.instance === undefined) {
expect(MockWorker.instance).toBeDefined();
return;
}
// Keep a reference to the old instance to check later
const oldInstance = MockWorker.instance;
const result1 = await runSaga({}, w.request, "test", message1);
expect(await result1.toPromise()).toEqual(message1);
// stop the worker
await (await runSaga({}, w.shutdown)).toPromise();
// Should have called terminate on worker
expect(oldInstance.running).toEqual(false);
// Send a message to the new worker before starting it
const newMockWorker = new MockWorkerClass();
w = new GracefulWorkerService(newMockWorker);
const message2 = { tree: "world" };
const result2 = await runSaga({}, w.request, "test", message2);
await runSaga({}, w.start);
// We should have a new instance of the worker
expect(newMockWorker.instance).not.toEqual(oldInstance);
// The new worker should get the correct message
expect(await result2.toPromise()).toEqual(message2);
});
test("Cancelling saga before starting up should not crash", async () => {
const MockWorker = new MockWorkerClass();
const w = new GracefulWorkerService(MockWorker);
const message = { tree: "hello" };
const task = await runSaga({}, w.request, "cancel_test", message);
// Start shutting down
const shutdown = await runSaga({}, w.shutdown);
task.cancel();
// wait for shutdown
await shutdown.toPromise();
expect(await task.toPromise()).not.toEqual(message);
});
test("Cancelled saga should clean up", async () => {
const MockWorker = new MockWorkerClass();
const w = new GracefulWorkerService(MockWorker);
const message = { tree: "hello" };
await runSaga({}, w.start);
// Need this to work with eslint
if (MockWorker.instance === undefined) {
expect(MockWorker.instance).toBeDefined();
return;
}
// Make sure we get a chance to cancel before the worker can respond
MockWorker.instance.delayMilliSeconds = 100;
const task = await runSaga({}, w.request, "cancel_test", message);
// Start shutting down
const shutdown = await runSaga({}, w.shutdown);
task.cancel();
// wait for shutdown
await shutdown.toPromise();
expect(await task.toPromise()).not.toEqual(message);
});
});