public async Task Queue_ProcessesNotifications_AndRestarts() { // Arrange var projectManager = new TestProjectSnapshotManager(Dispatcher, Workspace); projectManager.HostProjectAdded(HostProject1); projectManager.HostProjectAdded(HostProject2); projectManager.WorkspaceProjectAdded(WorkspaceProject1); projectManager.WorkspaceProjectAdded(WorkspaceProject2); projectManager.DocumentAdded(HostProject1, Documents[0], null); projectManager.DocumentAdded(HostProject1, Documents[1], null); var project = projectManager.GetLoadedProject(HostProject1.FilePath); var queue = new BackgroundDocumentGenerator(Dispatcher) { Delay = TimeSpan.FromMilliseconds(1), BlockBackgroundWorkStart = new ManualResetEventSlim(initialState: false), NotifyBackgroundWorkStarting = new ManualResetEventSlim(initialState: false), BlockBackgroundWorkCompleting = new ManualResetEventSlim(initialState: false), NotifyBackgroundWorkCompleted = new ManualResetEventSlim(initialState: false), }; // Act & Assert queue.Enqueue(project, project.GetDocument(Documents[0].FilePath)); Assert.True(queue.IsScheduledOrRunning, "Queue should be scheduled during Enqueue"); Assert.True(queue.HasPendingNotifications, "Queue should have a notification created during Enqueue"); // Allow the background work to start. queue.BlockBackgroundWorkStart.Set(); await Task.Run(() => queue.NotifyBackgroundWorkStarting.Wait(TimeSpan.FromSeconds(1))); Assert.True(queue.IsScheduledOrRunning, "Worker should be processing now"); Assert.False(queue.HasPendingNotifications, "Worker should have taken all notifications"); queue.Enqueue(project, project.GetDocument(Documents[1].FilePath)); Assert.True(queue.HasPendingNotifications); // Now we should see the worker restart when it finishes. // Allow work to complete, which should restart the timer. queue.BlockBackgroundWorkCompleting.Set(); await Task.Run(() => queue.NotifyBackgroundWorkCompleted.Wait(TimeSpan.FromSeconds(1))); queue.NotifyBackgroundWorkCompleted.Reset(); // It should start running again right away. Assert.True(queue.IsScheduledOrRunning, "Queue should be scheduled during Enqueue"); Assert.True(queue.HasPendingNotifications, "Queue should have a notification created during Enqueue"); // Allow the background work to proceed. queue.BlockBackgroundWorkStart.Set(); queue.BlockBackgroundWorkCompleting.Set(); await Task.Run(() => queue.NotifyBackgroundWorkCompleted.Wait(TimeSpan.FromSeconds(1))); Assert.False(queue.IsScheduledOrRunning, "Queue should not have restarted"); Assert.False(queue.HasPendingNotifications, "Queue should have processed all notifications"); }
public async Task Queue_ProcessesNotifications_AndGoesBackToSleep() { // Arrange var projectManager = new TestProjectSnapshotManager(Dispatcher, Workspace); projectManager.HostProjectAdded(HostProject1); projectManager.HostProjectAdded(HostProject2); projectManager.WorkspaceProjectAdded(WorkspaceProject1); projectManager.WorkspaceProjectAdded(WorkspaceProject2); var projectWorker = new TestProjectSnapshotWorker(); var queue = new ProjectSnapshotWorkerQueue(Dispatcher, projectManager, projectWorker) { Delay = TimeSpan.FromMilliseconds(1), BlockBackgroundWorkStart = new ManualResetEventSlim(initialState: false), NotifyBackgroundWorkFinish = new ManualResetEventSlim(initialState: false), NotifyForegroundWorkFinish = new ManualResetEventSlim(initialState: false), }; // Act & Assert queue.Enqueue(projectManager.GetSnapshot(HostProject1).CreateUpdateContext()); Assert.True(queue.IsScheduledOrRunning, "Queue should be scheduled during Enqueue"); Assert.True(queue.HasPendingNotifications, "Queue should have a notification created during Enqueue"); // Allow the background work to proceed. queue.BlockBackgroundWorkStart.Set(); // Get off the foreground thread and allow the updates to flow through. await Task.Run(() => queue.NotifyForegroundWorkFinish.Wait(TimeSpan.FromSeconds(1))); Assert.False(queue.IsScheduledOrRunning, "Queue should not have restarted"); Assert.False(queue.HasPendingNotifications, "Queue should have processed all notifications"); }
public async Task Queue_ProcessesNotifications_AndGoesBackToSleep() { // Arrange var projectManager = new TestProjectSnapshotManager(Dispatcher, Workspace); projectManager.HostProjectAdded(HostProject1); projectManager.HostProjectAdded(HostProject2); projectManager.WorkspaceProjectAdded(WorkspaceProject1); projectManager.WorkspaceProjectAdded(WorkspaceProject2); projectManager.DocumentAdded(HostProject1, Documents[0], null); projectManager.DocumentAdded(HostProject1, Documents[1], null); var project = projectManager.GetLoadedProject(HostProject1.FilePath); var queue = new BackgroundDocumentGenerator(Dispatcher, DynamicFileInfoProvider) { Delay = TimeSpan.FromMilliseconds(1), BlockBackgroundWorkStart = new ManualResetEventSlim(initialState: false), NotifyBackgroundWorkStarting = new ManualResetEventSlim(initialState: false), BlockBackgroundWorkCompleting = new ManualResetEventSlim(initialState: false), NotifyBackgroundWorkCompleted = new ManualResetEventSlim(initialState: false), }; queue.Initialize(projectManager); // Act & Assert queue.Enqueue(project, project.GetDocument(Documents[0].FilePath)); Assert.True(queue.IsScheduledOrRunning, "Queue should be scheduled during Enqueue"); Assert.True(queue.HasPendingNotifications, "Queue should have a notification created during Enqueue"); // Allow the background work to proceed. queue.BlockBackgroundWorkStart.Set(); queue.BlockBackgroundWorkCompleting.Set(); await Task.Run(() => queue.NotifyBackgroundWorkCompleted.Wait(TimeSpan.FromSeconds(3))); Assert.False(queue.IsScheduledOrRunning, "Queue should not have restarted"); Assert.False(queue.HasPendingNotifications, "Queue should have processed all notifications"); }