|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright file="AutoDilateMetadataTests.cs" company="Akka.NET Project"> |
| 3 | +// Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com> |
| 4 | +// Copyright (C) 2013-2026 .NET Foundation <https://github.com/akkadotnet/akka.net> |
| 5 | +// </copyright> |
| 6 | +//----------------------------------------------------------------------- |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.Linq; |
| 10 | +using System.Reflection; |
| 11 | +using Akka.TestKit.Internal; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace Akka.TestKit.Tests.TestKitBaseTests; |
| 15 | + |
| 16 | +public class AutoDilateMetadataTests |
| 17 | +{ |
| 18 | + [Fact] |
| 19 | + public void AutoDilateAttribute_should_be_parameter_only_metadata() |
| 20 | + { |
| 21 | + var usage = typeof(AutoDilateAttribute).GetCustomAttribute<AttributeUsageAttribute>(); |
| 22 | + |
| 23 | + Assert.NotNull(usage); |
| 24 | + Assert.Equal(AttributeTargets.Parameter, usage.ValidOn); |
| 25 | + Assert.False(usage.AllowMultiple); |
| 26 | + Assert.False(usage.Inherited); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void AutoDilateAttribute_should_only_mark_duration_parameters() |
| 31 | + { |
| 32 | + var annotatedParameters = typeof(TestKitBase).Assembly |
| 33 | + .GetTypes() |
| 34 | + .SelectMany(type => type |
| 35 | + .GetMembers(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | |
| 36 | + BindingFlags.NonPublic) |
| 37 | + .OfType<MethodBase>()) |
| 38 | + .SelectMany(method => method.GetParameters()) |
| 39 | + .Where(HasAutoDilateAttribute) |
| 40 | + .ToArray(); |
| 41 | + |
| 42 | + Assert.NotEmpty(annotatedParameters); |
| 43 | + Assert.All(annotatedParameters, parameter => |
| 44 | + Assert.True( |
| 45 | + parameter.ParameterType == typeof(TimeSpan) || |
| 46 | + parameter.ParameterType == typeof(TimeSpan?), |
| 47 | + $"{parameter.Member.DeclaringType?.FullName}.{parameter.Member.Name} parameter " + |
| 48 | + $"'{parameter.Name}' has unexpected type {parameter.ParameterType}.")); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public void Automatically_dilated_TestKit_parameters_should_be_marked() |
| 53 | + { |
| 54 | + AssertAutoDilated(typeof(TestKitBase), nameof(TestKitBase.Dilated), "duration", "duration"); |
| 55 | + AssertAutoDilated(typeof(TestKitBase), nameof(TestKitBase.RemainingOrDilated), "duration", "duration"); |
| 56 | + AssertAutoDilated(typeof(TestKitBase), nameof(TestKitBase.AwaitAssert), "duration", |
| 57 | + "assertion", "duration", "interval", "cancellationToken"); |
| 58 | + AssertAutoDilated(typeof(TestKitBase), nameof(TestKitBase.ReceiveWhile), "max", |
| 59 | + "filter", "max", "idle", "msgs", "cancellationToken"); |
| 60 | + AssertAutoDilated(typeof(TestKitBase), nameof(TestKitBase.Within), "max", |
| 61 | + "min", "max", "action", "hint", "epsilonValue", "cancellationToken"); |
| 62 | + AssertAutoDilated(typeof(TestKitBase), nameof(TestKitBase.FishUntilMessageAsync), "max", |
| 63 | + "max", "cancellationToken"); |
| 64 | + AssertAutoDilated(typeof(IEventFilterApplier), nameof(IEventFilterApplier.ExpectOne), "timeout", |
| 65 | + "timeout", "action", "cancellationToken"); |
| 66 | + AssertAutoDilated(typeof(InternalEventFilterApplier), nameof(InternalEventFilterApplier.ExpectOne), |
| 67 | + "timeout", "timeout", "action", "cancellationToken"); |
| 68 | + AssertAutoDilated(typeof(TestBarrier), nameof(TestBarrier.Await), "timeout", "timeout"); |
| 69 | + |
| 70 | + var constructor = typeof(TestBarrier).GetConstructors() |
| 71 | + .Single(ctor => HasParameterNames(ctor, "testKit", "count", "defaultTimeout")); |
| 72 | + Assert.True(HasAutoDilateAttribute(constructor.GetParameters().Single(p => p.Name == "defaultTimeout"))); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void Raw_or_conditionally_dilated_TestKit_parameters_should_not_be_marked() |
| 77 | + { |
| 78 | + AssertNotAutoDilated(typeof(TestKitBase), nameof(TestKitBase.AwaitAssert), "interval", |
| 79 | + "assertion", "duration", "interval", "cancellationToken"); |
| 80 | + AssertNotAutoDilated(typeof(TestKitBase), nameof(TestKitBase.ReceiveWhile), "idle", |
| 81 | + "filter", "max", "idle", "msgs", "cancellationToken"); |
| 82 | + AssertNotAutoDilated(typeof(TestKitBase), nameof(TestKitBase.Within), "min", |
| 83 | + "min", "max", "action", "hint", "epsilonValue", "cancellationToken"); |
| 84 | + AssertNotAutoDilated(typeof(TestKitBase), nameof(TestKitBase.Within), "epsilonValue", |
| 85 | + "min", "max", "action", "hint", "epsilonValue", "cancellationToken"); |
| 86 | + AssertNotAutoDilated(typeof(TestKitBase), nameof(TestKitBase.ReceiveOne), "max", |
| 87 | + "max", "cancellationToken"); |
| 88 | + AssertNotAutoDilated(typeof(TestKitBase), nameof(TestKitBase.AwaitConditionNoThrow), "max", |
| 89 | + "conditionIsFulfilled", "max", "interval", "cancellationToken"); |
| 90 | + AssertNotAutoDilated(typeof(TestLatch), nameof(TestLatch.Ready), "timeout", "timeout"); |
| 91 | + } |
| 92 | + |
| 93 | + private static void AssertAutoDilated( |
| 94 | + Type declaringType, |
| 95 | + string methodName, |
| 96 | + string parameterName, |
| 97 | + params string[] parameterNames) |
| 98 | + { |
| 99 | + var parameter = FindParameter(declaringType, methodName, parameterName, parameterNames); |
| 100 | + Assert.True(HasAutoDilateAttribute(parameter)); |
| 101 | + } |
| 102 | + |
| 103 | + private static void AssertNotAutoDilated( |
| 104 | + Type declaringType, |
| 105 | + string methodName, |
| 106 | + string parameterName, |
| 107 | + params string[] parameterNames) |
| 108 | + { |
| 109 | + var parameter = FindParameter(declaringType, methodName, parameterName, parameterNames); |
| 110 | + Assert.False(HasAutoDilateAttribute(parameter)); |
| 111 | + } |
| 112 | + |
| 113 | + private static ParameterInfo FindParameter( |
| 114 | + Type declaringType, |
| 115 | + string methodName, |
| 116 | + string parameterName, |
| 117 | + params string[] parameterNames) |
| 118 | + { |
| 119 | + var method = declaringType |
| 120 | + .GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | |
| 121 | + BindingFlags.NonPublic) |
| 122 | + .Single(candidate => |
| 123 | + candidate.Name == methodName && |
| 124 | + HasParameterNames(candidate, parameterNames)); |
| 125 | + |
| 126 | + return method.GetParameters().Single(parameter => parameter.Name == parameterName); |
| 127 | + } |
| 128 | + |
| 129 | + private static bool HasParameterNames(MethodBase method, params string[] parameterNames) |
| 130 | + => method.GetParameters().Select(parameter => parameter.Name).SequenceEqual(parameterNames); |
| 131 | + |
| 132 | + private static bool HasAutoDilateAttribute(ParameterInfo parameter) |
| 133 | + => parameter.IsDefined(typeof(AutoDilateAttribute), inherit: false); |
| 134 | +} |
0 commit comments