Skip to content

Commit

Permalink
feat: add fe polling and disable websocket (#3151)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeepc committed Sep 12, 2023
1 parent 467019d commit 619f936
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 57 deletions.
3 changes: 2 additions & 1 deletion web/src/gateways/WebSocket.gateway.ts
Expand Up @@ -172,6 +172,7 @@ function getWebSocketURL() {
}

const webSocketGateway = WebSocketGateway({url: getWebSocketURL()});
webSocketGateway.connect();
// Disable websocket connection for now
// webSocketGateway.connect();

export default webSocketGateway;
4 changes: 4 additions & 0 deletions web/src/models/TestSuiteRun.model.ts
Expand Up @@ -12,6 +12,10 @@ type TestSuiteRun = Model<
}
>;

export function isRunStateFinished(state: string) {
return ['FINISHED', 'FAILED'].includes(state);
}

const TestSuiteRun = ({
id = 0,
createdAt = '',
Expand Down
2 changes: 1 addition & 1 deletion web/src/providers/TestRun/TestRun.provider.tsx
Expand Up @@ -37,7 +37,7 @@ const POLLING_INTERVAL = 5000;
const TestRunProvider = ({children, testId, runId = 0}: IProps) => {
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 {data: runEvents = []} = useGetRunEventsQuery({testId, runId}, {skip: !runId, pollingInterval});
const [stopRunAction, {isLoading: isLoadingStop}] = useStopRunMutation();

const stopRun = useCallback(async () => {
Expand Down
14 changes: 11 additions & 3 deletions web/src/providers/TestSuiteRun/TestSuite.provider.tsx
@@ -1,6 +1,6 @@
import {createContext, useContext, useMemo} from 'react';
import {createContext, useContext, useEffect, useMemo, useState} from 'react';
import TracetestAPI from 'redux/apis/Tracetest';
import TestSuiteRun from 'models/TestSuiteRun.model';
import TestSuiteRun, {isRunStateFinished} from 'models/TestSuiteRun.model';
import TestSuiteProvider from '../TestSuite/TestSuite.provider';

const {useGetTestSuiteRunByIdQuery} = TracetestAPI.instance;
Expand All @@ -21,10 +21,18 @@ interface IProps {

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

const POLLING_INTERVAL = 5000;

const TestSuiteRunProvider = ({children, testSuiteId, runId}: IProps) => {
const {data: run} = useGetTestSuiteRunByIdQuery({testSuiteId, runId});
const [pollingInterval, setPollingInterval] = useState<number | undefined>(POLLING_INTERVAL);
const {data: run} = useGetTestSuiteRunByIdQuery({testSuiteId, runId}, {pollingInterval});
const value = useMemo<IContext>(() => ({run: run!}), [run]);

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

return run ? (
<TestSuiteProvider testSuiteId={testSuiteId} version={run.version}>
<Context.Provider value={value}>{children}</Context.Provider>
Expand Down
16 changes: 2 additions & 14 deletions web/src/redux/apis/Tracetest/endpoints/Setting.endpoint.ts
@@ -1,13 +1,12 @@
import {HTTP_METHOD} from 'constants/Common.constants';
import {TracetestApiTags} from 'constants/Test.constants';
import Config, {TRawConfig, TRawLiveConfig} from 'models/Config.model';
import Config, {TRawConfig} from 'models/Config.model';
import Demo, {TRawDemo} from 'models/Demo.model';
import Linter, {TRawLinter} from 'models/Linter.model';
import Polling, {TRawPolling} from 'models/Polling.model';
import TestRunner, {TRawTestRunnerResource} from 'models/TestRunner.model';
import WebSocketService, {IListenerFunction} from 'services/WebSocket.service';
import {ResourceType, TDraftResource, TListResponse} from 'types/Settings.types';
import { TTestApiEndpointBuilder } from '../Tracetest.api';
import {TTestApiEndpointBuilder} from '../Tracetest.api';

export const settingsEndpoints = (builder: TTestApiEndpointBuilder) => ({
getConfig: builder.query<Config, unknown>({
Expand All @@ -20,17 +19,6 @@ export const settingsEndpoints = (builder: TTestApiEndpointBuilder) => ({
}),
providesTags: () => [{type: TracetestApiTags.SETTING, id: ResourceType.ConfigType}],
transformResponse: (rawConfig: TRawConfig) => Config(rawConfig),
async onCacheEntryAdded(arg, {cacheDataLoaded, cacheEntryRemoved, updateCachedData}) {
const listener: IListenerFunction<TRawLiveConfig> = data => {
updateCachedData(() => Config.FromLiveUpdate(data.event));
};
await WebSocketService.initWebSocketSubscription({
listener,
resource: '/app/config/update',
waitToCleanSubscription: cacheEntryRemoved,
waitToInitSubscription: cacheDataLoaded,
});
},
}),
getPolling: builder.query<Polling, unknown>({
query: () => ({
Expand Down
25 changes: 0 additions & 25 deletions web/src/redux/apis/Tracetest/endpoints/TestRun.endpoint.ts
Expand Up @@ -9,7 +9,6 @@ import Test from 'models/Test.model';
import TestRun, {TRawTestRun} from 'models/TestRun.model';
import TestRunEvent, {TRawTestRunEvent} from 'models/TestRunEvent.model';
import {TRawTestSpecs} from 'models/TestSpecs.model';
import WebSocketService, {IListenerFunction} from 'services/WebSocket.service';
import {TTestApiEndpointBuilder} from '../Tracetest.api';

function getTotalCountFromHeaders(meta: any) {
Expand Down Expand Up @@ -46,17 +45,6 @@ export const testRunEndpoints = (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: number}>({
query: ({testId, runId}) => ({
Expand Down Expand Up @@ -108,18 +96,5 @@ export const testRunEndpoints = (builder: TTestApiEndpointBuilder) => ({
query: ({runId, testId}) => `/tests/${testId}/run/${runId}/events`,
providesTags: [{type: TracetestApiTags.TEST_RUN, id: 'EVENTS'}],
transformResponse: (rawTestRunEvent: TRawTestRunEvent[]) => rawTestRunEvent.map(event => TestRunEvent(event)),
async onCacheEntryAdded(arg, {cacheDataLoaded, cacheEntryRemoved, updateCachedData}) {
const listener: IListenerFunction<TRawTestRunEvent> = data => {
updateCachedData(draft => {
draft.push(TestRunEvent(data.event));
});
};
await WebSocketService.initWebSocketSubscription({
listener,
resource: `test/${arg.testId}/run/${arg.runId}/event`,
waitToCleanSubscription: cacheEntryRemoved,
waitToInitSubscription: cacheDataLoaded,
});
},
}),
});
13 changes: 0 additions & 13 deletions web/src/redux/apis/Tracetest/endpoints/TestSuiteRun.endpoint.ts
Expand Up @@ -4,7 +4,6 @@ import {PaginationResponse} from 'hooks/usePagination';
import {TVariableSetValue} from 'models/VariableSet.model';
import RunError from 'models/RunError.model';
import TestSuiteRun, {TRawTestSuiteRunResourceRun} from 'models/TestSuiteRun.model';
import WebSocketService, {IListenerFunction} from 'services/WebSocket.service';
import {getTotalCountFromHeaders} from 'utils/Common';
import {TTestApiEndpointBuilder} from '../Tracetest.api';

Expand Down Expand Up @@ -45,18 +44,6 @@ export const testSuiteRunEndpoints = (builder: TTestApiEndpointBuilder) => ({
query: ({testSuiteId, runId}) => `/testsuites/${testSuiteId}/run/${runId}`,
providesTags: result => [{type: TracetestApiTags.TESTSUITE_RUN, id: result?.id}],
transformResponse: (raw: TRawTestSuiteRunResourceRun) => TestSuiteRun(raw),
async onCacheEntryAdded(arg, {cacheDataLoaded, cacheEntryRemoved, updateCachedData}) {
const listener: IListenerFunction<TRawTestSuiteRunResourceRun> = data => {
updateCachedData(() => TestSuiteRun(data.event));
};

await WebSocketService.initWebSocketSubscription({
listener,
resource: `testsuites/${arg.testSuiteId}/run/${arg.runId}`,
waitToCleanSubscription: cacheEntryRemoved,
waitToInitSubscription: cacheDataLoaded,
});
},
}),

deleteTestSuiteRunById: builder.mutation<TestSuiteRun, {testSuiteId: string; runId: number}>({
Expand Down

0 comments on commit 619f936

Please sign in to comment.