Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

explorer: Avoid restart market data polling in case of 429 error #1928

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions explorer/src/lib/stores/__tests__/marketDataStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,112 @@ describe("marketDataStore", async () => {
});
});

it("should not restart the polling if the cause of the error is a `Response` with a 429 status", async () => {
const error = new Error("Some error message");

error.cause = new Response("", { status: 429 });

/**
* This is the result for the second call as the first one
* starts with the import and isn't resolved yet
*/
vi.mocked(duskAPI.getMarketData).mockImplementationOnce(() =>
rejectAfter(settleTime, error)
);

await vi.advanceTimersByTimeAsync(settleTime);

const storeA = {
data: fakeMarketDataA,
error: null,
isLoading: false,
lastUpdate: new Date(),
};

expect(duskAPI.getMarketData).toHaveBeenCalledTimes(1);
expect(get(marketDataStore)).toStrictEqual(storeA);

await vi.advanceTimersByTimeAsync(marketDataFetchInterval + settleTime);

const storeB = {
...storeA,
error,
isLoading: false,
};

expect(duskAPI.getMarketData).toHaveBeenCalledTimes(2);
expect(get(marketDataStore)).toStrictEqual(storeB);

await vi.advanceTimersByTimeAsync(marketDataFetchInterval * 10);

expect(duskAPI.getMarketData).toHaveBeenCalledTimes(2);
expect(get(marketDataStore)).toStrictEqual(storeB);
});

it("should restart the polling as usual if the cause of the error is a `Response` with a different status", async () => {
/**
* We make a subscription to avoid checking again the
* status of the store for every step as we already
* tested it before.
*/
const unubscribe = marketDataStore.subscribe(() => {});
const error = new Error("Some error message");

error.cause = new Response("", { status: 500 });

/**
* These are the results for the second and third call
* as the first one starts with the import and isn't resolved yet
*/
vi.mocked(duskAPI.getMarketData)
.mockImplementationOnce(() => rejectAfter(settleTime, error))
.mockImplementationOnce(() => resolveAfter(settleTime, fakeMarketDataB));

expect(duskAPI.getMarketData).toHaveBeenCalledTimes(1);

await vi.advanceTimersByTimeAsync(settleTime + marketDataFetchInterval);

expect(duskAPI.getMarketData).toHaveBeenCalledTimes(2);

await vi.advanceTimersByTimeAsync(settleTime);

expect(duskAPI.getMarketData).toHaveBeenCalledTimes(3);

unubscribe();
});

it("should restart the polling as usual if the cause of the error is anything else", async () => {
/**
* We make a subscription to avoid checking again the
* status of the store for every step as we already
* tested it before.
*/
const unubscribe = marketDataStore.subscribe(() => {});
const error = new Error("Some error message");

error.cause = new Error("some other error");

/**
* These are the results for the second and third call
* as the first one starts with the import and isn't resolved yet
*/
vi.mocked(duskAPI.getMarketData)
.mockImplementationOnce(() => rejectAfter(settleTime, error))
.mockImplementationOnce(() => resolveAfter(settleTime, fakeMarketDataB));

expect(duskAPI.getMarketData).toHaveBeenCalledTimes(1);

await vi.advanceTimersByTimeAsync(settleTime + marketDataFetchInterval);

expect(duskAPI.getMarketData).toHaveBeenCalledTimes(2);

await vi.advanceTimersByTimeAsync(settleTime);

expect(duskAPI.getMarketData).toHaveBeenCalledTimes(3);

unubscribe();
});

describe("Stale data checks", () => {
const startingStore = {
data: null,
Expand Down
6 changes: 5 additions & 1 deletion explorer/src/lib/stores/marketDataStore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { derived, get } from "svelte/store";
import { getPathIn } from "lamb";

import { createPollingDataStore } from "$lib/dusk/svelte-stores";
import { duskAPI } from "$lib/services";
Expand All @@ -24,6 +25,9 @@ const marketDataStore = derived(
const isDataChanged = $pollingDataStore.data !== current.data;
const isErrorChanged = $pollingDataStore.error !== current.error;
const hasNewData = $pollingDataStore.data && isDataChanged;
const isRecoverableError =
$pollingDataStore.error &&
getPathIn($pollingDataStore, "error.cause.status") !== 429;

set({
data: $pollingDataStore.data ?? current.data,
Expand All @@ -36,7 +40,7 @@ const marketDataStore = derived(
: null,
});

if ($pollingDataStore.error && isErrorChanged) {
if (isErrorChanged && isRecoverableError) {
pollingDataStore.start();
}
},
Expand Down
Loading