Skip to content

Commit

Permalink
NEW (Extension) @W-16371431@ Poll ApexGuru API and show the response …
Browse files Browse the repository at this point in the history
…as diagnostic - code review comments 2
  • Loading branch information
jag-j committed Aug 7, 2024
1 parent e6b7639 commit 3fb3e9d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
17 changes: 7 additions & 10 deletions src/apexguru/apex-guru-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ export async function runApexGuruOnFile(selection: vscode.Uri, runInfo: RunInfo)
}
}

export async function pollAndGetApexGuruResponse(connection: Connection, requestId: string, maxWaitTimeInSeconds: number, retryIntervalInMillis: number) {
export async function pollAndGetApexGuruResponse(connection: Connection, requestId: string, maxWaitTimeInSeconds: number, retryIntervalInMillis: number): Promise<ApexGuruQueryResponse> {
let queryResponse: ApexGuruQueryResponse;
let lastErrorMessage = '';
const startTime = Date.now();
while ((Date.now() - startTime) < maxWaitTimeInSeconds * 1000) {
try {
Expand All @@ -78,23 +79,19 @@ export async function pollAndGetApexGuruResponse(connection: Connection, request
url: `${Constants.APEX_GURU_REQUEST}/${requestId}`,
body: ''
});

if (queryResponse.status == 'success') {
return queryResponse;
} else {
// Add a delay between requests
await new Promise(resolve => setTimeout(resolve, retryIntervalInMillis));
}
}
} catch (error) {
await new Promise(resolve => setTimeout(resolve, retryIntervalInMillis));
lastErrorMessage = (error as Error).message;
}
}
await new Promise(resolve => setTimeout(resolve, retryIntervalInMillis));

}
if (queryResponse) {
return queryResponse;
} else {
throw new Error('Failed to get a successful response from Apex Guru after maximum retries');
}
throw new Error(`Failed to get a successful response from Apex Guru after maximum retries.${lastErrorMessage}`);
}

export async function initiateApexGuruRequest(selection: vscode.Uri, outputChannel: vscode.LogOutputChannel, connection: Connection): Promise<string> {
Expand Down
24 changes: 23 additions & 1 deletion src/test/suite/apexguru/apex-guru-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ suite('Apex Guru Test Suite', () => {
await ApexGuruFunctions.pollAndGetApexGuruResponse(connectionStub as unknown as Connection, requestId, maxWaitTimeInSeconds, retryInterval);
throw new Error('Expected to throw an error due to timeout');
} catch (error) {
expect(error.message).to.equal('Failed to get a successful response from Apex Guru after maximum retries');
expect(error.message).to.equal('Failed to get a successful response from Apex Guru after maximum retries.');
}

// ===== ASSERTIONS =====
Expand Down Expand Up @@ -286,5 +286,27 @@ suite('Apex Guru Test Suite', () => {
expect(response).to.deep.equal(successResponse);
expect(connectionStub.request.callCount).to.equal(3);
});

test('Throws last error if maximum retries are exhausted', async () => {
// ===== SETUP =====
const requestId = 'dummyRequestId';
const maxWaitTimeInSeconds = 1; // Set to 1 second for quick test
const retryInterval = 500;

const errorResponse: Error = new Error('Some dummy error');

connectionStub.request.rejects(errorResponse);

// ===== TEST =====
try {
await ApexGuruFunctions.pollAndGetApexGuruResponse(connectionStub as unknown as Connection, requestId, maxWaitTimeInSeconds, retryInterval);
expect.fail('Expected function to throw an error');
} catch (error) {
// ===== ASSERTIONS =====
expect((error as Error).message).to.contain('Failed to get a successful response from Apex Guru after maximum retries.Some dummy error');
}

expect(connectionStub.request.callCount).to.be.greaterThan(0);
});
});
});

0 comments on commit 3fb3e9d

Please sign in to comment.