Skip to content

Commit 4bb157c

Browse files
authored
chore: handled race condition in stateless query integration test (#4045)
* fix: handled race condition in stateless query integration test The testTableResultJobIdAndQueryId test was failing intermittently on slower networks. The test strictly asserted that Job ID must be null for stateless queries. However, the library correctly falls back to creating a Job ID if the stateless query times out. This change updates the assertion logic to accept either a valid Query ID (stateless success) or a valid Job ID (fallback success). Fixes #4008 * refactor: use XOR assertion for conciseness Applied feedback from code review to use exclusive OR operator for validating JobID/QueryID mutual exclusivity. * fix: apply race condition logic to testStatelessQueries Applied XOR assertion logic to testStatelessQueries. Test was failing on slow networks because they did not account for JOB_CREATION_OPTIONAL falling back to job creation. Fixes #4002 * docs: add comment explaining stateless query fallback behavior * docs: add comment explaining stateless query fallback behavior in testTableResultJobIdAndQueryId()
1 parent d214d10 commit 4bb157c

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7181,8 +7181,12 @@ public void testStatelessQueries() throws InterruptedException {
71817181
// Stateless query should have no job id.
71827182
bigQuery.getOptions().setDefaultJobCreationMode(JobCreationMode.JOB_CREATION_OPTIONAL);
71837183
TableResult tableResult = executeSimpleQuery(bigQuery);
7184-
assertNotNull(tableResult.getQueryId());
7185-
assertNull(tableResult.getJobId());
7184+
// Use XOR: We accept EITHER a QueryId (fast path) OR a JobId (slow fallback), but not both.
7185+
// Ideally Stateless query will return queryId but in some cases it would return jobId instead
7186+
// of queryId based on the query complexity or other factors (job timeout configs).
7187+
assertTrue(
7188+
"Exactly one of jobId or queryId should be non-null",
7189+
(tableResult.getJobId() != null) ^ (tableResult.getQueryId() != null));
71867190

71877191
// Job creation takes over, no query id is created.
71887192
bigQuery.getOptions().setDefaultJobCreationMode(JobCreationMode.JOB_CREATION_REQUIRED);
@@ -7220,8 +7224,14 @@ public void testTableResultJobIdAndQueryId() throws InterruptedException {
72207224
String query = "SELECT 1 as one";
72217225
QueryJobConfiguration configStateless = QueryJobConfiguration.newBuilder(query).build();
72227226
TableResult result = bigQuery.query(configStateless);
7223-
assertNull(result.getJobId());
7224-
assertNotNull(result.getQueryId());
7227+
// A stateless query should result in either a queryId (stateless success) or a jobId (fallback
7228+
// to a job).
7229+
// Exactly one of them should be non-null.
7230+
// Ideally Stateless query will return queryId but in some cases it would return jobId instead
7231+
// of queryId based on the query complexity or other factors (job timeout configs).
7232+
assertTrue(
7233+
"Exactly one of jobId or queryId should be non-null",
7234+
(result.getJobId() != null) ^ (result.getQueryId() != null));
72257235

72267236
// Test scenario 2 by failing stateless check by setting job timeout.
72277237
QueryJobConfiguration configQueryWithJob =

0 commit comments

Comments
 (0)