public void CancelPendingUpdates_PendingTasksAreCanceled() { var scheduler = new TaskDelayScheduler(TimeSpan.FromMilliseconds(50), new IProjectThreadingServiceMock(), CancellationToken.None); bool taskRan = false; var task1 = scheduler.ScheduleAsyncTask((ct) => { taskRan = true; int count = 50; while (count != 0) { ct.ThrowIfCancellationRequested(); Thread.Sleep(20); --count; } return(Task.CompletedTask); }); scheduler.CancelPendingUpdates(); try { task1.Task.Wait(); Assert.False(taskRan); } catch (OperationCanceledException) { } }
private void WatchLaunchSettingsFile() { if (FileWatcher == null) { FileChangeScheduler?.Dispose(); // Create our scheduler for processing file changes FileChangeScheduler = new TaskDelayScheduler(FileChangeProcessingDelay, _commonProjectServices.ThreadingService, _projectServices.ProjectAsynchronousTasks.UnloadCancellationToken); try { FileWatcher = new SimpleFileWatcher(Path.GetDirectoryName(_commonProjectServices.Project.FullPath), true, NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite, LaunchSettingsFilename, LaunchSettingsFile_Changed, LaunchSettingsFile_Changed); } catch (Exception ex) when(ex is IOException || ex is ArgumentException) { // If the project folder is no longer available this will throw, which can happen during branch switching } } }
public void ScheduleAsyncTask_CancelsExistingTasks() { var scheduler = new TaskDelayScheduler(TimeSpan.FromMilliseconds(50), new IProjectThreadingServiceMock(), CancellationToken.None); var tasksRun = new bool[3]; var task1 = scheduler.ScheduleAsyncTask((ct) => { tasksRun[0] = true; return(Task.CompletedTask); }); var task2 = scheduler.ScheduleAsyncTask((ct) => { tasksRun[1] = true; return(Task.CompletedTask); }); var task3 = scheduler.ScheduleAsyncTask((ct) => { tasksRun[2] = true; return(Task.CompletedTask); }); task1.Task.Wait(); task2.Task.Wait(); task3.Task.Wait(); Assert.False(tasksRun[0]); Assert.False(tasksRun[1]); Assert.True(tasksRun[2]); }
public void Dispose_ClearsPendingTasks() { var scheduler = new TaskDelayScheduler(TimeSpan.FromMilliseconds(50), new IProjectThreadingServiceMock(), CancellationToken.None); bool taskRan = false; var task1 = scheduler.ScheduleAsyncTask((ct) => { taskRan = true; int count = 50; while (count != 0) { ct.ThrowIfCancellationRequested(); Thread.Sleep(20); --count; } return(Task.CompletedTask); }); scheduler.Dispose(); // There are two cases to consider in the verification. One, the cancellation is detected by the scheduler. In that case // the task will complete successfully (it does not cancel itself), but the inner async task will not run. And 2, the cancellation // occurs after the scheduling starts. In that case the inner task will execute, but is crafted to throw and OperationCancelledException. try { task1.Task.Wait(); Assert.False(taskRan); } catch (OperationCanceledException) { } }
public DependencySubscriptionsHost( IUnconfiguredProjectCommonServices commonServices, Lazy <IAggregateCrossTargetProjectContextProvider> contextProvider, [Import(ExportContractNames.Scopes.UnconfiguredProject)] IProjectAsynchronousTasksService tasksService, IActiveConfiguredProjectSubscriptionService activeConfiguredProjectSubscriptionService, IActiveProjectConfigurationRefreshService activeProjectConfigurationRefreshService, ITargetFrameworkProvider targetFrameworkProvider, IAggregateDependenciesSnapshotProvider aggregateSnapshotProvider) : base(commonServices, contextProvider, tasksService, activeConfiguredProjectSubscriptionService, activeProjectConfigurationRefreshService) { CommonServices = commonServices; DependencySubscribers = new OrderPrecedenceImportCollection <IDependencyCrossTargetSubscriber>( projectCapabilityCheckProvider: commonServices.Project); SnapshotFilters = new OrderPrecedenceImportCollection <IDependenciesSnapshotFilter>( projectCapabilityCheckProvider: commonServices.Project, orderingStyle: ImportOrderPrecedenceComparer.PreferenceOrder.PreferredComesLast); SubTreeProviders = new OrderPrecedenceImportCollection <IProjectDependenciesSubTreeProvider>( ImportOrderPrecedenceComparer.PreferenceOrder.PreferredComesLast, projectCapabilityCheckProvider: commonServices.Project); DependenciesUpdateScheduler = new TaskDelayScheduler( _dependenciesUpdateThrottleInterval, commonServices.ThreadingService, tasksService.UnloadCancellationToken); TargetFrameworkProvider = targetFrameworkProvider; AggregateSnapshotProvider = aggregateSnapshotProvider; ProjectFilePath = CommonServices.Project.FullPath; }
// ECan pass null for all and a default will be crewated public LaunchSettingsUnderTest(UnconfiguredProject project, IUnconfiguredProjectServices projectServices, IFileSystem fileSystem, IUnconfiguredProjectCommonServices commonProjectServices, IActiveConfiguredProjectSubscriptionService projectSubscriptionService, ActiveConfiguredProject <AppDesignerFolderSpecialFileProvider> appDesignerFolderSpecialFileProvider) : base(project, projectServices, fileSystem, commonProjectServices, projectSubscriptionService, appDesignerFolderSpecialFileProvider) { // Block the code from setting up one on the real file system. Since we block, it we need to set up the fileChange scheduler manually FileWatcher = new SimpleFileWatcher(); // Make the unit tests run faster FileChangeProcessingDelay = TimeSpan.FromMilliseconds(50); FileChangeScheduler = new TaskDelayScheduler(FileChangeProcessingDelay, commonProjectServices.ThreadingService, CancellationToken.None); }
public void ScheduleAsyncTask_RunsAsyncMethod() { var scheduler = new TaskDelayScheduler(TimeSpan.FromMilliseconds(10), new IProjectThreadingServiceMock(), CancellationToken.None); bool taskRan = false; var task = scheduler.ScheduleAsyncTask((ct) => { taskRan = true; return(Task.CompletedTask); }); task.Task.Wait(); Assert.True(taskRan); }
public void CancelPendingUpdates_PendingTasksAreCanceled() { var scheduler = new TaskDelayScheduler(TimeSpan.FromMilliseconds(50), new IProjectThreadingServiceMock(), CancellationToken.None); bool taskRan = false; var task1 = scheduler.ScheduleAsyncTask((ct) => { taskRan = true; return(Task.CompletedTask); }); scheduler.CancelPendingUpdates(); task1.Task.Wait(); Assert.False(taskRan); }
/// <summary> /// Sets up a file system watcher to look for changes to the launchsettings.json file. It watches at the root of the /// project oltherwise we force the project to have a properties folder. /// </summary> private void WatchLaunchSettingsFile() { if (FileWatcher == null) { // Create our scheduler for processing file chagnes FileChangeScheduler = new TaskDelayScheduler(FileChangeProcessingDelay, CommonProjectServices.ThreadingService, ProjectServices.ProjectAsynchronousTasks.UnloadCancellationToken); FileWatcher = new SimpleFileWatcher(Path.GetDirectoryName(CommonProjectServices.Project.FullPath), true, NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite, LaunchSettingsFilename, LaunchSettingsFile_Changed, LaunchSettingsFile_Changed); } }
public DesignTimeInputsCompiler(UnconfiguredProject project, IActiveWorkspaceProjectContextHost activeWorkspaceProjectContextHost, IProjectThreadingService threadingService, IDesignTimeInputsChangeTracker changeTracker, ITempPECompiler compiler, IFileSystem fileSystem, ITelemetryService telemetryService) : base(threadingService.JoinableTaskContext) { _project = project; _activeWorkspaceProjectContextHost = activeWorkspaceProjectContextHost; _threadingService = threadingService; _changeTracker = changeTracker; _compiler = compiler; _fileSystem = fileSystem; _telemetryService = telemetryService; _scheduler = new TaskDelayScheduler(s_compilationDelayTime, threadingService, CancellationToken.None); }
/// <summary> /// Handles one time initialization /// </summary> protected override async Task InitializeCoreAsync(CancellationToken cancellationToken) { ReloadDelayScheduler = new TaskDelayScheduler(TimeSpan.FromMilliseconds(ReloadDelay), _threadHandling, CancellationToken.None); await ConnectToSolutionEvents().ConfigureAwait(false); }