Files
sdk/pkg/analysis_server/test/integration/support/web_sockets.dart
T
Danny Tuppeny 8884a8b4ad [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 <srawlins@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
2025-01-27 08:47:42 -08:00

41 lines
1.4 KiB
Dart

// Copyright (c) 2024, 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:stream_channel/stream_channel.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import '../../constants.dart';
/// Creates a [StreamChannel] for a connection to a WebSocket at [wsUri] that
/// prints all communication if [debugPrintCommunication] is `true`.
Future<StreamChannel<String>> createLoggedWebSocketChannel(Uri wsUri) async {
var rawChannel = WebSocketChannel.connect(wsUri);
await rawChannel.ready;
var rawStringChannel = rawChannel.cast<String>();
/// A helper to create a function that can be used in stream.map() to log
/// traffic with a prefix.
String Function(String) logTraffic(String prefix) {
return (String data) {
if (debugPrintCommunication) {
print('$prefix $data'.trim());
}
return data;
};
}
// Create a channel that logs the data going through it.
var loggedInput = rawStringChannel.stream.map(logTraffic('DTD ==>'));
var loggedOutputController = StreamController<String>();
unawaited(
loggedOutputController.stream
.map(logTraffic('DTD <=='))
.pipe(rawStringChannel.sink),
);
return StreamChannel<String>(loggedInput, loggedOutputController.sink);
}