Files
sdk/pkg/vm_service/test/debugging_test.dart
T
Siva Annamalai 5cb3b37c74 Revert "Refactor _Future."
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>
2022-06-09 16:51:55 +00:00

244 lines
8.3 KiB
Dart

// Copyright (c) 2019, 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 'dart:async';
import 'package:test/test.dart';
import 'package:vm_service/vm_service.dart';
import 'common/service_test_common.dart';
import 'common/test_helper.dart';
int counter = 0;
void periodicTask(_) {
counter++;
counter++; // Line 16. We set our breakpoint here.
counter++;
if (counter % 300 == 0) {
print('counter = $counter');
}
}
void startTimer() {
Timer.periodic(const Duration(milliseconds: 10), periodicTask);
}
int getLineNumberFromTokenPos(Script s, int token) =>
s.tokenPosTable![token].first;
var tests = <IsolateTest>[
// Pause
(VmService? service, IsolateRef? isolateRef) async {
final isolateId = isolateRef!.id!;
Completer completer = Completer();
var stream = service!.onDebugEvent;
late var subscription;
subscription = stream.listen((Event event) {
if (event.kind == EventKind.kPauseInterrupted) {
subscription.cancel();
completer.complete();
}
});
await service.streamListen(EventStreams.kDebug);
await service.pause(isolateId);
await completer.future;
await service.streamCancel(EventStreams.kDebug);
},
// Resume
(VmService service, IsolateRef isolate) async {
final isolateId = isolate.id!;
Completer completer = Completer();
var stream = service.onDebugEvent;
late var subscription;
subscription = stream.listen((Event event) {
if (event.kind == EventKind.kResume) {
subscription.cancel();
completer.complete();
}
});
await service.streamListen(EventStreams.kDebug);
await service.resume(isolateId);
await completer.future;
await service.streamCancel(EventStreams.kDebug);
},
// Add breakpoint
(VmService service, IsolateRef isolateRef) async {
final isolateId = isolateRef.id!;
Isolate isolate = await service.getIsolate(isolateId);
final Library rootLib =
(await service.getObject(isolateId, isolate.rootLib!.id!)) as Library;
// Set up a listener to wait for breakpoint events.
Completer completer = Completer();
var stream = service.onDebugEvent;
late var subscription;
subscription = stream.listen((Event event) {
if (event.kind == EventKind.kPauseBreakpoint) {
print('Breakpoint reached');
subscription.cancel();
completer.complete();
}
});
await service.streamListen(EventStreams.kDebug);
final Script script = (await service.getObject(
isolateId, rootLib.scripts!.first.id!)) as Script;
// Add the breakpoint.
final Breakpoint bpt =
await service.addBreakpoint(isolateId, script.id!, 16);
final SourceLocation location = bpt.location;
expect(location.script!.id, script.id);
expect(script.getLineNumberFromTokenPos(location.tokenPos!), 16);
expect(location.line, 16);
isolate = await service.getIsolate(isolateId);
expect(isolate.breakpoints!.length, 1);
await completer.future; // Wait for breakpoint events.
await service.streamCancel(EventStreams.kDebug);
},
// We are at the breakpoint on line 16.
(VmService service, IsolateRef isolateRef) async {
final isolateId = isolateRef.id!;
final stack = await service.getStack(isolateId);
expect(stack.frames!.length, greaterThanOrEqualTo(1));
final location = stack.frames![0].location!;
Script script =
(await service.getObject(isolateId, location.script!.id!)) as Script;
expect(script.uri, endsWith('debugging_test.dart'));
expect(script.getLineNumberFromTokenPos(location.tokenPos!), 16);
expect(location.line, 16);
},
// Stepping
(VmService service, IsolateRef isolate) async {
// Set up a listener to wait for breakpoint events.
final completer = Completer();
var stream = service.onDebugEvent;
late var subscription;
subscription = stream.listen((Event event) {
if (event.kind == EventKind.kPauseBreakpoint) {
print('Breakpoint reached');
subscription.cancel();
completer.complete();
}
});
print('performing step over');
await stepOver(service, isolate);
print('step over done');
await completer.future; // Wait for breakpoint events.
print('breakpoint completed');
},
// We are now at line 17.
(VmService service, IsolateRef isolateRef) async {
final isolateId = isolateRef.id!;
final stack = await service.getStack(isolateId);
expect(stack.frames!.length, greaterThanOrEqualTo(1));
final location = stack.frames![0].location!;
final Script script =
(await service.getObject(isolateId, location.script!.id!)) as Script;
expect(script.uri, endsWith('debugging_test.dart'));
expect(script.getLineNumberFromTokenPos(location.tokenPos!), 17);
expect(location.line, 17);
},
// Remove breakpoint
(VmService service, IsolateRef isolateRef) async {
final isolateId = isolateRef.id!;
// Set up a listener to wait for breakpoint events.
final completer = Completer();
var stream = service.onDebugEvent;
late var subscription;
subscription = stream.listen((Event event) async {
if (event.kind == EventKind.kBreakpointRemoved) {
print('Breakpoint removed');
final isolate = await service.getIsolate(isolateId);
expect(isolate.breakpoints!.length, 0);
subscription.cancel();
completer.complete();
}
});
final Isolate isolate = await service.getIsolate(isolateId);
expect(isolate.breakpoints!.length, 1);
final bpt = isolate.breakpoints!.first;
await service.streamListen(EventStreams.kDebug);
await service.removeBreakpoint(isolateId, bpt.id!);
await completer.future;
await service.streamCancel(EventStreams.kDebug);
},
// Resume
(VmService service, IsolateRef isolate) async {
final completer = Completer();
var stream = service.onDebugEvent;
late var subscription;
subscription = stream.listen((Event event) {
if (event.kind == EventKind.kResume) {
subscription.cancel();
completer.complete();
}
});
await resumeIsolate(service, isolate);
await completer.future;
},
// Add breakpoint at function entry
(VmService service, IsolateRef isolateRef) async {
final isolateId = isolateRef.id!;
Isolate isolate = await service.getIsolate(isolateId);
// Set up a listener to wait for breakpoint events.
final completer = Completer();
var stream = service.onDebugEvent;
late var subscription;
subscription = stream.listen((Event event) {
if (event.kind == EventKind.kPauseBreakpoint) {
print('Breakpoint reached');
subscription.cancel();
completer.complete();
}
});
await service.streamListen(EventStreams.kDebug);
final Library rootLib =
(await service.getObject(isolateId, isolate.rootLib!.id!)) as Library;
// Find a specific function.
final FuncRef function =
rootLib.functions!.firstWhere((f) => f.name == 'periodicTask');
expect(function, isNotNull);
// Add the breakpoint at function entry
final bpt = await service.addBreakpointAtEntry(isolateId, function.id!);
final Script script =
(await service.getObject(isolateId, bpt.location.script.id)) as Script;
expect(script.uri, endsWith('debugging_test.dart'));
expect(script.getLineNumberFromTokenPos(bpt.location.tokenPos), 14);
expect(bpt.location.line, 14);
// Refresh isolate state.
isolate = await service.getIsolate(isolateId);
expect(isolate.breakpoints!.length, 1);
await completer.future; // Wait for breakpoint events.
},
// We are now at line 14.
(VmService service, IsolateRef isolateRef) async {
final isolateId = isolateRef.id!;
final stack = await service.getStack(isolateId);
expect(stack.frames!.length, greaterThanOrEqualTo(1));
final location = stack.frames![0].location!;
final Script script =
(await service.getObject(isolateId, location.script!.id!)) as Script;
expect(script.uri, endsWith('debugging_test.dart'));
expect(script.getLineNumberFromTokenPos(location.tokenPos!), 14);
expect(location.line, 14);
},
];
main([args = const <String>[]]) => runIsolateTests(
args,
tests,
'debugging_test.dart',
testeeBefore: startTimer,
);