TestConfigurationSetGroupProcessor.cs (4701B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="TestConfigurationSetGroupProcessor.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.Management.Configuration.UnitTests.Helpers 8 { 9 using System; 10 using System.Collections.Generic; 11 using System.Diagnostics; 12 using System.Runtime.InteropServices.WindowsRuntime; 13 using System.Threading; 14 using System.Threading.Tasks; 15 using Windows.Foundation; 16 using Windows.Foundation.Collections; 17 18 /// <summary> 19 /// A test implementation of IConfigurationGroupProcessor. 20 /// </summary> 21 internal partial class TestConfigurationSetGroupProcessor : TestConfigurationSetProcessor, IConfigurationGroupProcessor 22 { 23 /// <summary> 24 /// The event that is waited on before actually processing the async operations. 25 /// </summary> 26 private AutoResetEvent asyncWaitEvent = new AutoResetEvent(false); 27 28 /// <summary> 29 /// Initializes a new instance of the <see cref="TestConfigurationSetGroupProcessor"/> class. 30 /// </summary> 31 /// <param name="set">The set that this processor is for.</param> 32 internal TestConfigurationSetGroupProcessor(ConfigurationSet? set) 33 : base(set) 34 { 35 } 36 37 /// <summary> 38 /// Gets the group that this processor targets. 39 /// </summary> 40 public object? Group 41 { 42 get { return this.Set; } 43 } 44 45 /// <summary> 46 /// Gets or sets a value indicating whether the async methods should wait on an event before processing. 47 /// </summary> 48 internal bool ShouldWaitOnAsyncEvent { get; set; } = false; 49 50 /// <summary> 51 /// Apply settings for the group. 52 /// </summary> 53 /// <param name="progressHandler">The progress handler.</param> 54 /// <returns>The operation to apply settings.</returns> 55 public IAsyncOperation<IApplyGroupSettingsResult> ApplyGroupSettingsAsync(EventHandler<IApplyGroupMemberSettingsResult> progressHandler) 56 { 57 return AsyncInfo.Run((CancellationToken cancellationToken) => Task.Run<IApplyGroupSettingsResult>(() => 58 { 59 this.WaitOnAsyncEvent(cancellationToken); 60 61 ApplyGroupSettingsResultInstance result = new (this.Group); 62 result.UnitResults = new List<IApplyGroupMemberSettingsResult>(); 63 64 if (this.Set != null) 65 { 66 TestConfigurationUnitGroupProcessor.ApplyGroupSettings(this.Set.Units, progressHandler, result); 67 } 68 69 return result; 70 })); 71 } 72 73 /// <summary> 74 /// Test settings for the group. 75 /// </summary> 76 /// <param name="progressHandler">The progress handler.</param> 77 /// <returns>The operation to test settings.</returns> 78 public IAsyncOperation<ITestGroupSettingsResult> TestGroupSettingsAsync(EventHandler<ITestSettingsResult> progressHandler) 79 { 80 return AsyncInfo.Run((CancellationToken cancellationToken) => Task.Run<ITestGroupSettingsResult>(() => 81 { 82 this.WaitOnAsyncEvent(cancellationToken); 83 84 TestGroupSettingsResultInstance result = new (this.Group); 85 result.UnitResults = new List<ITestSettingsResult>(); 86 87 if (this.Set != null) 88 { 89 result.TestResult = TestConfigurationUnitGroupProcessor.GetTestResult(this.Set.Metadata); 90 TestConfigurationUnitGroupProcessor.TestGroupSettings(this.Set.Units, progressHandler, result); 91 } 92 93 return result; 94 })); 95 } 96 97 /// <summary> 98 /// Signals the async event. 99 /// </summary> 100 internal void SignalAsyncEvent() 101 { 102 this.asyncWaitEvent.Set(); 103 } 104 105 /// <summary> 106 /// Waits on the async event. 107 /// </summary> 108 private void WaitOnAsyncEvent(CancellationToken cancellationToken) 109 { 110 if (this.ShouldWaitOnAsyncEvent) 111 { 112 cancellationToken.Register(() => this.asyncWaitEvent.Set()); 113 if (!this.asyncWaitEvent.WaitOne(10000)) 114 { 115 throw new TimeoutException(); 116 } 117 } 118 } 119 } 120 }