Skip to content

Commit

Permalink
Take into account current behavior of /GetSession /BatchCreateSession…
Browse files Browse the repository at this point in the history
… in tests
  • Loading branch information
odeke-em committed Jan 4, 2025
1 parent 9d9a9fd commit 4a6657e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 21 deletions.
3 changes: 0 additions & 3 deletions google/cloud/spanner_v1/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,6 @@ def bind(self, database):
returned_session_count = 0
while created_session_count < self.size:
nth_req = database._next_nth_request
print(
f"\033[36mPingingPool.nth_req: {nth_req}: {hex(id(self))}\033[00m"
)

def create_sessions(attempt):
all_metadata = database.metadata_with_request_id(
Expand Down
1 change: 0 additions & 1 deletion google/cloud/spanner_v1/testing/database_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def spanner_api(self):
channel = grpc.insecure_channel(self._instance.emulator_host)
self._x_goog_request_id_interceptor = XGoogRequestIDHeaderInterceptor()
self._interceptors.append(self._x_goog_request_id_interceptor)
# print("self._interceptors", self._interceptors)
channel = grpc.intercept_channel(channel, *self._interceptors)
transport = SpannerGrpcTransport(channel=channel)
self._spanner_api = SpannerClient(
Expand Down
2 changes: 0 additions & 2 deletions google/cloud/spanner_v1/testing/interceptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ def intercept(self, method, request_or_iterator, call_details):
)

response_or_iterator = method(request_or_iterator, call_details)
print("call_details", call_details, "\n", response_or_iterator)
streaming = getattr(response_or_iterator, "__iter__", None) is not None
print("x_append", call_details.method, x_goog_request_id)
with self.__lock:
if streaming:
self._stream_req_segments.append(
Expand Down
73 changes: 58 additions & 15 deletions tests/mockserver_tests/test_request_id_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,16 @@ def test_snapshot_execute_sql(self):
assert got_stream_segments == want_stream_segments

def test_snapshot_read_concurrent(self):
db = self.database
# Trigger BatchCreateSessions firstly.
with self.database.snapshot() as snapshot:
with db.snapshot() as snapshot:
rows = snapshot.execute_sql("select 1")
for row in rows:
_ = row

# The other requests can then proceed.
def select1():
with self.database.snapshot() as snapshot:
with db.snapshot() as snapshot:
rows = snapshot.execute_sql("select 1")
res_list = []
for row in rows:
Expand Down Expand Up @@ -112,17 +113,56 @@ def select1():
requests = self.spanner_service.requests
self.assertEqual(2 + n * 2, len(requests), msg=requests)

client_id = self.database._nth_client_id
channel_id = self.database._channel_id
client_id = db._nth_client_id
channel_id = db._channel_id
got_stream_segments, got_unary_segments = self.canonicalize_request_id_headers()

want_unary_segments = [
(
"/google.spanner.v1.Spanner/BatchCreateSessions",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 1, 1),
),
(
"/google.spanner.v1.Spanner/GetSession",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 3, 1),
),
(
"/google.spanner.v1.Spanner/GetSession",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 5, 1),
),
(
"/google.spanner.v1.Spanner/GetSession",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 7, 1),
),
(
"/google.spanner.v1.Spanner/GetSession",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 9, 1),
),
(
"/google.spanner.v1.Spanner/GetSession",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 11, 1),
),
(
"/google.spanner.v1.Spanner/GetSession",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 13, 1),
),
(
"/google.spanner.v1.Spanner/GetSession",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 15, 1),
),
(
"/google.spanner.v1.Spanner/GetSession",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 17, 1),
),
(
"/google.spanner.v1.Spanner/GetSession",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 19, 1),
),
(
"/google.spanner.v1.Spanner/GetSession",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 21, 1),
),
]
print("got_unary", got_unary_segments)
assert got_unary_segments == want_unary_segments

want_stream_segments = [
Expand All @@ -132,39 +172,43 @@ def select1():
),
(
"/google.spanner.v1.Spanner/ExecuteStreamingSql",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 3, 1),
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 4, 1),
),
(
"/google.spanner.v1.Spanner/ExecuteStreamingSql",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 4, 1),
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 6, 1),
),
(
"/google.spanner.v1.Spanner/ExecuteStreamingSql",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 5, 1),
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 8, 1),
),
(
"/google.spanner.v1.Spanner/ExecuteStreamingSql",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 6, 1),
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 10, 1),
),
(
"/google.spanner.v1.Spanner/ExecuteStreamingSql",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 7, 1),
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 12, 1),
),
(
"/google.spanner.v1.Spanner/ExecuteStreamingSql",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 8, 1),
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 14, 1),
),
(
"/google.spanner.v1.Spanner/ExecuteStreamingSql",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 9, 1),
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 16, 1),
),
(
"/google.spanner.v1.Spanner/ExecuteStreamingSql",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 10, 1),
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 18, 1),
),
(
"/google.spanner.v1.Spanner/ExecuteStreamingSql",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 11, 1),
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 20, 1),
),
(
"/google.spanner.v1.Spanner/ExecuteStreamingSql",
(1, REQ_RAND_PROCESS_ID, client_id, channel_id, 22, 1),
),
]
assert got_stream_segments == want_stream_segments
Expand Down Expand Up @@ -265,7 +309,6 @@ def test_unary_retryable_error(self):
)
]

print("got_unary_segments", got_unary_segments)
assert got_unary_segments == want_unary_segments
assert got_stream_segments == want_stream_segments

Expand Down

0 comments on commit 4a6657e

Please sign in to comment.