From ee8afef5b5afa94f06af7f6bbe543c6c736d6ecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Istv=C3=A1n=20So=C3=B3s?= Date: Mon, 6 Jan 2025 16:36:38 +0100 Subject: [PATCH] Stricter retry verification. (#8417) --- .../lib/retry_enforcer_storage.dart | 43 ++++++++++++++----- pkg/fake_gcloud/pubspec.yaml | 1 + 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/pkg/fake_gcloud/lib/retry_enforcer_storage.dart b/pkg/fake_gcloud/lib/retry_enforcer_storage.dart index 7a8fa72bf..85cc89f16 100644 --- a/pkg/fake_gcloud/lib/retry_enforcer_storage.dart +++ b/pkg/fake_gcloud/lib/retry_enforcer_storage.dart @@ -5,24 +5,45 @@ import 'dart:async'; import 'package:gcloud/storage.dart'; +import 'package:stack_trace/stack_trace.dart'; + +const _skippedFirstFrames = { + 'package:fake_gcloud/retry_enforcer_storage.dart', + 'package:pub_dev/shared/storage.dart', +}; void _verifyRetryOnStack() { - final st = StackTrace.current.toString(); - if (st.contains('package:retry/')) return; - if (st.contains('retryAsync')) return; // lib/shared/utils.dart + final trace = Trace.current(); + + // The first frame index outside of this file. + var startFrameIndex = 0; + while (_skippedFirstFrames.any((skipped) => + trace.frames[startFrameIndex].uri.toString().contains(skipped))) { + startFrameIndex++; + } + + final firstRealFrame = + trace.frames.skip(startFrameIndex).firstOrNull?.toString(); + if (firstRealFrame == null) { + return; + } // detect direct test calls - final linesWithoutThisFile = st - .split('\n') - .where((l) => !l.contains('retry_enforcer_storage.dart')) - .toList(); - if (linesWithoutThisFile.isNotEmpty && - linesWithoutThisFile.first.contains('_test.dart')) { + if (firstRealFrame.contains('_test.dart')) { + return; + } + + // detect retry library + if (firstRealFrame.contains('package:retry/')) { + return; + } + // detect lib/shared/utils.dart use + if (firstRealFrame.contains('retryAsync')) { return; } - print('Missing retry detected:\n$st\n'); - throw AssertionError('retry is not present in stacktrace: $st'); + print('Missing retry detected:\n$trace\n'); + throw AssertionError('retry is not present in stacktrace: $trace'); } Future _verifyRetry( diff --git a/pkg/fake_gcloud/pubspec.yaml b/pkg/fake_gcloud/pubspec.yaml index 0affdd8d6..4b2c4261f 100644 --- a/pkg/fake_gcloud/pubspec.yaml +++ b/pkg/fake_gcloud/pubspec.yaml @@ -12,6 +12,7 @@ dependencies: gcloud: ^0.8.18 logging: '>=0.11.3 <2.0.0' _discoveryapis_commons: ^1.0.7 + stack_trace: ^1.12.0 dev_dependencies: coverage: any # test already depends on it