|
| 1 | +// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Security.Cryptography; |
| 5 | +using System.Text; |
| 6 | +using NUnit.Framework.Interfaces; |
| 7 | + |
| 8 | +namespace NUnit.Framework.Internal.Filters |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// PartitionFilter filter matches a subset of tests based upon a chosen partition number and partition count |
| 12 | + /// |
| 13 | + /// This is helpful when you may want to run a subset of tests (eg, across 3 machines - or partitions), each with a separately assigned partition number and fixed partition count |
| 14 | + /// </summary> |
| 15 | + internal sealed class PartitionFilter : TestFilter |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// The matching partition number (between 1 and Partition Count, inclusive) this filter should match on |
| 19 | + /// </summary> |
| 20 | + public uint PartitionNumber { get; private set; } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// The number of partitions available to use when assigning a matching partition number for each test this filter should match on |
| 24 | + /// </summary> |
| 25 | + public uint PartitionCount { get; private set; } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Construct a PartitionFilter that matches tests that have the assigned partition number from the total partition count |
| 29 | + /// </summary> |
| 30 | + /// <param name="partitionNumber">The partition number this filter will recognize and match on.</param> |
| 31 | + /// <param name="partitionCount">The total number of partitions that should be configured when assigning each test to a partition number.</param> |
| 32 | + public PartitionFilter(uint partitionNumber, uint partitionCount) |
| 33 | + { |
| 34 | + PartitionNumber = partitionNumber; |
| 35 | + PartitionCount = partitionCount; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Create a new PartitionFilter from the provided string value, or return false if the value could not be parsed |
| 40 | + /// </summary> |
| 41 | + /// <param name="value">The partition value (eg, 1/10 to indicate partition 1 of 10)</param> |
| 42 | + /// <param name="partitionFilter">The created PartitionFilter if the parsing succeeded</param> |
| 43 | + /// <returns>True on successful parsing, or False if there is an error</returns> |
| 44 | + public static bool TryCreate(string value, out PartitionFilter partitionFilter) |
| 45 | + { |
| 46 | + // Split our numberWithCount into two parts, such that "1/10" becomes PartitionNumber 1, PartitionCount 10 |
| 47 | + string[] parts = value.Split('/'); |
| 48 | + |
| 49 | + // Parts must be exactly 2, and be in the format of "number/count" |
| 50 | + if (parts.Length == 2 && uint.TryParse(parts[0], out uint number) && uint.TryParse(parts[1], out uint count)) { |
| 51 | + // Number must be between 1 and Count, inclusive |
| 52 | + // Return a new PartitionFilter with the parsed values |
| 53 | + if (number >= 1 && number <= count) |
| 54 | + { |
| 55 | + partitionFilter = new PartitionFilter(number, count); |
| 56 | + return true; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Could not parse partition information |
| 61 | + partitionFilter = null; |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Match a test against a single value. |
| 67 | + /// </summary> |
| 68 | + public override bool Match(ITest test) |
| 69 | + { |
| 70 | + // Do not match a test Suite, only match individual tests |
| 71 | + if (test.IsSuite) |
| 72 | + return false; |
| 73 | + |
| 74 | + // Calculate the partition number for the provided Test |
| 75 | + var partitionForTest = ComputePartitionNumber(test); |
| 76 | + |
| 77 | + // Return a match if the calculated partition number matches our configured Partition Number |
| 78 | + return partitionForTest == PartitionNumber; |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Adds a PartitionFilter XML node to the provided parentNode |
| 83 | + /// </summary> |
| 84 | + /// <param name="parentNode">Parent node</param> |
| 85 | + /// <param name="recursive">True if recursive</param> |
| 86 | + /// <returns>The added XML node</returns> |
| 87 | + public override TNode AddToXml(TNode parentNode, bool recursive) |
| 88 | + { |
| 89 | + return parentNode.AddElement("partition", $"{PartitionNumber}/{PartitionCount}"); |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Computes the Partition Number that has been assigned to the provided ITest value (based upon the configured Partition Count) |
| 94 | + /// </summary> |
| 95 | + /// <param name="value">A partition value between 1 and PartitionCount, inclusive</param> |
| 96 | + /// <returns>A partition value between 1 and PartitionCount, inclusive</returns> |
| 97 | + public uint ComputePartitionNumber(ITest value) |
| 98 | + { |
| 99 | + return ComputeHashValue(value.FullName) % PartitionCount + 1; |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Computes an unsigned integer hash value based upon the provided string |
| 104 | + /// </summary> |
| 105 | + private static uint ComputeHashValue(string name) |
| 106 | + { |
| 107 | + using var hashAlgorithm = SHA256.Create(); |
| 108 | + |
| 109 | + // SHA256 ComputeHash will return 32 bytes, we will use the first 4 bytes of that to convert to an unsigned integer |
| 110 | + var hashValue = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(name)); |
| 111 | + |
| 112 | + return BitConverter.ToUInt32(hashValue, 0); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments