From 31d72756ef607d368547c08e00d3ed994e7acf84 Mon Sep 17 00:00:00 2001 From: Leyla <54935347+bigfluffycookie@users.noreply.github.com> Date: Fri, 8 Sep 2023 09:14:29 +0200 Subject: [PATCH] Make secretsAnalyzer ctor free threaded (#4858) * Make secretsAnalyzer ctor free threaded * Remove one lazy --- .../SecretsAnalyzerTests.cs | 29 +++++++++++++++++++ src/CloudSecrets/SecretsAnalyzer.cs | 13 +++++---- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/CloudSecrets.UnitTests/SecretsAnalyzerTests.cs b/src/CloudSecrets.UnitTests/SecretsAnalyzerTests.cs index b1b404a9b7..f947253350 100644 --- a/src/CloudSecrets.UnitTests/SecretsAnalyzerTests.cs +++ b/src/CloudSecrets.UnitTests/SecretsAnalyzerTests.cs @@ -51,6 +51,34 @@ public void MefCtor_CheckIsExported() MefTestHelpers.CreateExport()); } + [TestMethod] + public void Ctor_DoesNotCallAnyNonFreeThreadedServices() + { + // Arrange + var textDocumentFactoryService = new Mock(); + var contentTypeRegistryService = new Mock(); + var analysisStatusNotifierFactory = new Mock(); + var cloudSecretsTelemetryManager = new Mock(); + var ruleSettingsProviderFactory = new Mock(); + var secretDetectors = new[] + { + SetupSecretDetector("rule1", "rule1"), + }; + + // Act + _ = new SecretsAnalyzer(textDocumentFactoryService.Object, contentTypeRegistryService.Object, secretDetectors.Select(x => x.Object), analysisStatusNotifierFactory.Object, + ruleSettingsProviderFactory.Object, cloudSecretsTelemetryManager.Object); + + // The MEF constructor should be free-threaded, which it will be if + // it doesn't make any external calls. + textDocumentFactoryService.Invocations.Should().BeEmpty(); + contentTypeRegistryService.Invocations.Should().BeEmpty(); + analysisStatusNotifierFactory.Invocations.Should().BeEmpty(); + cloudSecretsTelemetryManager.Invocations.Should().BeEmpty(); + ruleSettingsProviderFactory.Invocations.Should().BeEmpty(); + secretDetectors[0].Invocations.Should().BeEmpty(); + } + [TestMethod] public void IsAnalysisSupported_NoLanguages_True() { @@ -390,3 +418,4 @@ private IAnalysisStatusNotifierFactory SetupStatusNotifierFactory(IAnalysisStatu } } } + diff --git a/src/CloudSecrets/SecretsAnalyzer.cs b/src/CloudSecrets/SecretsAnalyzer.cs index ce6f0889bc..2eab0e3126 100644 --- a/src/CloudSecrets/SecretsAnalyzer.cs +++ b/src/CloudSecrets/SecretsAnalyzer.cs @@ -38,10 +38,10 @@ internal class SecretsAnalyzer : IAnalyzer private readonly ITextDocumentFactoryService textDocumentFactoryService; private readonly IEnumerable secretDetectors; private readonly IAnalysisStatusNotifierFactory analysisStatusNotifierFactory; - private readonly IRuleSettingsProvider ruleSettingsProvider; + private readonly Lazy ruleSettingsProvider; private readonly ICloudSecretsTelemetryManager cloudSecretsTelemetryManager; private readonly ISecretsToAnalysisIssueConverter secretsToAnalysisIssueConverter; - private readonly IContentType filesContentType; + private readonly IContentTypeRegistryService contentTypeRegistryService; [ImportingConstructor] public SecretsAnalyzer( @@ -74,8 +74,9 @@ internal SecretsAnalyzer(ITextDocumentFactoryService textDocumentFactoryService, this.analysisStatusNotifierFactory = analysisStatusNotifierFactory; this.cloudSecretsTelemetryManager = cloudSecretsTelemetryManager; this.secretsToAnalysisIssueConverter = secretsToAnalysisIssueConverter; - filesContentType = contentTypeRegistryService.UnknownContentType; - ruleSettingsProvider = ruleSettingsProviderFactory.Get(Language.Secrets); + this.contentTypeRegistryService = contentTypeRegistryService; + + ruleSettingsProvider = new Lazy(() => ruleSettingsProviderFactory.Get(Language.Secrets)); } public bool IsAnalysisSupported(IEnumerable languages) @@ -98,10 +99,10 @@ public void ExecuteAnalysis(string filePath, { var stopwatch = Stopwatch.StartNew(); - var textDocument = textDocumentFactoryService.CreateAndLoadTextDocument(filePath, filesContentType); // load the document from disc + var textDocument = textDocumentFactoryService.CreateAndLoadTextDocument(filePath, contentTypeRegistryService.UnknownContentType); // load the document from disc var currentSnapshot = textDocument.TextBuffer.CurrentSnapshot; var fileContent = currentSnapshot.GetText(); - var rulesSettings = ruleSettingsProvider.Get(); + var rulesSettings = ruleSettingsProvider.Value.Get(); var issues = new List();