Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw timeout error when using jobs.query #1402

Merged
merged 6 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion src/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,17 +362,17 @@
private _universeDomain: string;
private _enableQueryPreview: boolean;

createQueryStream(options?: Query | string): ResourceStream<RowMetadata> {

Check warning on line 365 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

'options' is defined but never used
// placeholder body, overwritten in constructor
return new ResourceStream<RowMetadata>({}, () => {});
}

getDatasetsStream(options?: GetDatasetsOptions): ResourceStream<Dataset> {

Check warning on line 370 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

'options' is defined but never used
// placeholder body, overwritten in constructor
return new ResourceStream<Dataset>({}, () => {});
}

getJobsStream(options?: GetJobsOptions): ResourceStream<Job> {

Check warning on line 375 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

'options' is defined but never used
// placeholder body, overwritten in constructor
return new ResourceStream<Job>({}, () => {});
}
Expand Down Expand Up @@ -1569,7 +1569,7 @@
const parameterMode = is.array(params) ? 'positional' : 'named';
const queryParameters: bigquery.IQueryParameter[] = [];
if (parameterMode === 'named') {
const namedParams = params as {[param: string]: any};

Check warning on line 1572 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
for (const namedParameter of Object.getOwnPropertyNames(namedParams)) {
const value = namedParams[namedParameter];
let queryParameter;
Expand Down Expand Up @@ -2206,13 +2206,13 @@
this.runJobsQuery(queryReq, (err, job, res) => {
this.trace_('[runJobsQuery callback]: ', query, err, job, res);
if (err) {
(callback as SimpleQueryRowsCallback)(err, null, res);
(callback as SimpleQueryRowsCallback)(err, null, job);
alvarowolfx marked this conversation as resolved.
Show resolved Hide resolved
return;
}

options = extend({job}, queryOpts, options);
if (res && res.jobComplete) {
let rows: any = [];

Check warning on line 2215 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (res.schema && res.rows) {
rows = BigQuery.mergeSchemaWithRows_(res.schema, res.rows, {
wrapIntegers: options.wrapIntegers || false,
Expand All @@ -2232,6 +2232,14 @@
job!.getQueryResults(options, callback as QueryRowsCallback);
return;
}
// If timeout override was provided, return error.
if (queryReq.timeoutMs) {
alvarowolfx marked this conversation as resolved.
Show resolved Hide resolved
const err = new Error(
alvarowolfx marked this conversation as resolved.
Show resolved Hide resolved
`The query did not complete before ${queryReq.timeoutMs}ms`
alvarowolfx marked this conversation as resolved.
Show resolved Hide resolved
);
(callback as SimpleQueryRowsCallback)(err, null, job);
return;
}
delete options.timeoutMs;
this.trace_('[runJobsQuery] job not complete');
job!.getQueryResults(options, callback as QueryRowsCallback);
Expand Down
37 changes: 37 additions & 0 deletions system-test/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,43 @@ describe('BigQuery', () => {
assert.strictEqual(res.totalRows, '100');
});

describe('Query timeout', () => {
let longRunningQuery = '';

beforeEach(() => {
const tableId = generateName('table');
longRunningQuery = `CREATE TABLE ${dataset.projectId}.${dataset.id}.${tableId} AS SELECT num FROM UNNEST(GENERATE_ARRAY(1,1000000)) as num`;
});

it('should throw a timeout error with jobs.query', async () => {
const qOpts: QueryOptions = {
timeoutMs: 1000,
};
let foundError: Error | null = null;
try {
await bigquery.query(longRunningQuery, qOpts);
} catch (error: unknown) {
foundError = error as Error;
}
assert.notEqual(foundError, null);
leahecole marked this conversation as resolved.
Show resolved Hide resolved
});

it('should throw a timeout error without jobs.query', async () => {
const jobId = generateName('job');
const qOpts: QueryOptions = {
job: bigquery.job(jobId),
timeoutMs: 1000,
};
let foundError: Error | null = null;
try {
await bigquery.query(longRunningQuery, qOpts);
} catch (error: unknown) {
foundError = error as Error;
}
assert.notEqual(foundError, null);
leahecole marked this conversation as resolved.
Show resolved Hide resolved
});
});

it('should allow querying in series', done => {
bigquery.query(
query,
Expand Down
27 changes: 26 additions & 1 deletion test/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3108,7 +3108,7 @@ describe('BigQuery', () => {
const error = new Error('err');

bq.runJobsQuery = (query: {}, callback: Function) => {
callback(error, null, FAKE_RESPONSE);
callback(error, FAKE_RESPONSE, {});
};

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

it('should return throw error when jobs.query times out', done => {
const fakeJob = {};

bq.runJobsQuery = (query: {}, callback: Function) => {
callback(null, fakeJob, {
queryId: uuid.v1(),
jobComplete: false,
});
};

bq.query(
QUERY_STRING,
{timeoutMs: 1000},
(err: Error, rows: {}, resp: {}) => {
assert.strictEqual(
err.message,
'The query did not complete before 1000ms'
);
assert.strictEqual(rows, null);
assert.strictEqual(resp, fakeJob);
done();
}
);
});

it('should exit early if dryRun is set', done => {
const options = {
query: QUERY_STRING,
Expand Down
Loading