|
1 | 1 | using System.Collections.Generic; |
2 | 2 | using System.Diagnostics; |
3 | 3 | using System.IO; |
| 4 | +using System.Threading; |
4 | 5 | using System.Threading.Tasks; |
5 | 6 | using Microsoft.Test.Apex.VisualStudio.Solution; |
6 | 7 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 8 | +using NuGet.PackageManagement; |
7 | 9 |
|
8 | 10 | namespace NuGet.Tests.Apex |
9 | 11 | { |
@@ -249,25 +251,27 @@ public async Task InstallPackageFromPMC_TriggersNuGetCacheUpdatedEventAsync() |
249 | 251 | testContext.SolutionService.Build(); |
250 | 252 | testContext.NuGetApexTestService.WaitForAutoRestore(); |
251 | 253 |
|
252 | | - var nugetConsole = GetConsole(testContext.Project); |
| 254 | + // Subscribe to the ISolutionManager.AfterNuGetCacheUpdated event |
| 255 | + using var cacheUpdatedEvent = new ManualResetEventSlim(false); |
| 256 | + var solutionManager = testContext.NuGetApexTestService.SolutionManager; |
| 257 | + void OnAfterNuGetCacheUpdated(object sender, NuGetEventArgs<string> e) => cacheUpdatedEvent.Set(); |
| 258 | + solutionManager.AfterNuGetCacheUpdated += OnAfterNuGetCacheUpdated; |
253 | 259 |
|
254 | | - // Register for the ISolutionManager.AfterNuGetCacheUpdated event via PMC PowerShell session |
255 | | - nugetConsole.Execute("Get-Event | Remove-Event"); |
256 | | - nugetConsole.Execute("$componentModel = Get-VSComponentModel"); |
257 | | - nugetConsole.Execute("$solutionManager = $componentModel.GetService([NuGet.PackageManagement.ISolutionManager])"); |
258 | | - nugetConsole.Execute("Register-ObjectEvent -InputObject $solutionManager -EventName AfterNuGetCacheUpdated -SourceIdentifier SolutionManagerCacheUpdated"); |
259 | | - |
260 | | - // Act |
261 | | - nugetConsole.InstallPackageFromPMC(packageName, packageVersion); |
262 | | - |
263 | | - // Assert - verify the cache update event was raised |
264 | | - nugetConsole.Execute("$cacheEvent = Wait-Event -SourceIdentifier SolutionManagerCacheUpdated -TimeoutSec 10"); |
265 | | - nugetConsole.Execute("Unregister-Event -SourceIdentifier SolutionManagerCacheUpdated"); |
266 | | - nugetConsole.Execute("if ($cacheEvent) { Write-Host 'CACHE_EVENT_RECEIVED' } else { Write-Host 'CACHE_EVENT_NOT_RECEIVED' }"); |
| 260 | + try |
| 261 | + { |
| 262 | + // Act |
| 263 | + var nugetConsole = GetConsole(testContext.Project); |
| 264 | + nugetConsole.InstallPackageFromPMC(packageName, packageVersion); |
267 | 265 |
|
268 | | - Assert.IsTrue( |
269 | | - nugetConsole.IsMessageFoundInPMC("CACHE_EVENT_RECEIVED"), |
270 | | - $"Cache update event should have been raised after package install. Actual PMC output: {nugetConsole.GetText()}"); |
| 266 | + // Assert |
| 267 | + Assert.IsTrue( |
| 268 | + cacheUpdatedEvent.Wait(TimeSpan.FromSeconds(10)), |
| 269 | + "Cache update event should have been raised after package install."); |
| 270 | + } |
| 271 | + finally |
| 272 | + { |
| 273 | + solutionManager.AfterNuGetCacheUpdated -= OnAfterNuGetCacheUpdated; |
| 274 | + } |
271 | 275 | } |
272 | 276 |
|
273 | 277 | // Migrated from Test-NetCoreVSandMSBuildNoOp in NetCoreProjectTest.ps1 |
|
0 commit comments