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

Commit

Permalink
fix: flaky sample test two ways (#375)
Browse files Browse the repository at this point in the history
1. Examine all the resources in the response.
2. I suspect, but cannot prove that the search function is eventually
   consistent.  So, give it some time.

Fixes #344
  • Loading branch information
SurferJeffAtGoogle committed Oct 20, 2021
1 parent 6d31388 commit d722bdb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
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'));
})
);
});

0 comments on commit d722bdb

Please sign in to comment.