Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit bd9ab70

Browse files
committed
added more tests
1 parent 4ccfdab commit bd9ab70

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

google/cloud/bigtable/data/_metrics/data_model.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ class ActiveOperationMetric:
165165

166166
@property
167167
def interceptor_metadata(self) -> tuple[str, str]:
168+
"""
169+
returns a tuple to attach to the grpc metadata.
170+
171+
This metadata field will be read by the BigtableMetricsInterceptor to associate a request with an operation
172+
"""
168173
return OPERATION_INTERCEPTOR_METADATA_KEY, self.uuid
169174

170175
@property

tests/unit/data/_metrics/test_data_model.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,15 +391,17 @@ def test_end_attempt_with_status(self):
391391
- add one to completed_attempts
392392
- reset active_attempt to None
393393
- update state
394+
- notify handlers
394395
"""
395396
expected_start_time = 1
396397
expected_status = object()
397398
expected_gfe_latency_ns = 5
398399
expected_app_blocking = 12
399400
expected_backoff = 2
400401
expected_grpc_throttle = 3
402+
handlers = [mock.Mock(), mock.Mock()]
401403

402-
metric = self._make_one(mock.Mock())
404+
metric = self._make_one(mock.Mock(), handlers=handlers)
403405
assert metric.active_attempt is None
404406
assert len(metric.completed_attempts) == 0
405407
metric.start_attempt()
@@ -420,6 +422,11 @@ def test_end_attempt_with_status(self):
420422
assert got_attempt.backoff_before_attempt_ns == expected_backoff
421423
# state should be changed to BETWEEN_ATTEMPTS
422424
assert metric.state == State.BETWEEN_ATTEMPTS
425+
# check handlers
426+
for h in handlers:
427+
assert h.on_attempt_complete.call_count == 1
428+
assert h.on_attempt_complete.call_args[0][0] == got_attempt
429+
assert h.on_attempt_complete.call_args[0][1] == metric
423430

424431
def test_end_attempt_with_status_w_exception(self):
425432
"""
@@ -528,6 +535,48 @@ def test_end_with_status_w_exception(self):
528535
final_op = handlers[0].on_operation_complete.call_args[0][0]
529536
assert final_op.final_status == expected_status
530537

538+
def test_interceptor_metadata(self):
539+
from google.cloud.bigtable.data._metrics.data_model import (
540+
OPERATION_INTERCEPTOR_METADATA_KEY,
541+
)
542+
543+
metric = self._make_one(mock.Mock())
544+
key, value = metric.interceptor_metadata
545+
assert key == OPERATION_INTERCEPTOR_METADATA_KEY
546+
assert value == metric.uuid
547+
548+
def test_cancel(self):
549+
"""
550+
cancel should call on_operation_cancelled on handlers
551+
"""
552+
handlers = [mock.Mock(), mock.Mock()]
553+
metric = self._make_one(mock.Mock(), handlers=handlers)
554+
metric.cancel()
555+
for h in handlers:
556+
assert h.on_operation_cancelled.call_count == 1
557+
assert h.on_operation_cancelled.call_args[0][0] == metric
558+
559+
def test_end_with_status_with_default_cluster_zone(self):
560+
"""
561+
ending the operation should use default cluster and zone if not set
562+
"""
563+
from google.cloud.bigtable.data._metrics.data_model import (
564+
DEFAULT_CLUSTER_ID,
565+
DEFAULT_ZONE,
566+
)
567+
568+
handlers = [mock.Mock()]
569+
metric = self._make_one(mock.Mock(), handlers=handlers)
570+
assert metric.cluster_id is None
571+
assert metric.zone is None
572+
metric.end_with_status(mock.Mock())
573+
assert metric.state == State.COMPLETED
574+
# check that finalized operation was passed to handlers
575+
for h in handlers:
576+
assert h.on_operation_complete.call_count == 1
577+
called_with = h.on_operation_complete.call_args[0][0]
578+
assert called_with.cluster_id == DEFAULT_CLUSTER_ID
579+
assert called_with.zone == DEFAULT_ZONE
531580
def test_end_with_success(self):
532581
"""
533582
end with success should be a pass-through helper for end_with_status

0 commit comments

Comments
 (0)