Skip to content

Commit cf962a5

Browse files
authored
fix: throw timeout error when using jobs.query (#1402)
1 parent 0138928 commit cf962a5

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

src/bigquery.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -2206,7 +2206,7 @@ export class BigQuery extends Service {
22062206
this.runJobsQuery(queryReq, (err, job, res) => {
22072207
this.trace_('[runJobsQuery callback]: ', query, err, job, res);
22082208
if (err) {
2209-
(callback as SimpleQueryRowsCallback)(err, null, res);
2209+
(callback as SimpleQueryRowsCallback)(err, null, job);
22102210
return;
22112211
}
22122212

@@ -2232,6 +2232,14 @@ export class BigQuery extends Service {
22322232
job!.getQueryResults(options, callback as QueryRowsCallback);
22332233
return;
22342234
}
2235+
// If timeout override was provided, return error.
2236+
if (queryReq.timeoutMs) {
2237+
const err = new Error(
2238+
`The query did not complete before ${queryReq.timeoutMs}ms`
2239+
);
2240+
(callback as SimpleQueryRowsCallback)(err, null, job);
2241+
return;
2242+
}
22352243
delete options.timeoutMs;
22362244
this.trace_('[runJobsQuery] job not complete');
22372245
job!.getQueryResults(options, callback as QueryRowsCallback);

system-test/bigquery.ts

+45
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,51 @@ describe('BigQuery', () => {
369369
assert.strictEqual(res.totalRows, '100');
370370
});
371371

372+
describe('Query timeout', () => {
373+
let longRunningQuery = '';
374+
375+
beforeEach(() => {
376+
const tableId = generateName('table');
377+
longRunningQuery = `CREATE TABLE ${dataset.projectId}.${dataset.id}.${tableId} AS SELECT num FROM UNNEST(GENERATE_ARRAY(1,1000000)) as num`;
378+
});
379+
380+
it('should throw a timeout error with jobs.query', async () => {
381+
const qOpts: QueryOptions = {
382+
timeoutMs: 1000,
383+
};
384+
let foundError: Error | null = null;
385+
try {
386+
await bigquery.query(longRunningQuery, qOpts);
387+
} catch (error: unknown) {
388+
foundError = error as Error;
389+
}
390+
assert.notEqual(foundError, null);
391+
assert.equal(
392+
foundError?.message,
393+
'The query did not complete before 1000ms'
394+
);
395+
});
396+
397+
it('should throw a timeout error without jobs.query', async () => {
398+
const jobId = generateName('job');
399+
const qOpts: QueryOptions = {
400+
job: bigquery.job(jobId),
401+
timeoutMs: 1000,
402+
};
403+
let foundError: Error | null = null;
404+
try {
405+
await bigquery.query(longRunningQuery, qOpts);
406+
} catch (error: unknown) {
407+
foundError = error as Error;
408+
}
409+
assert.notEqual(foundError, null);
410+
assert.equal(
411+
foundError?.message,
412+
'The query did not complete before 1000ms'
413+
);
414+
});
415+
});
416+
372417
it('should allow querying in series', done => {
373418
bigquery.query(
374419
query,

test/bigquery.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -3108,7 +3108,7 @@ describe('BigQuery', () => {
31083108
const error = new Error('err');
31093109

31103110
bq.runJobsQuery = (query: {}, callback: Function) => {
3111-
callback(error, null, FAKE_RESPONSE);
3111+
callback(error, FAKE_RESPONSE, {});
31123112
};
31133113

31143114
bq.query(QUERY_STRING, (err: Error, rows: {}, resp: {}) => {
@@ -3119,6 +3119,31 @@ describe('BigQuery', () => {
31193119
});
31203120
});
31213121

3122+
it('should return throw error when jobs.query times out', done => {
3123+
const fakeJob = {};
3124+
3125+
bq.runJobsQuery = (query: {}, callback: Function) => {
3126+
callback(null, fakeJob, {
3127+
queryId: uuid.v1(),
3128+
jobComplete: false,
3129+
});
3130+
};
3131+
3132+
bq.query(
3133+
QUERY_STRING,
3134+
{timeoutMs: 1000},
3135+
(err: Error, rows: {}, resp: {}) => {
3136+
assert.strictEqual(
3137+
err.message,
3138+
'The query did not complete before 1000ms'
3139+
);
3140+
assert.strictEqual(rows, null);
3141+
assert.strictEqual(resp, fakeJob);
3142+
done();
3143+
}
3144+
);
3145+
});
3146+
31223147
it('should exit early if dryRun is set', done => {
31233148
const options = {
31243149
query: QUERY_STRING,

0 commit comments

Comments
 (0)