Skip to content

Commit 759bb24

Browse files
fix: flaky sample test two ways (#375)
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 googleapis/nodejs-talent#344
1 parent 7d0c147 commit 759bb24

File tree

2 files changed

+53
-26
lines changed

2 files changed

+53
-26
lines changed

talent/snippet/job_search_custom_ranking_search.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ function sampleSearchJobs(projectId, tenantId) {
5555
client
5656
.searchJobs(request)
5757
.then(responses => {
58-
const resources = responses[0];
59-
for (const resource of resources.matchingJobs) {
60-
console.log(`Job summary: ${resource.jobSummary}`);
61-
console.log(`Job title snippet: ${resource.jobTitleSnippet}`);
62-
const job = resource.job;
63-
console.log(`Job name: ${job.name}`);
64-
console.log(`Job title: ${job.title}`);
58+
for (const resources of responses) {
59+
for (const resource of resources.matchingJobs) {
60+
console.log(`Job summary: ${resource.jobSummary}`);
61+
console.log(`Job title snippet: ${resource.jobTitleSnippet}`);
62+
const job = resource.job;
63+
console.log(`Job name: ${job.name}`);
64+
console.log(`Job title: ${job.title}`);
65+
}
6566
}
6667
})
6768
.catch(err => {

talent/test/talent.test.js

+45-19
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ const {describe, it, before, after} = require('mocha');
2222
const talent = require('@google-cloud/talent').v4;
2323
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2424

25+
/**
26+
* For eventually consistent APIs, retry the test after a few seconds, up to 3 times.
27+
* @param {function} testFunction the test function to retry.
28+
* @returns {function}
29+
*/
30+
function eventually(testFunction) {
31+
return async () => {
32+
let delayMs = 2000;
33+
for (let i = 0; i < 2; ++i) {
34+
try {
35+
return await testFunction();
36+
} catch (e) {
37+
await new Promise(resolve => setTimeout(resolve, delayMs));
38+
delayMs *= 2;
39+
}
40+
}
41+
return await testFunction();
42+
};
43+
}
44+
2545
describe('Talent Solution Jobs API v4 samples', () => {
2646
const projectId = process.env.GCLOUD_PROJECT;
2747
const tenantService = new talent.TenantServiceClient();
@@ -118,23 +138,29 @@ describe('Talent Solution Jobs API v4 samples', () => {
118138
assert.match(output, new RegExp('Name'));
119139
});
120140

121-
it('Searches for a job with custom ranking search', async () => {
122-
console.log(
123-
`node snippet/job_search_custom_ranking_search.js --project_id=${projectId} --tenant_id=${tenantId}`
124-
);
125-
const output = execSync(
126-
`node snippet/job_search_custom_ranking_search.js --project_id=${projectId} --tenant_id=${tenantId}`
127-
);
128-
assert.match(output, new RegExp('Job summary'));
129-
});
130-
131-
it('Searches for a job with histogram', async () => {
132-
console.log(
133-
`node snippet/job_search_histogram_search.js --project_id=${projectId} --tenant_id=${tenantId}`
134-
);
135-
const output = execSync(
136-
`node snippet/job_search_histogram_search.js --project_id=${projectId} --tenant_id=${tenantId}`
137-
);
138-
assert.match(output, new RegExp('Job summary'));
139-
});
141+
it(
142+
'Searches for a job with custom ranking search',
143+
eventually(async () => {
144+
console.log(
145+
`node snippet/job_search_custom_ranking_search.js --project_id=${projectId} --tenant_id=${tenantId}`
146+
);
147+
const output = execSync(
148+
`node snippet/job_search_custom_ranking_search.js --project_id=${projectId} --tenant_id=${tenantId}`
149+
);
150+
assert.match(output, new RegExp('Job summary'));
151+
})
152+
);
153+
154+
it(
155+
'Searches for a job with histogram',
156+
eventually(async () => {
157+
console.log(
158+
`node snippet/job_search_histogram_search.js --project_id=${projectId} --tenant_id=${tenantId}`
159+
);
160+
const output = execSync(
161+
`node snippet/job_search_histogram_search.js --project_id=${projectId} --tenant_id=${tenantId}`
162+
);
163+
assert.match(output, new RegExp('Job summary'));
164+
})
165+
);
140166
});

0 commit comments

Comments
 (0)