From 758b1c59e40b3e7b8105fa4202e3b59d1ecca10d Mon Sep 17 00:00:00 2001 From: Jeffrey Rennie Date: Fri, 15 Oct 2021 11:23:54 -0700 Subject: [PATCH] fix: flaky sample test two ways 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 https://github.com/googleapis/nodejs-talent/issues/344 --- .../job_search_custom_ranking_search.js | 15 +++-- samples/test/talent.test.js | 64 +++++++++++++------ 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/samples/snippet/job_search_custom_ranking_search.js b/samples/snippet/job_search_custom_ranking_search.js index 136dc59..7a58341 100644 --- a/samples/snippet/job_search_custom_ranking_search.js +++ b/samples/snippet/job_search_custom_ranking_search.js @@ -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 => { diff --git a/samples/test/talent.test.js b/samples/test/talent.test.js index 353f129..bd1e769 100644 --- a/samples/test/talent.test.js +++ b/samples/test/talent.test.js @@ -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(); @@ -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')); + }) + ); });