Skip to content

Commit

Permalink
Fix concurrency issue
Browse files Browse the repository at this point in the history
Use workaround for dart-lang/build#2634
by @eernstg
  • Loading branch information
nielsenko committed Sep 14, 2022
1 parent 82436ab commit c2c8e07
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
26 changes: 23 additions & 3 deletions generator/lib/src/realm_object_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,34 @@ import 'measure.dart';
import 'realm_model_info.dart';
import 'session.dart';

Future<ResolvedLibraryResult?> _getResolvedLibrary(LibraryElement library, Resolver resolver) async {
var attempts = 0;
while (true) {
try {
final freshLibrary = await resolver.libraryFor(await resolver.assetIdForElement(library));
final freshSession = freshLibrary.session;
var someResult = await freshSession.getResolvedLibraryByElement(freshLibrary);
if (someResult is ResolvedLibraryResult) return someResult;
} catch (_) {
++attempts;
if (attempts == 10) {
log.severe('Internal error: Analysis session '
'did not stabilize after ten attempts!');
return null;
}
}
}
}

class RealmObjectGenerator extends Generator {
@override
Future<String> generate(LibraryReader library, BuildStep buildStep) async {
return await measure(
() async {
return await scopeSession(
(await library.element.session.getResolvedLibraryByElement(library.element)) as ResolvedLibraryResult,
() async => library.classes.realmInfo.expand((m) => m.toCode()).join('\n'),
final result = await _getResolvedLibrary(library.element, buildStep.resolver);
return scopeSession(
result!,
() => library.classes.realmInfo.expand((m) => m.toCode()).join('\n'),
color: stdout.supportsAnsiEscapes,
);
},
Expand Down
2 changes: 1 addition & 1 deletion generator/lib/src/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const _sessionKey = #SessionKey;
// in case multiple libs are processed concurrently, we make session zone local
Session get session => Zone.current[_sessionKey] as Session;

Future<T> scopeSession<T>(
FutureOr<T> scopeSession<T>(
ResolvedLibraryResult resolvedLibrary,
FutureOr<T> Function() fn, {
String? prefix,
Expand Down
22 changes: 16 additions & 6 deletions generator/test/test_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,18 @@ class GoldenFileMatcher extends Matcher {
return _matcher.describe(description);
}

@override
Description describeMismatch(
dynamic item,
Description mismatchDescription,
Map<dynamic, dynamic> matchState,
bool verbose,
) =>
_matcher.describeMismatch(item, mismatchDescription, matchState, verbose);

@override
bool matches(dynamic item, Map<dynamic, dynamic> matchState) {
// create golden, if missing
// create golden master, if missing
if (!golden.existsSync()) {
var bytes = <int>[];
if (item is File) {
Expand All @@ -72,19 +81,19 @@ class GoldenFileMatcher extends Matcher {

/// A special equality matcher for strings.
class LinesEqualsMatcher extends Matcher {
late final List<String> expectedLines;
final String expected;

LinesEqualsMatcher(String expected) {
expectedLines = expected.split("\n");
}
LinesEqualsMatcher(this.expected);

@override
Description describe(Description description) => description.add("LinesEqualsMatcher");
Description describe(Description description) => description.add(expected);

@override
// ignore: strict_raw_type
bool matches(dynamic actual, Map matchState) {
final actualValue = utf8.decode(actual as List<int>);

final expectedLines = expected.split("\n");
final actualLines = actualValue.split("\n");

if (actualLines.length > expectedLines.length) {
Expand All @@ -109,6 +118,7 @@ class LinesEqualsMatcher extends Matcher {

@override
Description describeMismatch(dynamic item, Description mismatchDescription, Map matchState, bool verbose) {
mismatchDescription.addDescriptionOf(item).replace(utf8.decode(item as List<int>));
if (matchState["Error"] != null) {
mismatchDescription.add(matchState["Error"] as String);
}
Expand Down

0 comments on commit c2c8e07

Please sign in to comment.