Skip to content

Commit

Permalink
Merge pull request #2878 from andrewbaldwin44/bugfix/chart-fix
Browse files Browse the repository at this point in the history
Webui Remove chart initial data fetch
  • Loading branch information
cyberw authored Aug 30, 2024
2 parents 4e29bf6 + cd1ec17 commit b65dd78
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
26 changes: 17 additions & 9 deletions locust/webui/src/hooks/tests/useFetchStats.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { act, waitFor } from '@testing-library/react';
import { act } from '@testing-library/react';
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
import { beforeAll, afterEach, afterAll, describe, expect, test, vi } from 'vitest';
Expand All @@ -23,27 +23,32 @@ function MockHook() {
}

describe('useFetchStats', () => {
beforeAll(() => server.listen());
beforeAll(() => {
server.listen();
vi.useFakeTimers();
});
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
afterAll(() => {
server.close();
vi.useRealTimers();
});

test('should fetch request stats and update UI accordingly', async () => {
const { store } = renderWithProvider(<MockHook />, {
swarm: { state: SWARM_STATE.RUNNING },
});

await waitFor(() => {
if (!store.getState().ui.stats.length) {
throw new Error();
}
await act(async () => {
await vi.advanceTimersByTimeAsync(2000);
});
await act(async () => {
await vi.advanceTimersByTimeAsync(2000);
});

expect(store.getState().ui.stats).toEqual(statsResponseTransformed.stats);
});

test('should add markers to charts between tests', async () => {
vi.useFakeTimers();

const testStopTime = new Date().toISOString();

const { store } = renderWithProvider(<MockHook />, {
Expand All @@ -62,6 +67,9 @@ describe('useFetchStats', () => {
store.dispatch(swarmActions.setSwarm({ state: SWARM_STATE.RUNNING }));
});

await act(async () => {
await vi.advanceTimersByTimeAsync(2000);
});
await act(async () => {
await vi.advanceTimersByTimeAsync(2000);
});
Expand Down
12 changes: 0 additions & 12 deletions locust/webui/src/hooks/useFetchStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export default function useFetchStats() {
const updateChartMarkers = useAction(uiActions.updateChartMarkers);
const swarm = useSelector(({ swarm }) => swarm);
const previousSwarmState = useRef(swarm.state);
const hasSetInitStats = useRef(false);
const [shouldAddMarker, setShouldAddMarker] = useState(false);

const { data: statsData, refetch: refetchStats } = useGetStatsQuery();
Expand Down Expand Up @@ -89,17 +88,6 @@ export default function useFetchStats() {
}
}, [statsData && statsData.state]);

useEffect(() => {
if (statsData) {
if (!hasSetInitStats.current) {
// handle setting stats on first load
updateStats();
}

hasSetInitStats.current = true;
}
}, [statsData]);

useInterval(updateStats, STATS_REFETCH_INTERVAL, {
shouldRunInterval: !!statsData && shouldRunRefetchInterval,
});
Expand Down

0 comments on commit b65dd78

Please sign in to comment.