Skip to content

Commit 8884a8b

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Make it easier to print protocol messages when running LSP-over-Legacy tests
This moves the existing `debugPrintCommunication` flag out of `test/lsp` into `test/` and passes it to the `MockServerChannel` to simplify printing protocol messages for LSP-over-Legacy tests (similar to what MockLspServerChannel already does). Change-Id: Ic5fa6309e8de01ce0c624584ad6eb653ced90698 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/406021 Commit-Queue: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent 6ddd96b commit 8884a8b

File tree

6 files changed

+41
-22
lines changed

6 files changed

+41
-22
lines changed

pkg/analysis_server/lib/src/utilities/mocks.dart

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ class MockServerChannel implements ServerCommunicationChannel {
5656

5757
String? name;
5858

59-
MockServerChannel();
59+
/// True if we are printing out messages exchanged with the server.
60+
final bool printMessages;
61+
62+
MockServerChannel({bool? printMessages})
63+
: printMessages = printMessages ?? false;
6064

6165
/// Return the broadcast stream of notifications.
6266
Stream<Notification> get notifications {
@@ -99,12 +103,14 @@ class MockServerChannel implements ServerCommunicationChannel {
99103

100104
@override
101105
void sendRequest(Request request) {
106+
var jsonString = jsonEncode(request.toJson());
107+
if (printMessages) {
108+
print('<== $jsonString');
109+
}
110+
102111
// Round-trip via JSON to ensure all types are fully serialized as they
103112
// would be in a real setup.
104-
request =
105-
Request.fromJson(
106-
jsonDecode(jsonEncode(request.toJson())) as Map<String, Object?>,
107-
)!;
113+
request = Request.fromJson(jsonDecode(jsonString) as Map<String, Object?>)!;
108114

109115
serverRequestsSent.add(request);
110116
responseController.add(request);
@@ -117,12 +123,15 @@ class MockServerChannel implements ServerCommunicationChannel {
117123
return;
118124
}
119125

126+
var jsonString = jsonEncode(response.toJson());
127+
if (printMessages) {
128+
print('<== $jsonString');
129+
}
130+
120131
// Round-trip via JSON to ensure all types are fully serialized as they
121132
// would be in a real setup.
122133
response =
123-
Response.fromJson(
124-
jsonDecode(jsonEncode(response.toJson())) as Map<String, Object?>,
125-
)!;
134+
Response.fromJson(jsonDecode(jsonString) as Map<String, Object?>)!;
126135

127136
responsesReceived.add(response);
128137
responseController.add(response);
@@ -138,12 +147,14 @@ class MockServerChannel implements ServerCommunicationChannel {
138147
throw Exception('simulateRequestFromClient after connection closed');
139148
}
140149

150+
var jsonString = jsonEncode(request.toJson());
151+
if (printMessages) {
152+
print('==> $jsonString');
153+
}
154+
141155
// Round-trip via JSON to ensure all types are fully serialized as they
142156
// would be in a real setup.
143-
request =
144-
Request.fromJson(
145-
jsonDecode(jsonEncode(request)) as Map<String, Object?>,
146-
)!;
157+
request = Request.fromJson(jsonDecode(jsonString) as Map<String, Object?>)!;
147158

148159
requestController.add(request);
149160
var response = await waitForResponse(request);
@@ -166,12 +177,15 @@ class MockServerChannel implements ServerCommunicationChannel {
166177
throw Exception('simulateRequestFromClient after connection closed');
167178
}
168179

180+
var jsonString = jsonEncode(response.toJson());
181+
if (printMessages) {
182+
print('==> $jsonString');
183+
}
184+
169185
// Round-trip via JSON to ensure all types are fully serialized as they
170186
// would be in a real setup.
171187
response =
172-
Response.fromJson(
173-
jsonDecode(jsonEncode(response)) as Map<String, Object?>,
174-
)!;
188+
Response.fromJson(jsonDecode(jsonString) as Map<String, Object?>)!;
175189

176190
requestController.add(response);
177191
}

pkg/analysis_server/test/analysis_server_base.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import 'package:meta/meta.dart';
2424
import 'package:test/test.dart';
2525
import 'package:unified_analytics/unified_analytics.dart';
2626

27+
import 'constants.dart';
2728
import 'mocks.dart';
2829
import 'support/configuration_files.dart';
2930
import 'test_macros.dart';
@@ -189,7 +190,7 @@ abstract class ContextResolutionTest with ResourceProviderMixin {
189190

190191
@mustCallSuper
191192
void setUp() {
192-
serverChannel = MockServerChannel();
193+
serverChannel = MockServerChannel(printMessages: debugPrintCommunication);
193194

194195
createMockSdk(resourceProvider: resourceProvider, root: sdkRoot);
195196

pkg/analysis_server/test/analysis_server_test.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import 'package:test/test.dart';
2121
import 'package:test_reflective_loader/test_reflective_loader.dart';
2222
import 'package:unified_analytics/unified_analytics.dart';
2323

24+
import 'constants.dart';
25+
2426
void main() {
2527
defineReflectiveSuite(() {
2628
defineReflectiveTests(AnalysisServerTest);
@@ -41,7 +43,7 @@ class AnalysisServerTest with ResourceProviderMixin {
4143
late LegacyAnalysisServer server;
4244

4345
void setUp() {
44-
channel = MockServerChannel();
46+
channel = MockServerChannel(printMessages: debugPrintCommunication);
4547

4648
// Create an SDK in the mock file system.
4749
var sdkRoot = newFolder('/sdk');

pkg/analysis_server/test/constants.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ const String CODE = 'code';
66
const String COMPLETION_RESULTS = 'completion.results';
77
const String CONTEXT_MESSAGES = 'contextMessages';
88
const String CORRECTION = 'correction';
9+
10+
/// Useful for debugging locally, setting this to true will cause all JSON
11+
/// communication to be printed to stdout.
12+
const debugPrintCommunication = false;
13+
914
const String EDITS = 'edits';
1015
const String END_COLUMN = 'endColumn';
1116
const String END_LINE = 'endLine';

pkg/analysis_server/test/integration/support/web_sockets.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'dart:async';
77
import 'package:stream_channel/stream_channel.dart';
88
import 'package:web_socket_channel/web_socket_channel.dart';
99

10-
import '../../lsp/server_abstract.dart';
10+
import '../../constants.dart';
1111

1212
/// Creates a [StreamChannel] for a connection to a WebSocket at [wsUri] that
1313
/// prints all communication if [debugPrintCommunication] is `true`.

pkg/analysis_server/test/lsp/server_abstract.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import 'package:path/path.dart' as path;
3131
import 'package:test/test.dart' hide expect;
3232
import 'package:unified_analytics/unified_analytics.dart';
3333

34+
import '../constants.dart';
3435
import '../mocks.dart';
3536
import '../mocks_lsp.dart';
3637
import '../shared/shared_test_interface.dart';
@@ -41,10 +42,6 @@ import 'request_helpers_mixin.dart';
4142

4243
const dartLanguageId = 'dart';
4344

44-
/// Useful for debugging locally, setting this to true will cause all JSON
45-
/// communication to be printed to stdout.
46-
const debugPrintCommunication = false;
47-
4845
abstract class AbstractLspAnalysisServerTest
4946
with
5047
ResourceProviderMixin,

0 commit comments

Comments
 (0)