OData / ODataConnectedService

A Visual Studio extension for generating client code for OData Services
Other
81 stars 44 forks source link

Refactor Connected Service tests that are Synchronous to Match their execution Context. #412

Closed marabooy closed 1 month ago

marabooy commented 2 months ago

Is your feature request related to a problem? Please describe.

Currently, most of the Connected Service tests utilize Task.Wait() to ensure that the context does not shift from the Main UI thread while executing certain end-to-end (E2E) code and expectations. However, this approach does not align well with how Visual Studio executes tasks, potentially obscuring issues when transitioning code to an asynchronous model.

For example, SetFileAsEmbeddedResource in ConnectedServiceFileHandler.cs should only execute on the main thread. If it does not, a silent exception is thrown and handled by Visual Studio.

Similarly, the AddFileAsync method in the same file also throws a silent exception when called. These silent exceptions could contribute to slowdowns in Visual Studio.

Describe the solution you'd like

Refactoring the tests to use STAThread could assist in refactoring core components and enable the use of ThreadHelper, preventing extra exceptions when code like AddFileAsync interacts with COM extensions in Visual Studio.

Describe alternatives you've considered

N/A

Additional context

Implementing this solution would also significantly reduce the number of warnings in the Connected Service project and facilitate the migration of other code branches to use asynchronous patterns.

Sample Warnings

"D:\a\1\s\ODataCodeGenTools.sln" (default target) (1) ->
"D:\a\1\s\test\ODataConnectedService.Tests\ODataConnectedService.Tests.csproj" (default target) (5) ->
"D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj" (default target) (6:2) ->
  D:\a\1\s\src\ODataConnectedService.Shared\ConnectedServicePackageInstaller.cs(29,16): warning CS0618: 'IVsPackageInstallerServices' is obsolete: 'Use INuGetProjectService in the NuGet.VisualStudio.Contracts package instead.' [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\ConnectedServicePackageInstaller.cs(53,75): warning CS0618: 'IVsPackageInstallerServices' is obsolete: 'Use INuGetProjectService in the NuGet.VisualStudio.Contracts package instead.' [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\ConnectedServicePackageInstaller.cs(69,26): warning CS0618: 'IVsPackageInstallerServices.IsPackageInstalled(Project, string)' is obsolete: 'This method can cause UI delays if called on the UI thread. Use INuGetProjectService.GetInstalledPackagesAsync in the NuGet.VisualStudio.Contracts package instead, and check the specific package you're interested in' [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\ConnectedServicePackageInstaller.cs(72,25): warning CS0618: 'IVsPackageInstaller.InstallPackage(string, Project, string, Version, bool)' is obsolete: 'System.Version does not support SemVer pre-release versions. Use the overload with string version instead.' [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\Common\UserSettingsPersistenceHelper.cs(137,113): warning VSTHRD002: Synchronously waiting on tasks or awaiters may cause deadlocks. Use await or JoinableTaskFactory.Run instead. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD002.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\Common\ProjectHelper.cs(28,43): warning VSTHRD010: Accessing "IVsHierarchy" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\Common\ProjectHelper.cs(37,20): warning VSTHRD010: Accessing "Project" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\Common\ProjectHelper.cs(42,56): warning VSTHRD010: Accessing "Property" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\Common\ProjectHelper.cs(42,39): warning VSTHRD010: Accessing "Properties" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\Common\ProjectHelper.cs(47,39): warning VSTHRD010: Accessing "CodeModel" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\Common\ProjectHelper.cs(47,29): warning VSTHRD010: Accessing "Project" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\Common\ProjectHelper.cs(42,28): warning VSTHRD010: Accessing "Project" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\Views\ConfigODataEndpoint.xaml.cs(61,28): warning VSTHRD100: Avoid "async void" methods, because any exceptions not handled by the method will crash the process (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD100.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\ConnectedServiceFileHandler.cs(64,90): warning VSTHRD010: Accessing "DTE" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\ConnectedServiceFileHandler.cs(67,208): warning VSTHRD010: Accessing "ProjectItems" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\ConnectedServiceFileHandler.cs(67,195): warning VSTHRD010: Accessing "ProjectItem" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\ConnectedServiceFileHandler.cs(67,101): warning VSTHRD010: Accessing "ProjectItems" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\ConnectedServiceFileHandler.cs(67,88): warning VSTHRD010: Accessing "ProjectItem" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\ConnectedServiceFileHandler.cs(67,61): warning VSTHRD010: Accessing "ProjectItems" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\ConnectedServiceFileHandler.cs(67,48): warning VSTHRD010: Accessing "Project" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\ConnectedServiceFileHandler.cs(68,60): warning VSTHRD010: Accessing "Property" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\ConnectedServiceFileHandler.cs(68,40): warning VSTHRD010: Accessing "Properties" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]
  D:\a\1\s\src\ODataConnectedService.Shared\ConnectedServiceFileHandler.cs(68,29): warning VSTHRD010: Accessing "ProjectItem" should only be done on the main thread. Call Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread() first. (https://github.com/Microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD010.md) [D:\a\1\s\src\ODataConnectedService_VS2022Plus\ODataConnectedService_VS2022Plus.csproj]