Skip to content

Commit

Permalink
fix(frontend): add polling mechanism for test run
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeepc committed May 4, 2023
1 parent 22f7dd1 commit c356c8e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 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
11 changes: 0 additions & 11 deletions web/src/redux/apis/endpoints/TestRun.endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,6 @@ const TestRunEndpoint = (builder: TTestApiEndpointBuilder) => ({
query: ({testId, runId}) => `/tests/${testId}/run/${runId}`,
providesTags: result => (result ? [{type: TracetestApiTags.TEST_RUN, id: result?.id}] : []),
transformResponse: (rawTestResult: TRawTestRun) => TestRun(rawTestResult),
async onCacheEntryAdded(arg, {cacheDataLoaded, cacheEntryRemoved, updateCachedData}) {
const listener: IListenerFunction<TRawTestRun> = data => {
updateCachedData(() => TestRun(data.event));
};
await WebSocketService.initWebSocketSubscription({
listener,
resource: `test/${arg.testId}/run/${arg.runId}`,
waitToCleanSubscription: cacheEntryRemoved,
waitToInitSubscription: cacheDataLoaded,
});
},
}),
reRun: builder.mutation<TestRun, {testId: string; runId: string}>({
query: ({testId, runId}) => ({
Expand Down

0 comments on commit c356c8e

Please sign in to comment.