Skip to content

Commit

Permalink
explorer: Remove market poll restart after an error
Browse files Browse the repository at this point in the history
Resolves #1938
  • Loading branch information
ascartabelli committed Jul 5, 2024
1 parent 26b1824 commit 296fdc0
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 125 deletions.
117 changes: 1 addition & 116 deletions explorer/src/lib/stores/__tests__/marketDataStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe("marketDataStore", async () => {
});
});

it("should not reset its data and continue polling after an error, without resetting it as well", async () => {
it("should not reset its data and stop the polling after an error, without resetting it as well", async () => {
const error = new Error("Some error message");

/**
Expand Down Expand Up @@ -143,130 +143,15 @@ describe("marketDataStore", async () => {

await vi.advanceTimersByTimeAsync(settleTime);

/**
* The store is loading because after an error the polling
* restarts immediately and we see only the last store update here.
*/
expect(get(marketDataStore)).toStrictEqual({
...storeA,
error,
isLoading: true,
});

await vi.advanceTimersByTimeAsync(settleTime);

expect(get(marketDataStore)).toStrictEqual({
data: fakeMarketDataB,
error: null,
isLoading: false,
lastUpdate: new Date(),
});
});

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", () => {
Expand Down
9 changes: 0 additions & 9 deletions explorer/src/lib/stores/marketDataStore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { derived, get } from "svelte/store";
import { getPathIn } from "lamb";

import { createPollingDataStore } from "$lib/dusk/svelte-stores";
import { duskAPI } from "$lib/services";
Expand All @@ -23,11 +22,7 @@ const marketDataStore = derived(
($pollingDataStore, set) => {
const current = get(marketDataStore);
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 @@ -39,10 +34,6 @@ const marketDataStore = derived(
? current.lastUpdate
: null,
});

if (isErrorChanged && isRecoverableError) {
pollingDataStore.start();
}
},
initialState
);
Expand Down

0 comments on commit 296fdc0

Please sign in to comment.