Skip to content

Commit

Permalink
Stricter retry verification. (#8417)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Jan 6, 2025
1 parent c65e113 commit ee8afef
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
43 changes: 32 additions & 11 deletions pkg/fake_gcloud/lib/retry_enforcer_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<R> _verifyRetry<R>(
Expand Down
1 change: 1 addition & 0 deletions pkg/fake_gcloud/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ee8afef

Please sign in to comment.