2121UPDATE_SOURCE_PREFIX = "to-update-"
2222TEST_SOURCE_PREFIXES = (HUB_SOURCE_PREFIX , MQTT_SOURCE_PREFIX , UPDATE_SOURCE_PREFIX )
2323
24+ DESTINATION_PREFIX = "myNewDestination-"
25+ DESTINATION_FOR_TESTING_PREFIX = "myNewDestinationForTesting-"
26+ UPDATE_DESTINATION_PREFIX = "toupdate-"
27+ TEST_DESTINATION_PREFIXES = (DESTINATION_PREFIX , DESTINATION_FOR_TESTING_PREFIX , UPDATE_DESTINATION_PREFIX )
28+
29+ # Sources and Destinatinos are repeat offenders for hitting max limits because they are
30+ # quite low. We add specific cleanup for anything older than...:
31+ DELETE_THRESHOLD = 1 * 60 * 60 * 1000 # 1 hour in ms
32+ NOW_MS = int (time .time () * 1000 )
33+
34+
35+ @pytest .fixture (scope = "session" , autouse = True )
36+ def cleanup_stale_test_destinations (cognite_client : CogniteClient ) -> None :
37+ # max is low number like e.g. 50 per project, safe to list all:
38+ all_destinations = cognite_client .hosted_extractors .destinations .list (limit = None )
39+ stale = [
40+ d .external_id
41+ for d in all_destinations
42+ if d .external_id
43+ and d .external_id .startswith (TEST_DESTINATION_PREFIXES )
44+ and NOW_MS - d .created_time >= DELETE_THRESHOLD
45+ ]
46+ if stale :
47+ cognite_client .hosted_extractors .destinations .delete (stale , force = True , ignore_unknown_ids = True )
48+
2449
2550@pytest .fixture (scope = "session" , autouse = True )
2651def cleanup_stale_test_sources (cognite_client : CogniteClient ) -> None :
2752 all_sources = cognite_client .hosted_extractors .sources .list (limit = None )
28- now_ms = int (time .time () * 1000 )
29- # Clean up anyting older than 3 hours:
3053 stale = [
3154 s .external_id
3255 for s in all_sources
3356 if s .external_id
3457 and s .external_id .startswith (TEST_SOURCE_PREFIXES )
35- and now_ms - s .created_time >= 3 * 60 * 60 * 1000 # type: ignore [attr-defined]
58+ and NOW_MS - s .created_time >= DELETE_THRESHOLD # type: ignore [attr-defined]
3659 ]
3760 if stale :
3861 cognite_client .hosted_extractors .sources .delete (stale , force = True , ignore_unknown_ids = True )
@@ -58,7 +81,7 @@ def a_data_set(cognite_client: CogniteClient) -> DataSet:
5881@pytest .fixture
5982def one_destination (cognite_client : CogniteClient , fresh_session : SessionWrite ) -> Iterator [Destination ]:
6083 my_dest = DestinationWrite (
61- external_id = f"myNewDestination- { random_string (10 )} " ,
84+ external_id = f"{ DESTINATION_PREFIX } { random_string (10 )} " ,
6285 credentials = fresh_session ,
6386 )
6487 created = cognite_client .hosted_extractors .destinations .create (my_dest )
0 commit comments