[Analyzer] Close streams to remove listeners in LSP tests

Change-Id: I7b000de76c81c1d4786d4ced35b6e6f40b0f6ca4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/165320
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Danny Tuppeny <danny@tuppeny.com>
diff --git a/pkg/analysis_server/test/lsp/configuration_test.dart b/pkg/analysis_server/test/lsp/configuration_test.dart
index 0a864ee..2c91650 100644
--- a/pkg/analysis_server/test/lsp/configuration_test.dart
+++ b/pkg/analysis_server/test/lsp/configuration_test.dart
@@ -86,12 +86,15 @@
   }
 
   Future<void> test_configurationRequest_notSupported() async {
-    final configRequest = requestsFromServer
-        .firstWhere((n) => n.method == Method.workspace_configuration);
-    expect(configRequest, doesNotComplete);
+    var didGetConfigRequest = false;
+    requestsFromServer
+        .where((n) => n.method == Method.workspace_configuration)
+        .listen((_) => didGetConfigRequest = true);
 
     await initialize();
     pumpEventQueue();
+
+    expect(didGetConfigRequest, isFalse);
   }
 
   Future<void> test_configurationRequest_supported() async {
diff --git a/pkg/analysis_server/test/lsp/initialization_test.dart b/pkg/analysis_server/test/lsp/initialization_test.dart
index 43e9db8..4caaecc 100644
--- a/pkg/analysis_server/test/lsp/initialization_test.dart
+++ b/pkg/analysis_server/test/lsp/initialization_test.dart
@@ -88,10 +88,8 @@
     // Set a flag if any registerCapability request comes through.
     var didGetRegisterCapabilityRequest = false;
     requestsFromServer
-        .firstWhere((n) => n.method == Method.client_registerCapability)
-        .then((params) {
-      didGetRegisterCapabilityRequest = true;
-    });
+        .where((n) => n.method == Method.client_registerCapability)
+        .listen((_) => didGetRegisterCapabilityRequest = true);
 
     // Initialize with no dynamic registrations advertised.
     final initResponse = await initialize();
diff --git a/pkg/analysis_server/test/lsp/server_abstract.dart b/pkg/analysis_server/test/lsp/server_abstract.dart
index 3d2a1d0..5029f4c 100644
--- a/pkg/analysis_server/test/lsp/server_abstract.dart
+++ b/pkg/analysis_server/test/lsp/server_abstract.dart
@@ -1370,8 +1370,8 @@
         return diagnosticParams.uri == uri.toString();
       }
       return false;
-    });
-    return diagnosticParams.diagnostics;
+    }, orElse: () => null);
+    return diagnosticParams?.diagnostics;
   }
 
   Future<FlutterOutline> waitForFlutterOutline(Uri uri) async {
diff --git a/pkg/analysis_server/test/mocks.dart b/pkg/analysis_server/test/mocks.dart
index 56e583f..396a19f 100644
--- a/pkg/analysis_server/test/mocks.dart
+++ b/pkg/analysis_server/test/mocks.dart
@@ -80,6 +80,12 @@
     if (!_closed.isCompleted) {
       _closed.complete();
     }
+    if (!_serverToClient.isClosed) {
+      _serverToClient.close();
+    }
+    if (!_clientToServer.isClosed) {
+      _clientToServer.close();
+    }
   }
 
   @override