1919import time
2020
2121import backoff
22- from google .cloud import logging
22+ from google .cloud import logging , storage
2323import pytest
2424
2525import export
@@ -46,8 +46,8 @@ def _create_sink_name():
4646
4747
4848@backoff .on_exception (backoff .expo , Exception , max_time = 60 , raise_on_giveup = False )
49- def _delete_sink ( sink ):
50- sink .delete ()
49+ def _delete_object ( obj ):
50+ obj .delete ()
5151
5252
5353# Runs once for entire test suite
@@ -62,7 +62,20 @@ def cleanup_old_sinks():
6262 if match :
6363 sink_timestamp = int (match .group (1 ))
6464 if TIMESTAMP - sink_timestamp > CLEANUP_THRESHOLD :
65- _delete_sink (sink )
65+ _delete_object (sink )
66+
67+ storage_client = storage .Client ()
68+
69+ # See _sink_storage_setup in usage_guide.py for details about how
70+ # sinks are named.
71+ test_bucket_name_regex = r"^sink\-storage\-(\d+)$"
72+ for bucket in storage_client .list_buckets ():
73+ match = re .match (test_bucket_name_regex , bucket .name )
74+ if match :
75+ # Bucket timestamp is int(time.time() * 1000)
76+ bucket_timestamp = int (match .group (1 ))
77+ if TIMESTAMP - bucket_timestamp // 1000 > CLEANUP_THRESHOLD :
78+ _delete_object (bucket )
6679
6780
6881@pytest .fixture
@@ -79,7 +92,7 @@ def example_sink(cleanup_old_sinks):
7992
8093 yield sink
8194
82- _delete_sink (sink )
95+ _delete_object (sink )
8396
8497
8598def test_list (example_sink , capsys ):
@@ -99,7 +112,7 @@ def test_create(capsys):
99112 export .create_sink (sink_name , BUCKET , TEST_SINK_FILTER )
100113 # Clean-up the temporary sink.
101114 finally :
102- _delete_sink (logging .Client ().sink (sink_name ))
115+ _delete_object (logging .Client ().sink (sink_name ))
103116
104117 out , _ = capsys .readouterr ()
105118 assert sink_name in out
0 commit comments