Replace expect() with outOfTestExpect() in Analysis Server performance benchmark.

In package:test function expect() does not want to be used outside of
test() methods. So, we need a partial copy for using it in integration
tesitng code, and performance benchmarks.

R=paulberry@google.com, brianwilkerson@google.com
BUG=

Review URL: https://codereview.chromium.org/2413573003 .
This commit is contained in:
Konstantin Shcheglov 2016-10-12 12:57:03 -07:00
parent 08d70cdca5
commit 33ecec5d4f
6 changed files with 104 additions and 64 deletions

View file

@ -11,6 +11,7 @@ import 'dart:math';
import 'package:analysis_server/plugin/protocol/protocol.dart';
import 'package:test/test.dart';
import '../../test/integration/integration_tests.dart';
import 'performance_tests.dart';
void printBenchmarkResults(String id, String description, List<int> times) {
@ -46,10 +47,10 @@ class BenchmarkScenario extends AbstractTimingTest {
String file,
FileChange fileChange,
int numOfRepeats}) async {
expect(roots, isNotNull, reason: 'roots');
expect(file, isNotNull, reason: 'file');
expect(fileChange, isNotNull, reason: 'fileChange');
expect(numOfRepeats, isNotNull, reason: 'numOfRepeats');
outOfTestExpect(roots, isNotNull, reason: 'roots');
outOfTestExpect(file, isNotNull, reason: 'file');
outOfTestExpect(fileChange, isNotNull, reason: 'fileChange');
outOfTestExpect(numOfRepeats, isNotNull, reason: 'numOfRepeats');
// Initialize Analysis Server.
await super.setUp();
await subscribeToStatusNotifications();
@ -95,11 +96,11 @@ class BenchmarkScenario extends AbstractTimingTest {
FileChange fileChange,
String completeAfterStr,
int numOfRepeats}) async {
expect(roots, isNotNull, reason: 'roots');
expect(file, isNotNull, reason: 'file');
expect(fileChange, isNotNull, reason: 'fileChange');
expect(completeAfterStr, isNotNull, reason: 'completeAfterStr');
expect(numOfRepeats, isNotNull, reason: 'numOfRepeats');
outOfTestExpect(roots, isNotNull, reason: 'roots');
outOfTestExpect(file, isNotNull, reason: 'file');
outOfTestExpect(fileChange, isNotNull, reason: 'fileChange');
outOfTestExpect(completeAfterStr, isNotNull, reason: 'completeAfterStr');
outOfTestExpect(numOfRepeats, isNotNull, reason: 'numOfRepeats');
// Initialize Analysis Server.
await super.setUp();
await subscribeToStatusNotifications();
@ -149,13 +150,14 @@ class BenchmarkScenario extends AbstractTimingTest {
RefactoringKind refactoringKind,
RefactoringOptions refactoringOptions,
int numOfRepeats}) async {
expect(roots, isNotNull, reason: 'roots');
expect(file, isNotNull, reason: 'file');
expect(fileChange, isNotNull, reason: 'fileChange');
expect(refactoringAtStr, isNotNull, reason: 'refactoringAtStr');
expect(refactoringKind, isNotNull, reason: 'refactoringKind');
expect(refactoringOptions, isNotNull, reason: 'refactoringOptions');
expect(numOfRepeats, isNotNull, reason: 'numOfRepeats');
outOfTestExpect(roots, isNotNull, reason: 'roots');
outOfTestExpect(file, isNotNull, reason: 'file');
outOfTestExpect(fileChange, isNotNull, reason: 'fileChange');
outOfTestExpect(refactoringAtStr, isNotNull, reason: 'refactoringAtStr');
outOfTestExpect(refactoringKind, isNotNull, reason: 'refactoringKind');
outOfTestExpect(refactoringOptions, isNotNull,
reason: 'refactoringOptions');
outOfTestExpect(numOfRepeats, isNotNull, reason: 'numOfRepeats');
// Initialize Analysis Server.
await super.setUp();
await subscribeToStatusNotifications();
@ -243,8 +245,8 @@ class BenchmarkScenario extends AbstractTimingTest {
*/
static Future<List<int>> start_waitInitialAnalysis_shutdown(
{List<String> roots, int numOfRepeats}) async {
expect(roots, isNotNull, reason: 'roots');
expect(numOfRepeats, isNotNull, reason: 'numOfRepeats');
outOfTestExpect(roots, isNotNull, reason: 'roots');
outOfTestExpect(numOfRepeats, isNotNull, reason: 'numOfRepeats');
// Repeat.
List<int> times = <int>[];
for (int i = 0; i < numOfRepeats; i++) {
@ -265,7 +267,8 @@ class BenchmarkScenario extends AbstractTimingTest {
static String _getFileContent(String path) {
File file = new File(path);
expect(file.existsSync(), isTrue, reason: 'File $path does not exist.');
outOfTestExpect(file.existsSync(), isTrue,
reason: 'File $path does not exist.');
return file.readAsStringSync();
}
@ -274,7 +277,7 @@ class BenchmarkScenario extends AbstractTimingTest {
*/
static int _indexOf(String file, String where, String what) {
int index = where.indexOf(what);
expect(index, isNot(-1), reason: 'Cannot find |$what| in $file.');
outOfTestExpect(index, isNot(-1), reason: 'Cannot find |$what| in $file.');
return index;
}
@ -293,11 +296,16 @@ class FileChange {
final String replaceWhat;
final String replaceWith;
FileChange({this.afterStr, this.afterStrBack: 0, this.insertStr, this.replaceWhat, this.replaceWith}) {
FileChange(
{this.afterStr,
this.afterStrBack: 0,
this.insertStr,
this.replaceWhat,
this.replaceWith}) {
if (afterStr != null) {
expect(insertStr, isNotNull, reason: 'insertStr');
outOfTestExpect(insertStr, isNotNull, reason: 'insertStr');
} else if (replaceWhat != null) {
expect(replaceWith, isNotNull, reason: 'replaceWith');
outOfTestExpect(replaceWith, isNotNull, reason: 'replaceWith');
}
}
}

View file

@ -65,7 +65,7 @@ class AnalysisServerMemoryUsageTest
});
Completer serverConnected = new Completer();
onServerConnected.listen((_) {
expect(serverConnected.isCompleted, isFalse);
outOfTestExpect(serverConnected.isCompleted, isFalse);
serverConnected.complete();
});
return startServer(servicesPort: vmServicePort).then((_) {
@ -109,8 +109,8 @@ class AnalysisServerMemoryUsageTest
*/
static Future<List<int>> start_waitInitialAnalysis_shutdown(
{List<String> roots, int numOfRepeats}) async {
expect(roots, isNotNull, reason: 'roots');
expect(numOfRepeats, isNotNull, reason: 'numOfRepeats');
outOfTestExpect(roots, isNotNull, reason: 'roots');
outOfTestExpect(numOfRepeats, isNotNull, reason: 'numOfRepeats');
// Repeat.
List<int> sizes = <int>[];
for (int i = 0; i < numOfRepeats; i++) {

View file

@ -43,7 +43,7 @@ abstract class AbstractAnalysisServerPerformanceTest
});
Completer serverConnected = new Completer();
onServerConnected.listen((_) {
expect(serverConnected.isCompleted, isFalse);
outOfTestExpect(serverConnected.isCompleted, isFalse);
serverConnected.complete();
});
return startServer(checked: false).then((_) {

View file

@ -51,7 +51,7 @@ abstract class IntegrationTestMixin {
*/
Future sendServerShutdown() async {
var result = await server.send("server.shutdown", null);
expect(result, isNull);
outOfTestExpect(result, isNull);
return null;
}
@ -72,7 +72,7 @@ abstract class IntegrationTestMixin {
Future sendServerSetSubscriptions(List<ServerService> subscriptions) async {
var params = new ServerSetSubscriptionsParams(subscriptions).toJson();
var result = await server.send("server.setSubscriptions", params);
expect(result, isNull);
outOfTestExpect(result, isNull);
return null;
}
@ -376,7 +376,7 @@ abstract class IntegrationTestMixin {
Future sendAnalysisReanalyze({List<String> roots}) async {
var params = new AnalysisReanalyzeParams(roots: roots).toJson();
var result = await server.send("analysis.reanalyze", params);
expect(result, isNull);
outOfTestExpect(result, isNull);
return null;
}
@ -442,7 +442,7 @@ abstract class IntegrationTestMixin {
Future sendAnalysisSetAnalysisRoots(List<String> included, List<String> excluded, {Map<String, String> packageRoots}) async {
var params = new AnalysisSetAnalysisRootsParams(included, excluded, packageRoots: packageRoots).toJson();
var result = await server.send("analysis.setAnalysisRoots", params);
expect(result, isNull);
outOfTestExpect(result, isNull);
return null;
}
@ -464,7 +464,7 @@ abstract class IntegrationTestMixin {
Future sendAnalysisSetGeneralSubscriptions(List<GeneralAnalysisService> subscriptions) async {
var params = new AnalysisSetGeneralSubscriptionsParams(subscriptions).toJson();
var result = await server.send("analysis.setGeneralSubscriptions", params);
expect(result, isNull);
outOfTestExpect(result, isNull);
return null;
}
@ -496,7 +496,7 @@ abstract class IntegrationTestMixin {
Future sendAnalysisSetPriorityFiles(List<String> files) async {
var params = new AnalysisSetPriorityFilesParams(files).toJson();
var result = await server.send("analysis.setPriorityFiles", params);
expect(result, isNull);
outOfTestExpect(result, isNull);
return null;
}
@ -535,7 +535,7 @@ abstract class IntegrationTestMixin {
Future sendAnalysisSetSubscriptions(Map<AnalysisService, List<String>> subscriptions) async {
var params = new AnalysisSetSubscriptionsParams(subscriptions).toJson();
var result = await server.send("analysis.setSubscriptions", params);
expect(result, isNull);
outOfTestExpect(result, isNull);
return null;
}
@ -579,7 +579,7 @@ abstract class IntegrationTestMixin {
Future sendAnalysisUpdateOptions(AnalysisOptions options) async {
var params = new AnalysisUpdateOptionsParams(options).toJson();
var result = await server.send("analysis.updateOptions", params);
expect(result, isNull);
outOfTestExpect(result, isNull);
return null;
}
@ -1503,7 +1503,7 @@ abstract class IntegrationTestMixin {
Future sendExecutionDeleteContext(String id) async {
var params = new ExecutionDeleteContextParams(id).toJson();
var result = await server.send("execution.deleteContext", params);
expect(result, isNull);
outOfTestExpect(result, isNull);
return null;
}
@ -1579,7 +1579,7 @@ abstract class IntegrationTestMixin {
Future sendExecutionSetSubscriptions(List<ExecutionService> subscriptions) async {
var params = new ExecutionSetSubscriptionsParams(subscriptions).toJson();
var result = await server.send("execution.setSubscriptions", params);
expect(result, isNull);
outOfTestExpect(result, isNull);
return null;
}
@ -1678,71 +1678,71 @@ abstract class IntegrationTestMixin {
ResponseDecoder decoder = new ResponseDecoder(null);
switch (event) {
case "server.connected":
expect(params, isServerConnectedParams);
outOfTestExpect(params, isServerConnectedParams);
_onServerConnected.add(new ServerConnectedParams.fromJson(decoder, 'params', params));
break;
case "server.error":
expect(params, isServerErrorParams);
outOfTestExpect(params, isServerErrorParams);
_onServerError.add(new ServerErrorParams.fromJson(decoder, 'params', params));
break;
case "server.status":
expect(params, isServerStatusParams);
outOfTestExpect(params, isServerStatusParams);
_onServerStatus.add(new ServerStatusParams.fromJson(decoder, 'params', params));
break;
case "analysis.analyzedFiles":
expect(params, isAnalysisAnalyzedFilesParams);
outOfTestExpect(params, isAnalysisAnalyzedFilesParams);
_onAnalysisAnalyzedFiles.add(new AnalysisAnalyzedFilesParams.fromJson(decoder, 'params', params));
break;
case "analysis.errors":
expect(params, isAnalysisErrorsParams);
outOfTestExpect(params, isAnalysisErrorsParams);
_onAnalysisErrors.add(new AnalysisErrorsParams.fromJson(decoder, 'params', params));
break;
case "analysis.flushResults":
expect(params, isAnalysisFlushResultsParams);
outOfTestExpect(params, isAnalysisFlushResultsParams);
_onAnalysisFlushResults.add(new AnalysisFlushResultsParams.fromJson(decoder, 'params', params));
break;
case "analysis.folding":
expect(params, isAnalysisFoldingParams);
outOfTestExpect(params, isAnalysisFoldingParams);
_onAnalysisFolding.add(new AnalysisFoldingParams.fromJson(decoder, 'params', params));
break;
case "analysis.highlights":
expect(params, isAnalysisHighlightsParams);
outOfTestExpect(params, isAnalysisHighlightsParams);
_onAnalysisHighlights.add(new AnalysisHighlightsParams.fromJson(decoder, 'params', params));
break;
case "analysis.implemented":
expect(params, isAnalysisImplementedParams);
outOfTestExpect(params, isAnalysisImplementedParams);
_onAnalysisImplemented.add(new AnalysisImplementedParams.fromJson(decoder, 'params', params));
break;
case "analysis.invalidate":
expect(params, isAnalysisInvalidateParams);
outOfTestExpect(params, isAnalysisInvalidateParams);
_onAnalysisInvalidate.add(new AnalysisInvalidateParams.fromJson(decoder, 'params', params));
break;
case "analysis.navigation":
expect(params, isAnalysisNavigationParams);
outOfTestExpect(params, isAnalysisNavigationParams);
_onAnalysisNavigation.add(new AnalysisNavigationParams.fromJson(decoder, 'params', params));
break;
case "analysis.occurrences":
expect(params, isAnalysisOccurrencesParams);
outOfTestExpect(params, isAnalysisOccurrencesParams);
_onAnalysisOccurrences.add(new AnalysisOccurrencesParams.fromJson(decoder, 'params', params));
break;
case "analysis.outline":
expect(params, isAnalysisOutlineParams);
outOfTestExpect(params, isAnalysisOutlineParams);
_onAnalysisOutline.add(new AnalysisOutlineParams.fromJson(decoder, 'params', params));
break;
case "analysis.overrides":
expect(params, isAnalysisOverridesParams);
outOfTestExpect(params, isAnalysisOverridesParams);
_onAnalysisOverrides.add(new AnalysisOverridesParams.fromJson(decoder, 'params', params));
break;
case "completion.results":
expect(params, isCompletionResultsParams);
outOfTestExpect(params, isCompletionResultsParams);
_onCompletionResults.add(new CompletionResultsParams.fromJson(decoder, 'params', params));
break;
case "search.results":
expect(params, isSearchResultsParams);
outOfTestExpect(params, isSearchResultsParams);
_onSearchResults.add(new SearchResultsParams.fromJson(decoder, 'params', params));
break;
case "execution.launchData":
expect(params, isExecutionLaunchDataParams);
outOfTestExpect(params, isExecutionLaunchDataParams);
_onExecutionLaunchData.add(new ExecutionLaunchDataParams.fromJson(decoder, 'params', params));
break;
default:

View file

@ -39,6 +39,38 @@ Matcher isMapOf(Matcher keyMatcher, Matcher valueMatcher) =>
Matcher isOneOf(List<Matcher> choiceMatchers) => new _OneOf(choiceMatchers);
/**
* Assert that [actual] matches [matcher].
*/
void outOfTestExpect(actual, matcher,
{String reason, skip, bool verbose: false}) {
var matchState = {};
try {
if (matcher.matches(actual, matchState)) return;
} catch (e, trace) {
if (reason == null) {
reason = '${(e is String) ? e : e.toString()} at $trace';
}
}
fail(_defaultFailFormatter(actual, matcher, reason, matchState, verbose));
}
String _defaultFailFormatter(
actual, Matcher matcher, String reason, Map matchState, bool verbose) {
var description = new StringDescription();
description.add('Expected: ').addDescriptionOf(matcher).add('\n');
description.add(' Actual: ').addDescriptionOf(actual).add('\n');
var mismatchDescription = new StringDescription();
matcher.describeMismatch(actual, mismatchDescription, matchState, verbose);
if (mismatchDescription.length > 0) {
description.add(' Which: $mismatchDescription\n');
}
if (reason != null) description.add(reason).add('\n');
return description.toString();
}
/**
* Type of closures used by LazyMatcher.
*/
@ -111,7 +143,7 @@ abstract class AbstractAnalysisServerIntegrationTest
StreamSubscription subscription;
// This will only work if the caller has already subscribed to
// SERVER_STATUS (e.g. using sendServerSetSubscriptions(['STATUS']))
expect(_subscribedToServerStatus, isTrue);
outOfTestExpect(_subscribedToServerStatus, isTrue);
subscription = onServerStatus.listen((ServerStatusParams params) {
if (params.analysis != null && !params.analysis.isAnalyzing) {
completer.complete(params);
@ -147,7 +179,7 @@ abstract class AbstractAnalysisServerIntegrationTest
});
Completer serverConnected = new Completer();
onServerConnected.listen((_) {
expect(serverConnected.isCompleted, isFalse);
outOfTestExpect(serverConnected.isCompleted, isFalse);
serverConnected.complete();
});
onServerError.listen((ServerErrorParams params) {
@ -525,10 +557,10 @@ class Server {
_badDataFromServer('JSON decode failure: $exception');
return;
}
expect(message, isMap);
outOfTestExpect(message, isMap);
Map messageAsMap = message;
if (messageAsMap.containsKey('id')) {
expect(messageAsMap['id'], isString);
outOfTestExpect(messageAsMap['id'], isString);
String id = message['id'];
Completer completer = _pendingCommands[id];
if (completer == null) {
@ -546,17 +578,17 @@ class Server {
// Check that the message is well-formed. We do this after calling
// completer.complete() or completer.completeError() so that we don't
// stall the test in the event of an error.
expect(message, isResponse);
outOfTestExpect(message, isResponse);
} else {
// Message is a notification. It should have an event and possibly
// params.
expect(messageAsMap, contains('event'));
expect(messageAsMap['event'], isString);
outOfTestExpect(messageAsMap, contains('event'));
outOfTestExpect(messageAsMap['event'], isString);
notificationProcessor(messageAsMap['event'], messageAsMap['params']);
// Check that the message is well-formed. We do this after calling
// notificationController.add() so that we don't stall the test in the
// event of an error.
expect(message, isNotification);
outOfTestExpect(message, isNotification);
}
});
_process.stderr

View file

@ -179,7 +179,7 @@ class CodegenInttestMethodsVisitor extends DartCodegenVisitor
indent(() {
String paramsValidator = camelJoin(
['is', notification.domainName, notification.event, 'params']);
writeln('expect(params, $paramsValidator);');
writeln('outOfTestExpect(params, $paramsValidator);');
String constructorCall;
if (notification.params == null) {
constructorCall = 'new $className()';
@ -255,7 +255,7 @@ class CodegenInttestMethodsVisitor extends DartCodegenVisitor
writeln('ResponseDecoder decoder = new ResponseDecoder($kind);');
writeln("return new $resultClass.fromJson(decoder, 'result', result);");
} else {
writeln('expect(result, isNull);');
writeln('outOfTestExpect(result, isNull);');
writeln('return null;');
}
});