5cb3b37c74
This reverts commit 69f32d6ad7.
Reason for revert: We seem to have a number of tests failing with timeouts in CBUILD after this change, please see logs at
https://dart-in-g3-qa-prod.corp.google.com/dg3/Home#/cbuild/find/69f32d6ad7e724e3148cb2eb6601e63165e76ad3
Original change's description:
> Refactor `_Future`.
>
> This is a major rewrite of the `_Future` class,
> which is the default implementation of the `Future` interface.
>
> The main goal was to reduce the number of expensive type checks
> in the internal passing around of data.
> Expensive type checks are things like
> * `is _Future<T>` (more expensive than just `is _Future`, the latter
> can be a single class-ID check.
> * Covariant generic parameter checks (using `T` covariantly in a
> parameter forces a run-time type check).
>
> Also removed some plain unnecessary casts and turned some
> implicit casts from `dynamic` into `unsafeCast`s.
>
> This seems to be an success, at least on very primitive benchmarks, according to Golem:
> FutureCatchErrorTest 41.22% (1.9 noise)
> FutureValueTest 46.51% (2.8 noise)
> EmptyFutureTest 59.15% (3.1 noise)
> FutureWhenCompleteTest 51.10% (3.2 noise)
>
> A secondary goal was to clean up a very old and messy class,
> and make it clearer for other `dart:async` how to interact
> with the future.
>
> The change has a memory cost: The `_FutureListener<S,T>` class,
> which represents a `then`, `catchError` or `whenComplete`
> call on a `_Future`, now contains a reference to its source future,
> the one which provides the inputs to the callbacks,
> as well as the result future returned by the call.
> That's one extra memory slot per listener.
>
> In return, the `_FutureListener` now does not need to
> get its source future as an argument, which needs a covariant
> generic type check, and the methods of `_Future` can be written
> in a way which ignores the type parameters of both `_Future`
> and `_FutureListener`, which reduces complex type checks
> significantly.
>
> In general, typed code is in `_FutureListener`, which knows both
> the source and target types of the listener callbacks, and which
> contains the futures already at that type, so no extra type checking
> is needed.
> The `_Future` class is mostly untyped, except for its "public"
> API, called by other classes, which checks inputs,
> and code interacting with non-native futures.
> Invariants ensure that only correctly typed values
> are stored in the untyped shared `_resultOrListeners` field
> on `_Future`, as determined by its `_state` integer.
> (This was already partially true, and has simply been made
> more consistent.)
>
> Further, we now throw an error in a situation that was previously
> unhandled: When a `_Future` is completed with *itself*.
> That would ensure that the future would never complete
> (it waits for itself to complete before it can complete),
> and may potentially have caused weird loops in the representation.
> In practice, it probably never happens. Now it makes the error
> fail with an error.
> Currently a private `_FutureCyclicDependencyError` which presents
> as an `UnsupportedError`.
> That avoids code like
> ```dart
> import "dart:async";
> void main() {
> var c = Completer();
> c.complete(c.future); // bad.
> print("well!");
> var d = Completer();
> d.complete(c.future);
> print("shucks!");
> }
> ```
> from hanging the runtime by busily searching for the end of a cycle.
>
> See https://github.com/dart-lang/sdk/issues/48225
> Fixes #48225
>
> TEST= refactoring covered by existing tests, few new tests.
>
> Change-Id: Id9fc5af5fe011deb0af3e1e8a4ea3a91799f9da4
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/244241
> Reviewed-by: Martin Kustermann <kustermann@google.com>
> Commit-Queue: Lasse Nielsen <lrn@google.com>
TBR=lrn@google.com,kustermann@google.com,sra@google.com,sigmund@google.com,nshahan@google.com
Change-Id: I455be5a04b4c346df26d4ded0fa7388baccb0f8c
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/247762
Reviewed-by: Siva Annamalai <asiva@google.com>
Reviewed-by: Alexander Aprelev <aam@google.com>
Commit-Queue: Alexander Aprelev <aam@google.com>
212 lines
6.5 KiB
Dart
212 lines
6.5 KiB
Dart
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
import 'package:observatory/service_io.dart';
|
|
import 'package:test/test.dart';
|
|
import 'test_helper.dart';
|
|
import 'dart:async';
|
|
|
|
int counter = 0;
|
|
|
|
void periodicTask(_) {
|
|
counter++;
|
|
counter++; // Line 15. We set our breakpoint here.
|
|
counter++;
|
|
if (counter % 300 == 0) {
|
|
print('counter = $counter');
|
|
}
|
|
}
|
|
|
|
void startTimer() {
|
|
new Timer.periodic(const Duration(milliseconds: 10), periodicTask);
|
|
}
|
|
|
|
var tests = <IsolateTest>[
|
|
// Pause
|
|
(Isolate isolate) async {
|
|
Completer completer = new Completer();
|
|
var stream = await isolate.vm.getEventStream(VM.kDebugStream);
|
|
var subscription;
|
|
subscription = stream.listen((ServiceEvent event) {
|
|
if (event.kind == ServiceEvent.kPauseInterrupted) {
|
|
subscription.cancel();
|
|
completer.complete();
|
|
}
|
|
});
|
|
isolate.pause();
|
|
await completer.future;
|
|
},
|
|
|
|
// Resume
|
|
(Isolate isolate) async {
|
|
Completer completer = new Completer();
|
|
var stream = await isolate.vm.getEventStream(VM.kDebugStream);
|
|
var subscription;
|
|
subscription = stream.listen((ServiceEvent event) {
|
|
if (event.kind == ServiceEvent.kResume) {
|
|
subscription.cancel();
|
|
completer.complete();
|
|
}
|
|
});
|
|
isolate.resume();
|
|
await completer.future;
|
|
},
|
|
|
|
// Add breakpoint
|
|
(Isolate isolate) async {
|
|
await isolate.rootLibrary.load() as Library;
|
|
|
|
// Set up a listener to wait for breakpoint events.
|
|
Completer completer = new Completer();
|
|
var stream = await isolate.vm.getEventStream(VM.kDebugStream);
|
|
var subscription;
|
|
subscription = stream.listen((ServiceEvent event) {
|
|
if (event.kind == ServiceEvent.kPauseBreakpoint) {
|
|
print('Breakpoint reached');
|
|
subscription.cancel();
|
|
completer.complete();
|
|
}
|
|
});
|
|
|
|
var script = isolate.rootLibrary.scripts[0];
|
|
await script.load();
|
|
|
|
// Add the breakpoint.
|
|
var result = await isolate.addBreakpoint(script, 15);
|
|
expect(result is Breakpoint, isTrue);
|
|
Breakpoint bpt = result;
|
|
expect(bpt.type, equals('Breakpoint'));
|
|
expect(bpt.location!.script.id, equals(script.id));
|
|
expect(
|
|
bpt.location!.script.tokenToLine(bpt.location!.tokenPos), equals(15));
|
|
expect(isolate.breakpoints.length, equals(1));
|
|
|
|
await completer.future; // Wait for breakpoint events.
|
|
},
|
|
|
|
// We are at the breakpoint on line 15.
|
|
(Isolate isolate) async {
|
|
ServiceMap stack = await isolate.getStack();
|
|
expect(stack.type, equals('Stack'));
|
|
expect(stack['frames'].length, greaterThanOrEqualTo(1));
|
|
|
|
Script script = stack['frames'][0].location.script;
|
|
expect(script.name, endsWith('debugging_test.dart'));
|
|
expect(
|
|
script.tokenToLine(stack['frames'][0].location.tokenPos), equals(15));
|
|
},
|
|
|
|
// Stepping
|
|
(Isolate isolate) async {
|
|
// Set up a listener to wait for breakpoint events.
|
|
Completer completer = new Completer();
|
|
var stream = await isolate.vm.getEventStream(VM.kDebugStream);
|
|
var subscription;
|
|
subscription = stream.listen((ServiceEvent event) {
|
|
if (event.kind == ServiceEvent.kPauseBreakpoint) {
|
|
print('Breakpoint reached');
|
|
subscription.cancel();
|
|
completer.complete();
|
|
}
|
|
});
|
|
|
|
await isolate.stepOver();
|
|
await completer.future; // Wait for breakpoint events.
|
|
},
|
|
|
|
// We are now at line 16.
|
|
(Isolate isolate) async {
|
|
ServiceMap stack = await isolate.getStack();
|
|
expect(stack.type, equals('Stack'));
|
|
expect(stack['frames'].length, greaterThanOrEqualTo(1));
|
|
|
|
Script script = stack['frames'][0].location.script;
|
|
expect(script.name, endsWith('debugging_test.dart'));
|
|
expect(
|
|
script.tokenToLine(stack['frames'][0].location.tokenPos), equals(16));
|
|
},
|
|
|
|
// Remove breakpoint
|
|
(Isolate isolate) async {
|
|
// Set up a listener to wait for breakpoint events.
|
|
Completer completer = new Completer();
|
|
var stream = await isolate.vm.getEventStream(VM.kDebugStream);
|
|
var subscription;
|
|
subscription = stream.listen((ServiceEvent event) {
|
|
if (event.kind == ServiceEvent.kBreakpointRemoved) {
|
|
print('Breakpoint removed');
|
|
expect(isolate.breakpoints.length, equals(0));
|
|
subscription.cancel();
|
|
completer.complete();
|
|
}
|
|
});
|
|
|
|
expect(isolate.breakpoints.length, equals(1));
|
|
var bpt = isolate.breakpoints.values.first;
|
|
await isolate.removeBreakpoint(bpt);
|
|
await completer.future;
|
|
},
|
|
|
|
// Resume
|
|
(Isolate isolate) async {
|
|
Completer completer = new Completer();
|
|
var stream = await isolate.vm.getEventStream(VM.kDebugStream);
|
|
var subscription;
|
|
subscription = stream.listen((ServiceEvent event) {
|
|
if (event.kind == ServiceEvent.kResume) {
|
|
subscription.cancel();
|
|
completer.complete();
|
|
}
|
|
});
|
|
isolate.resume();
|
|
await completer.future;
|
|
},
|
|
|
|
// Add breakpoint at function entry
|
|
(Isolate isolate) async {
|
|
// Set up a listener to wait for breakpoint events.
|
|
Completer completer = new Completer();
|
|
var stream = await isolate.vm.getEventStream(VM.kDebugStream);
|
|
var subscription;
|
|
subscription = stream.listen((ServiceEvent event) {
|
|
if (event.kind == ServiceEvent.kPauseBreakpoint) {
|
|
print('Breakpoint reached');
|
|
subscription.cancel();
|
|
completer.complete();
|
|
}
|
|
});
|
|
|
|
// Find a specific function.
|
|
ServiceFunction function = isolate.rootLibrary.functions
|
|
.firstWhere((f) => f.name == 'periodicTask');
|
|
expect(function, isNotNull);
|
|
|
|
// Add the breakpoint at function entry
|
|
var result = await isolate.addBreakpointAtEntry(function);
|
|
expect(result is Breakpoint, isTrue);
|
|
Breakpoint bpt = result;
|
|
expect(bpt.type, equals('Breakpoint'));
|
|
expect(bpt.location!.script.name, equals('debugging_test.dart'));
|
|
expect(
|
|
bpt.location!.script.tokenToLine(bpt.location!.tokenPos), equals(12));
|
|
expect(isolate.breakpoints.length, equals(1));
|
|
|
|
await completer.future; // Wait for breakpoint events.
|
|
},
|
|
|
|
// We are now at line 13.
|
|
(Isolate isolate) async {
|
|
ServiceMap stack = await isolate.getStack();
|
|
expect(stack.type, equals('Stack'));
|
|
expect(stack['frames'].length, greaterThanOrEqualTo(1));
|
|
|
|
Script script = stack['frames'][0].location.script;
|
|
expect(script.name, endsWith('debugging_test.dart'));
|
|
expect(
|
|
script.tokenToLine(stack['frames'][0].location.tokenPos), equals(12));
|
|
},
|
|
];
|
|
|
|
main(args) => runIsolateTests(args, tests, testeeBefore: startTimer);
|