[analyzer] Reuse analysis context collection in verify_docs_test

Perhaps there's a good reason not do this, but it seems to work fine for now, and doesn't hide errors. On my computer it brought the test down from 35 seconds to 600 ms.

Closes https://github.com/dart-lang/sdk/issues/54183

Bug: https://github.com/dart-lang/sdk/issues/54183
Change-Id: I15351ce71897534a82da1f649d33b0b7b6180f4c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/339440
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Auto-Submit: Parker Lougheed <parlough@gmail.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/test/verify_docs_test.dart b/pkg/analyzer/test/verify_docs_test.dart
index caaf502..d9a9c8e 100644
--- a/pkg/analyzer/test/verify_docs_test.dart
+++ b/pkg/analyzer/test/verify_docs_test.dart
@@ -25,6 +25,7 @@
   final Folder docFolder;
   final String snippetDirPath;
   final String snippetPath;
+  final AnalysisContextCollection collection;
 
   final StringBuffer output = StringBuffer();
 
@@ -41,7 +42,9 @@
   }
 
   SnippetTester._(
-      this.provider, this.docFolder, this.snippetDirPath, this.snippetPath);
+      this.provider, this.docFolder, this.snippetDirPath, this.snippetPath)
+      : collection = AnalysisContextCollection(
+            resourceProvider: provider, includedPaths: [snippetPath]);
 
   /// Return `true` if the given error is a diagnostic produced by a lint that
   /// is allowed to occur in documentation.
@@ -91,7 +94,7 @@
             if (output.isNotEmpty) {
               fail(output.toString());
             }
-          }, timeout: Timeout.factor(6));
+          }, timeout: Timeout.factor(4));
         }
       } else if (child is Folder) {
         await verifyFolder(child);
@@ -103,7 +106,7 @@
     // TODO(brianwilkerson): When the files outside of 'src' contain only public
     //  API, write code to compute the list of imports so that new public API
     //  will automatically be allowed.
-    String imports = '''
+    const String imports = '''
 import 'dart:math' as math;
 
 import 'package:analyzer/dart/analysis/analysis_context.dart';
@@ -127,13 +130,16 @@
 ''',
         modificationStamp: 1);
     try {
-      AnalysisContextCollection collection = AnalysisContextCollection(
-          includedPaths: <String>[snippetDirPath], resourceProvider: provider);
       List<AnalysisContext> contexts = collection.contexts;
       if (contexts.length != 1) {
         fail('The snippets directory contains multiple analysis contexts.');
       }
-      var results = await contexts[0].currentSession.getErrors(snippetPath);
+      var context = contexts[0];
+      // Mark the snippet as changed since we reuse the same path
+      // for each snippet found.
+      context.changeFile(snippetPath);
+      await context.applyPendingFileChanges();
+      var results = await context.currentSession.getErrors(snippetPath);
       if (results is ErrorsResult) {
         Iterable<AnalysisError> errors = results.errors.where((error) {
           ErrorCode errorCode = error.errorCode;