From 3fb3e9dad6e3fa5e40be061845b37c293014a21e Mon Sep 17 00:00:00 2001 From: Jag Jayaprakash Date: Wed, 7 Aug 2024 10:01:02 -0700 Subject: [PATCH] NEW (Extension) @W-16371431@ Poll ApexGuru API and show the response as diagnostic - code review comments 2 --- src/apexguru/apex-guru-service.ts | 17 ++++++------- .../suite/apexguru/apex-guru-service.test.ts | 24 ++++++++++++++++++- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/apexguru/apex-guru-service.ts b/src/apexguru/apex-guru-service.ts index d90ba3f..78b57ec 100644 --- a/src/apexguru/apex-guru-service.ts +++ b/src/apexguru/apex-guru-service.ts @@ -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 { let queryResponse: ApexGuruQueryResponse; + let lastErrorMessage = ''; const startTime = Date.now(); while ((Date.now() - startTime) < maxWaitTimeInSeconds * 1000) { try { @@ -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 { diff --git a/src/test/suite/apexguru/apex-guru-service.test.ts b/src/test/suite/apexguru/apex-guru-service.test.ts index ae2ec93..446131d 100644 --- a/src/test/suite/apexguru/apex-guru-service.test.ts +++ b/src/test/suite/apexguru/apex-guru-service.test.ts @@ -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 ===== @@ -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); + }); }); });