Skip to content

Commit

Permalink
Throttle Logs Endpoint from CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
amandeepsingh333 committed Sep 20, 2024
1 parent 676420e commit ec44d8b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/core/src/percy.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
} from './discovery.js';
import { WaitForJob } from './wait-for-job.js';

const MAX_SUGGESTION_CALLS = 10;

// A Percy instance will create a new build when started, handle snapshot creation, asset discovery,
// and resource uploads, and will finalize the build when stopped. Snapshots are processed
// concurrently and the build is not finalized until all snapshots have been handled.
Expand Down Expand Up @@ -72,6 +74,7 @@ export class Percy {
server = true,
port = 5338,
projectType = null,
suggestionsCallCounter = 0,
// options such as `snapshot` and `discovery` that are valid Percy config
// options which will become accessible via the `.config` property
...options
Expand All @@ -98,6 +101,7 @@ export class Percy {
this.delayUploads = this.skipUploads || !!delayUploads;
this.deferUploads = this.skipUploads || !!deferUploads;
this.labels = labels;
this.suggestionsCallCounter = suggestionsCallCounter;

this.client = new PercyClient({ token, clientInfo, environmentInfo, config, labels });
if (server) this.server = createPercyServer(this, port);
Expand Down Expand Up @@ -507,6 +511,13 @@ export class Percy {

async suggestionsForFix(errors, options = {}) {
try {
this.suggestionsCallCounter++;
if (this.suggestionsCallCounter > MAX_SUGGESTION_CALLS) {
if (this.suggestionsCallCounter === MAX_SUGGESTION_CALLS + 1) {
this.log.debug('Rate limit exceeded for Maximum allowed suggestions per build.');
}
return;
}
const suggestionResponse = await this.client.getErrorAnalysis(errors);
this.#displaySuggestionLogs(suggestionResponse, options);
} catch (e) {
Expand Down
48 changes: 48 additions & 0 deletions packages/core/test/percy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,54 @@ describe('Percy', () => {
});
});

describe('check for throttle logs endpoint from CLI', () => {
let maxSuggestionCalls = 10;
it('should increment suggestionsCallCounter and log a warning when the rate limit is reached', async () => {
spyOn(percy.client, 'getErrorAnalysis').and.returnValue([{
suggestion: 'some suggestion',
failure_reason: 'some failure reason'
}]);
percy.loglevel('debug');

for (let i = 0; i < maxSuggestionCalls; i++) {
await expectAsync(percy.suggestionsForFix('some_error')).toBeResolved();
}

await expectAsync(percy.suggestionsForFix('some_error')).toBeResolved();
expect(logger.stderr).toEqual(jasmine.arrayContaining([
'[percy:core] Rate limit exceeded for Maximum allowed suggestions per build.'
]));
});

it('should not call getErrorAnalysis after exceeding the rate limit', async () => {
spyOn(percy.client, 'getErrorAnalysis').and.returnValue([{
suggestion: 'some suggestion',
failure_reason: 'some failure reason'
}]);
for (let i = 0; i <= maxSuggestionCalls; i++) {
await expectAsync(percy.suggestionsForFix('some_error')).toBeResolved();
}

await expectAsync(percy.suggestionsForFix('some_error')).toBeResolved();

expect(percy.client.getErrorAnalysis.calls.count()).toBe(maxSuggestionCalls);
});

it('should printed debug log for rate limiting only once, even after it exceeded multiple times', async () => {
spyOn(percy.log, 'debug').and.callThrough();
percy.loglevel('debug');

for (let i = 0; i <= maxSuggestionCalls + 5; i++) {
await expectAsync(percy.suggestionsForFix('some_error')).toBeResolved();
}

expect(percy.log.debug).toHaveBeenCalledWith(
'Rate limit exceeded for Maximum allowed suggestions per build.'
);
expect(percy.log.debug.calls.count()).toBe(1);
});
});

describe('when response throw error', () => {
describe('when Request failed with error code of EHOSTUNREACH', () => {
it('should catch and logs expected error', async () => {
Expand Down

0 comments on commit ec44d8b

Please sign in to comment.