mirror of
https://github.com/prymitive/karma
synced 2026-05-09 03:36:44 +00:00
Merge pull request #1277 from prymitive/fix-fetch-retry
fix(ui): retry all fetch GET calls
This commit is contained in:
@@ -1,12 +1,26 @@
|
||||
import merge from "lodash/merge";
|
||||
|
||||
import promiseRetry from "promise-retry";
|
||||
|
||||
const CommonOptions = {
|
||||
credentials: "include",
|
||||
redirect: "follow"
|
||||
};
|
||||
|
||||
const FetchGet = async (uri, options) =>
|
||||
await fetch(uri, merge({}, { method: "GET" }, CommonOptions, options));
|
||||
const FetchRetryConfig = {
|
||||
retries: 5,
|
||||
minTimeout: 1000,
|
||||
maxTimeout: 5000
|
||||
};
|
||||
|
||||
const FetchGet = async (uri, options, retryOptions) =>
|
||||
await promiseRetry(
|
||||
(retry, number) =>
|
||||
fetch(uri, merge({}, { method: "GET" }, CommonOptions, options)).catch(
|
||||
retry
|
||||
),
|
||||
FetchRetryConfig
|
||||
);
|
||||
|
||||
const FetchPost = async (uri, options) =>
|
||||
await fetch(uri, merge({}, { method: "POST" }, CommonOptions, options));
|
||||
@@ -14,4 +28,4 @@ const FetchPost = async (uri, options) =>
|
||||
const FetchDelete = async (uri, options) =>
|
||||
await fetch(uri, merge({}, { method: "DELETE" }, CommonOptions, options));
|
||||
|
||||
export { CommonOptions, FetchGet, FetchPost, FetchDelete };
|
||||
export { CommonOptions, FetchGet, FetchPost, FetchDelete, FetchRetryConfig };
|
||||
|
||||
@@ -18,7 +18,7 @@ beforeEach(() => {
|
||||
silenceFormStore = new SilenceFormStore();
|
||||
cluster = "am";
|
||||
silence = MockSilence();
|
||||
fetch.mockResponseOnce(JSON.stringify(MockAPIResponse()));
|
||||
fetch.mockResponse(JSON.stringify(MockAPIResponse()));
|
||||
|
||||
alertStore.data.upstreams = {
|
||||
instances: [
|
||||
@@ -127,8 +127,9 @@ describe("<DeleteSilenceModalContent />", () => {
|
||||
expect(tree.find("LabelSetList")).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("fetches affected alerts on mount", () => {
|
||||
MountedDeleteSilenceModalContent();
|
||||
it("fetches affected alerts on mount", async () => {
|
||||
const tree = MountedDeleteSilenceModalContent();
|
||||
await expect(tree.instance().previewState.fetch).resolves.toBeUndefined();
|
||||
expect(fetch).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
@@ -196,14 +196,14 @@ describe("<FilterInput Autosuggest />", () => {
|
||||
});
|
||||
|
||||
it("handles failed suggestion fetches", async () => {
|
||||
fetch.mockRejectOnce("Fetch error");
|
||||
fetch.mockReject("Fetch error");
|
||||
|
||||
const tree = MountedInput();
|
||||
const instance = tree.instance();
|
||||
tree.find("input").simulate("change", { target: { value: "bar" } });
|
||||
await WaitForFetch(tree);
|
||||
|
||||
expect(fetch.mock.calls).toHaveLength(1);
|
||||
expect(fetch.mock.calls).toHaveLength(6);
|
||||
expect(fetch.mock.calls[0]).toContain("./autocomplete.json?term=bar");
|
||||
expect(instance.inputStore.suggestions).toHaveLength(0);
|
||||
});
|
||||
|
||||
@@ -8,8 +8,6 @@ import qs from "qs";
|
||||
|
||||
import moment from "moment";
|
||||
|
||||
import promiseRetry from "promise-retry";
|
||||
|
||||
import { FetchGet } from "Common/Fetch";
|
||||
|
||||
const QueryStringEncodeOptions = {
|
||||
@@ -260,11 +258,6 @@ class AlertStore {
|
||||
|
||||
constructor(initialFilters) {
|
||||
this.filters.setFilters(initialFilters);
|
||||
this.retryConfig = {
|
||||
retries: 5,
|
||||
minTimeout: 1000,
|
||||
maxTimeout: 5000
|
||||
};
|
||||
}
|
||||
|
||||
fetch = action((sortOrder, sortLabel, sortReverse) => {
|
||||
@@ -275,9 +268,7 @@ class AlertStore {
|
||||
`alerts.json?sortOrder=${sortOrder}&sortLabel=${sortLabel}&sortReverse=${sortReverse}&`
|
||||
) + FormatAPIFilterQuery(this.filters.values.map(f => f.raw));
|
||||
|
||||
return promiseRetry((retry, number) => {
|
||||
return FetchGet(alertsURI, {}).catch(retry);
|
||||
}, this.retryConfig)
|
||||
return FetchGet(alertsURI, {})
|
||||
.then(result => {
|
||||
this.status.setProcessing();
|
||||
return result.json();
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import Enzyme from "enzyme";
|
||||
import Adapter from "enzyme-adapter-react-16";
|
||||
|
||||
import { FetchRetryConfig } from "Common/Fetch";
|
||||
|
||||
// https://github.com/airbnb/enzyme
|
||||
Enzyme.configure({ adapter: new Adapter() });
|
||||
|
||||
@@ -27,3 +29,6 @@ for (const level of ["error", "warn", "info", "log", "trace"]) {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
FetchRetryConfig.minTimeout = 2;
|
||||
FetchRetryConfig.maxTimeout = 10;
|
||||
|
||||
Reference in New Issue
Block a user