mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Retry watch if timeout set, but request was not retried (#4388)
* Retry watch if timeout set, but request was not retried. Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com> * Cancel current request if retried. Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com> * Clarify retried variable/comment Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>
This commit is contained in:
1 parent
7d4a2a5735
commit
0ca8448d33
2 files changed
+175
-6
No files matched your search
@@ -23,6 +23,8 @@ import type { Request } from "node-fetch";
|
||||
import { forRemoteCluster, KubeApi } from "../kube-api";
|
||||
import { KubeJsonApi } from "../kube-json-api";
|
||||
import { KubeObject } from "../kube-object";
|
||||
import AbortController from "abort-controller";
|
||||
import { delay } from "../../utils/delay";
|
||||
|
||||
class TestKubeObject extends KubeObject {
|
||||
static kind = "Pod";
|
||||
@@ -325,5 +327,126 @@ describe("KubeApi", () => {
|
||||
api.watch({ namespace: "kube-system", timeout: 60 });
|
||||
expect(spy).toHaveBeenCalledWith("/api/v1/namespaces/kube-system/pods?watch=1&resourceVersion=", { query: { timeoutSeconds: 60 }}, expect.anything());
|
||||
});
|
||||
|
||||
it("aborts watch using abortController", async (done) => {
|
||||
const spy = jest.spyOn(request, "getResponse");
|
||||
|
||||
(fetch as any).mockResponse(async (request: Request) => {
|
||||
(request as any).signal.addEventListener("abort", () => {
|
||||
done();
|
||||
});
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
const abortController = new AbortController();
|
||||
|
||||
api.watch({
|
||||
namespace: "kube-system",
|
||||
timeout: 60,
|
||||
abortController,
|
||||
});
|
||||
|
||||
expect(spy).toHaveBeenCalledWith("/api/v1/namespaces/kube-system/pods?watch=1&resourceVersion=", { query: { timeoutSeconds: 60 }}, expect.anything());
|
||||
|
||||
await delay(100);
|
||||
|
||||
abortController.abort();
|
||||
});
|
||||
|
||||
describe("retries", () => {
|
||||
it("if request ended", (done) => {
|
||||
const spy = jest.spyOn(request, "getResponse");
|
||||
|
||||
// we need to mock using jest as jest-fetch-mock doesn't support mocking the body completely
|
||||
jest.spyOn(global, "fetch").mockImplementation(async () => {
|
||||
return {
|
||||
ok: true,
|
||||
body: {
|
||||
on: (eventName: string, callback: Function) => {
|
||||
// End the request in 100ms.
|
||||
if (eventName === "end") {
|
||||
setTimeout(() => {
|
||||
callback();
|
||||
}, 100);
|
||||
}
|
||||
},
|
||||
},
|
||||
} as any;
|
||||
});
|
||||
|
||||
api.watch({
|
||||
namespace: "kube-system",
|
||||
});
|
||||
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
|
||||
setTimeout(() => {
|
||||
expect(spy).toHaveBeenCalledTimes(2);
|
||||
done();
|
||||
}, 2000);
|
||||
});
|
||||
|
||||
it("if request not closed after timeout", (done) => {
|
||||
const spy = jest.spyOn(request, "getResponse");
|
||||
|
||||
(fetch as any).mockResponse(async () => {
|
||||
return {};
|
||||
});
|
||||
|
||||
const timeoutSeconds = 1;
|
||||
|
||||
api.watch({
|
||||
namespace: "kube-system",
|
||||
timeout: timeoutSeconds,
|
||||
});
|
||||
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
|
||||
setTimeout(() => {
|
||||
expect(spy).toHaveBeenCalledTimes(2);
|
||||
done();
|
||||
}, timeoutSeconds * 1000 * 1.2);
|
||||
});
|
||||
|
||||
it("retries only once if request ends and timeout is set", (done) => {
|
||||
const spy = jest.spyOn(request, "getResponse");
|
||||
|
||||
// we need to mock using jest as jest-fetch-mock doesn't support mocking the body completely
|
||||
jest.spyOn(global, "fetch").mockImplementation(async () => {
|
||||
return {
|
||||
ok: true,
|
||||
body: {
|
||||
on: (eventName: string, callback: Function) => {
|
||||
// End the request in 100ms
|
||||
if (eventName === "end") {
|
||||
setTimeout(() => {
|
||||
callback();
|
||||
}, 100);
|
||||
}
|
||||
},
|
||||
},
|
||||
} as any;
|
||||
});
|
||||
|
||||
const timeoutSeconds = 0.5;
|
||||
|
||||
api.watch({
|
||||
namespace: "kube-system",
|
||||
timeout: timeoutSeconds,
|
||||
});
|
||||
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
|
||||
setTimeout(() => {
|
||||
expect(spy).toHaveBeenCalledTimes(2);
|
||||
done();
|
||||
}, 2000);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user