Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

fix: flaky sample test two ways #375

Merged
merged 1 commit into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions samples/snippet/job_search_custom_ranking_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ function sampleSearchJobs(projectId, tenantId) {
client
.searchJobs(request)
.then(responses => {
const resources = responses[0];
for (const resource of resources.matchingJobs) {
console.log(`Job summary: ${resource.jobSummary}`);
console.log(`Job title snippet: ${resource.jobTitleSnippet}`);
const job = resource.job;
console.log(`Job name: ${job.name}`);
console.log(`Job title: ${job.title}`);
for (const resources of responses) {
for (const resource of resources.matchingJobs) {
console.log(`Job summary: ${resource.jobSummary}`);
console.log(`Job title snippet: ${resource.jobTitleSnippet}`);
const job = resource.job;
console.log(`Job name: ${job.name}`);
console.log(`Job title: ${job.title}`);
}
}
})
.catch(err => {
Expand Down
64 changes: 45 additions & 19 deletions samples/test/talent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ const {describe, it, before, after} = require('mocha');
const talent = require('@google-cloud/talent').v4;
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

/**
* For eventually consistent APIs, retry the test after a few seconds, up to 3 times.
* @param {function} testFunction the test function to retry.
* @returns {function}
*/
function eventually(testFunction) {
return async () => {
let delayMs = 2000;
for (let i = 0; i < 2; ++i) {
try {
return await testFunction();
} catch (e) {
await new Promise(resolve => setTimeout(resolve, delayMs));
delayMs *= 2;
}
}
return await testFunction();
};
}

describe('Talent Solution Jobs API v4 samples', () => {
const projectId = process.env.GCLOUD_PROJECT;
const tenantService = new talent.TenantServiceClient();
Expand Down Expand Up @@ -118,23 +138,29 @@ describe('Talent Solution Jobs API v4 samples', () => {
assert.match(output, new RegExp('Name'));
});

it('Searches for a job with custom ranking search', async () => {
console.log(
`node snippet/job_search_custom_ranking_search.js --project_id=${projectId} --tenant_id=${tenantId}`
);
const output = execSync(
`node snippet/job_search_custom_ranking_search.js --project_id=${projectId} --tenant_id=${tenantId}`
);
assert.match(output, new RegExp('Job summary'));
});

it('Searches for a job with histogram', async () => {
console.log(
`node snippet/job_search_histogram_search.js --project_id=${projectId} --tenant_id=${tenantId}`
);
const output = execSync(
`node snippet/job_search_histogram_search.js --project_id=${projectId} --tenant_id=${tenantId}`
);
assert.match(output, new RegExp('Job summary'));
});
it(
'Searches for a job with custom ranking search',
eventually(async () => {
console.log(
`node snippet/job_search_custom_ranking_search.js --project_id=${projectId} --tenant_id=${tenantId}`
);
const output = execSync(
`node snippet/job_search_custom_ranking_search.js --project_id=${projectId} --tenant_id=${tenantId}`
);
assert.match(output, new RegExp('Job summary'));
})
);

it(
'Searches for a job with histogram',
eventually(async () => {
console.log(
`node snippet/job_search_histogram_search.js --project_id=${projectId} --tenant_id=${tenantId}`
);
const output = execSync(
`node snippet/job_search_histogram_search.js --project_id=${projectId} --tenant_id=${tenantId}`
);
assert.match(output, new RegExp('Job summary'));
})
);
});