Skip to content

Commit

Permalink
fix(frontend): add polling mechanism for test run (#2489)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeepc authored and schoren committed May 9, 2023
1 parent 6b56a45 commit 90e02ee
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions web/src/providers/TestRun/TestRun.provider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {noop} from 'lodash';
import {createContext, useCallback, useContext, useMemo} from 'react';
import {createContext, useCallback, useContext, useEffect, useMemo, useState} from 'react';
import {useGetRunByIdQuery, useGetRunEventsQuery, useStopRunMutation} from 'redux/apis/TraceTest.api';
import TestRun from 'models/TestRun.model';
import TestRun, {isRunStateFinished} from 'models/TestRun.model';
import TestRunEvent from 'models/TestRunEvent.model';
import TestProvider from '../Test';

Expand Down Expand Up @@ -29,8 +29,11 @@ interface IProps {

export const useTestRun = () => useContext(Context);

const POLLING_INTERVAL = 5000;

const TestRunProvider = ({children, testId, runId = ''}: IProps) => {
const {data: run, isError} = useGetRunByIdQuery({testId, runId}, {skip: !runId});
const [pollingInterval, setPollingInterval] = useState<number | undefined>(POLLING_INTERVAL);
const {data: run, isError} = useGetRunByIdQuery({testId, runId}, {skip: !runId, pollingInterval});
const {data: runEvents = []} = useGetRunEventsQuery({testId, runId}, {skip: !runId});
const [stopRunAction, {isLoading: isLoadingStop}] = useStopRunMutation();

Expand All @@ -43,6 +46,11 @@ const TestRunProvider = ({children, testId, runId = ''}: IProps) => {
[run, isError, isLoadingStop, runEvents, stopRun]
);

useEffect(() => {
const shouldStopPolling = run?.state && isRunStateFinished(run.state);
setPollingInterval(shouldStopPolling ? undefined : POLLING_INTERVAL);
}, [run?.state]);

return run ? (
<Context.Provider value={value}>
<TestProvider testId={testId} version={run.testVersion}>
Expand Down

0 comments on commit 90e02ee

Please sign in to comment.