Skip to content

Commit

Permalink
fix(clients): rename maxTrial to maxRetries (#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed Jun 9, 2022
1 parent 2465969 commit baaf767
Show file tree
Hide file tree
Showing 17 changed files with 624 additions and 348 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-title.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
- name: Pull Request title rules
uses: deepakputhraya/action-pr-title@v1.0.2
with:
regex: '^(docs|chore)|((?:feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)\((?:generators|javascript|php|java|cts|specs|scripts|ci|templates|deps)\)): .+'
regex: '^(docs|chore)|((?:feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)\((?:clients|generators|javascript|php|java|cts|specs|scripts|ci|templates|deps)\)): .+'
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@

public class TaskUtils {

public static final int DEFAULT_MAX_TRIAL = 50;
public static final int DEFAULT_MAX_RETRIES = 50;
public static final IntUnaryOperator DEFAULT_TIMEOUT = (int retries) -> {
return Math.min(retries * 200, 5000);
};

public static <TResponse> void retryUntil(
Supplier<CompletableFuture<TResponse>> func,
Predicate<TResponse> validate,
int maxTrial,
int maxRetries,
IntUnaryOperator timeout
) throws AlgoliaRuntimeException {
int retryCount = 0;
while (retryCount < maxTrial) {
while (retryCount < maxRetries) {
try {
TResponse resp = func.get().get();
if (validate.test(resp)) {
Expand All @@ -40,6 +40,6 @@ public static <TResponse> void retryUntil(

retryCount++;
}
throw new AlgoliaRetriesExceededException("The maximum number of trials exceeded. (" + (retryCount + 1) + "/" + maxTrial + ")");
throw new AlgoliaRetriesExceededException("The maximum number of retries exceeded. (" + (retryCount + 1) + "/" + maxRetries + ")");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"devDependencies": {
"@types/jest": "27.5.2",
"@types/node": "16.11.38",
"jest": "28.1.0",
"jest": "28.1.1",
"jest-environment-jsdom": "28.1.1",
"typescript": "4.6.3"
},
"engines": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,11 @@ describe('browser local storage cache', () => {
const key = { foo: 'bar' };
const value = 'foo';

expect(
localStorage.getItem(`algoliasearch-client-js-${version}`)
).toBeNull();
expect(localStorage.getItem(`algolia-client-js-${version}`)).toBeNull();

await cache.set(key, value);

expect(localStorage.getItem(`algoliasearch-client-js-${version}`)).toBe(
expect(localStorage.getItem(`algolia-client-js-${version}`)).toBe(
'{"{\\"foo\\":\\"bar\\"}":"foo"}'
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ describe('createRetryablePromise', () => {
);
});

it('gets the rejection when it exceeds the max trial number', async () => {
const MAX_TRIAL = 3;
it('gets the rejection when it exceeds the max retries number', async () => {
const MAX_RETRIES = 3;
let calls = 0;

const promise = createRetryablePromise({
Expand All @@ -73,14 +73,14 @@ describe('createRetryablePromise', () => {
});
},
validate: () => false,
maxTrial: MAX_TRIAL,
maxRetries: MAX_RETRIES,
});

await expect(promise).rejects.toEqual(
expect.objectContaining({
message: 'The maximum number of trials exceeded. (3/3)',
message: 'The maximum number of retries exceeded. (3/3)',
})
);
expect(calls).toBe(MAX_TRIAL);
expect(calls).toBe(MAX_RETRIES);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CreateRetryablePromiseOptions } from './types/CreateRetryablePromise';

export const DEFAULT_MAX_TRIAL = 50;
export const DEFAULT_MAX_RETRIES = 50;
export const DEFAULT_TIMEOUT = (retryCount: number): number =>
Math.min(retryCount * 200, 5000);

Expand All @@ -10,13 +10,13 @@ export const DEFAULT_TIMEOUT = (retryCount: number): number =>
* @param createRetryablePromiseOptions - The createRetryablePromise options.
* @param createRetryablePromiseOptions.func - The function to run, which returns a promise.
* @param createRetryablePromiseOptions.validate - The validator function. It receives the resolved return of `func`.
* @param createRetryablePromiseOptions.maxTrial - The maximum number of trials. 10 by default.
* @param createRetryablePromiseOptions.maxRetries - The maximum number of retries. 50 by default.
* @param createRetryablePromiseOptions.timeout - The function to decide how long to wait between tries.
*/
export function createRetryablePromise<TResponse>({
func,
validate,
maxTrial = DEFAULT_MAX_TRIAL,
maxRetries = DEFAULT_MAX_RETRIES,
timeout = DEFAULT_TIMEOUT,
}: CreateRetryablePromiseOptions<TResponse>): Promise<TResponse> {
let retryCount = 0;
Expand All @@ -27,12 +27,12 @@ export function createRetryablePromise<TResponse>({
const isValid = validate(response);
if (isValid) {
resolve(response);
} else if (retryCount + 1 >= maxTrial) {
} else if (retryCount + 1 >= maxRetries) {
reject(
new Error(
`The maximum number of retries exceeded. (${
retryCount + 1
}/${maxTrial})`
}/${maxRetries})`
)
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export type CreateRetryablePromiseOptions<TResponse> = {
validate: (response: TResponse) => boolean;

/**
* The maximum number of trials. 10 by default.
* The maximum number of retry. 50 by default.
*/
maxTrial?: number;
maxRetries?: number;

/**
* The function to decide how long to wait between tries.
Expand Down
2 changes: 1 addition & 1 deletion eslint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"devDependencies": {
"@types/jest": "27.5.2",
"eslint": "8.16.0",
"jest": "28.1.0",
"jest": "28.1.1",
"ts-jest": "28.0.4",
"ts-node": "10.7.0",
"typescript": "4.6.3"
Expand Down
2 changes: 1 addition & 1 deletion scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"folder-hash": "4.0.2",
"fs-extra": "10.1.0",
"inquirer": "8.2.4",
"jest": "28.1.0",
"jest": "28.1.1",
"js-yaml": "4.1.0",
"micromatch": "4.0.5",
"mustache": "4.2.0",
Expand Down
35 changes: 35 additions & 0 deletions scripts/release/__tests__/createReleasePR.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,41 @@ describe('createReleasePR', () => {
expect(versions.java.next).toEqual('0.0.2');
});

it('releases every languages if a `clients` commit is present', () => {
const versions = decideReleaseStrategy({
versions: {
javascript: {
current: '0.0.1',
},
java: {
current: '0.0.1',
},
php: {
current: '0.0.1',
},
},
commits: [
{
hash: 'b2501882',
type: 'fix',
scope: 'clients',
message: 'fix some utils',
raw: 'b2501882 fix(clients): fix some utils',
},
],
});

expect(versions.javascript.noCommit).toBeUndefined();
expect(versions.javascript.releaseType).toEqual('patch');
expect(versions.javascript.next).toEqual('0.0.2');
expect(versions.php.noCommit).toBeUndefined();
expect(versions.php.releaseType).toEqual('patch');
expect(versions.php.next).toEqual('0.0.2');
expect(versions.java.noCommit).toBeUndefined();
expect(versions.java.releaseType).toEqual('patch');
expect(versions.java.next).toEqual('0.0.2');
});

it('bumps for `specs` feat with only language `fix` commits', () => {
const versions = decideReleaseStrategy({
versions: {
Expand Down
4 changes: 2 additions & 2 deletions scripts/release/createReleasePR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { updateAPIVersions } from './updateAPIVersions';

dotenv.config({ path: ROOT_ENV_PATH });

const COMMON_SCOPES = ['specs'];
export const COMMON_SCOPES = ['specs', 'clients'];

export function readVersions(): VersionsBeforeBump {
return Object.fromEntries(
Expand Down Expand Up @@ -142,7 +142,7 @@ export function parseCommit(commit: string): Commit {
return {
hash,
type, // `fix` | `feat` | `chore` | ...
scope, // `specs` | `javascript` | `php` | `java` | ...
scope, // `clients` | `specs` | `javascript` | `php` | `java` | ...
message,
raw: commit,
};
Expand Down
4 changes: 2 additions & 2 deletions scripts/release/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export type VersionsBeforeBump = {
[lang: string]: Omit<Version, 'next' | 'releaseType'>;
};

export type Scope = Language | 'specs';
export type Scope = Language | 'clients' | 'specs';

export type PassedCommit = {
hash: string;
type: string;
/**
* A commit can be scoped to a language, or the specs, which impacts all clients.
* A commit can be scoped to a language. When scoped to `clients` or `specs`, it impacts all clients.
*/
scope: Scope;
message: string;
Expand Down
4 changes: 2 additions & 2 deletions scripts/release/updateAPIVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ async function updateVersionForJavascript(
});

await fsp.writeFile(
toAbsolutePath('config/openapitools.json').concat('\n'),
JSON.stringify(openapiConfig, null, 2)
toAbsolutePath('config/openapitools.json'),
JSON.stringify(openapiConfig, null, 2).concat('\n')
);

// Sets the new version of the utils package
Expand Down
12 changes: 6 additions & 6 deletions templates/java/libraries/okhttp-gson/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -254,24 +254,24 @@ public class {{classname}} extends ApiClient {
{{/operation}}

{{#isSearchClient}}
public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions, int maxTrial, IntUnaryOperator timeout) {
public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions, int maxRetries, IntUnaryOperator timeout) {
TaskUtils.retryUntil(() -> {
return this.getTaskAsync(indexName, taskID, requestOptions);
}, (GetTaskResponse task) -> {
return task.getStatus() == TaskStatus.PUBLISHED;
}, maxTrial, timeout);
}, maxRetries, timeout);
}

public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions) {
this.waitForTask(indexName, taskID, requestOptions, TaskUtils.DEFAULT_MAX_TRIAL, TaskUtils.DEFAULT_TIMEOUT);
this.waitForTask(indexName, taskID, requestOptions, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT);
}

public void waitForTask(String indexName, Long taskID, int maxTrial, IntUnaryOperator timeout) {
this.waitForTask(indexName, taskID, null, maxTrial, timeout);
public void waitForTask(String indexName, Long taskID, int maxRetries, IntUnaryOperator timeout) {
this.waitForTask(indexName, taskID, null, maxRetries, timeout);
}

public void waitForTask(String indexName, Long taskID) {
this.waitForTask(indexName, taskID, null, TaskUtils.DEFAULT_MAX_TRIAL, TaskUtils.DEFAULT_TIMEOUT);
this.waitForTask(indexName, taskID, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT);
}
{{/isSearchClient}}
}
Expand Down
2 changes: 1 addition & 1 deletion templates/javascript/package.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@experimental-api-clients-automation/requester-node-http": "{{utilsPackageVersion}}"
},
"devDependencies": {
"@types/node": "16.11.26",
"@types/node": "16.11.38",
"typescript": "4.6.3"
},
"engines": {
Expand Down
8 changes: 4 additions & 4 deletions templates/javascript/tests/package.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"@experimental-api-clients-automation/requester-node-http": "{{utilsPackageVersion}}"
},
"devDependencies": {
"@types/jest": "27.4.1",
"@types/node": "16.11.26",
"jest": "27.5.1",
"ts-jest": "27.1.4",
"@types/jest": "27.5.2",
"@types/node": "16.11.38",
"jest": "28.1.1",
"ts-jest": "28.0.4",
"ts-node": "10.7.0",
"typescript": "4.6.3"
}
Expand Down

0 comments on commit baaf767

Please sign in to comment.