From 156f9f59059ec008a710881d7eb22f7eacb708a0 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Mon, 5 Apr 2021 21:40:03 -0500 Subject: [PATCH 1/2] close #4901 - replace reflection magic in MNTR with reading of MultiNodeConfig properties --- src/core/Akka.MultiNodeTestRunner/Discovery.cs | 10 ++-------- src/core/Akka.Remote.TestKit/MultiNodeSpec.cs | 2 +- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/core/Akka.MultiNodeTestRunner/Discovery.cs b/src/core/Akka.MultiNodeTestRunner/Discovery.cs index aa77bb53ff3..8d6a88cc339 100644 --- a/src/core/Akka.MultiNodeTestRunner/Discovery.cs +++ b/src/core/Akka.MultiNodeTestRunner/Discovery.cs @@ -108,14 +108,8 @@ private IEnumerable RoleNames(Type specType) var ctorWithConfig = FindConfigConstructor(specType); var configType = ctorWithConfig.GetParameters().First().ParameterType; var args = ConfigConstructorParamValues(configType); - var configInstance = Activator.CreateInstance(configType, args); - var roleType = typeof(RoleName); - var configProps = configType.GetProperties(BindingFlags.Instance | BindingFlags.Public); - var roleProps = configProps.Where(p => p.PropertyType == roleType && p.Name != "Myself").Select(p => (RoleName)p.GetValue(configInstance)); - var configFields = configType.GetFields(BindingFlags.Instance | BindingFlags.Public); - var roleFields = configFields.Where(f => f.FieldType == roleType && f.Name != "Myself").Select(f => (RoleName)f.GetValue(configInstance)); - var roles = roleProps.Concat(roleFields).Distinct(); - return roles; + var configInstance = (MultiNodeConfig)Activator.CreateInstance(configType, args); + return configInstance.Roles; } internal static ConstructorInfo FindConfigConstructor(Type configUser) diff --git a/src/core/Akka.Remote.TestKit/MultiNodeSpec.cs b/src/core/Akka.Remote.TestKit/MultiNodeSpec.cs index e13e484da6d..7314b6bd259 100644 --- a/src/core/Akka.Remote.TestKit/MultiNodeSpec.cs +++ b/src/core/Akka.Remote.TestKit/MultiNodeSpec.cs @@ -168,7 +168,7 @@ internal ImmutableList Deployments(RoleName node) return deployments == null ? _allDeploy : deployments.AddRange(_allDeploy); } - internal ImmutableList Roles + public ImmutableList Roles { get { return _roles; } } From f0a55ee689f14fc22a1c488471559ba96751840f Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Mon, 5 Apr 2021 23:05:02 -0500 Subject: [PATCH 2/2] fixed outdated DiscoverySpec --- .../DiscoveryCases.cs | 25 +++++++++++++++++++ .../DiscoverySpec.cs | 6 ++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/core/Akka.MultiNodeTestRunner.Shared.Tests/MultiNodeTestRunnerDiscovery/DiscoveryCases.cs b/src/core/Akka.MultiNodeTestRunner.Shared.Tests/MultiNodeTestRunnerDiscovery/DiscoveryCases.cs index e9954d7a39d..950b3beb88e 100644 --- a/src/core/Akka.MultiNodeTestRunner.Shared.Tests/MultiNodeTestRunnerDiscovery/DiscoveryCases.cs +++ b/src/core/Akka.MultiNodeTestRunner.Shared.Tests/MultiNodeTestRunnerDiscovery/DiscoveryCases.cs @@ -5,6 +5,7 @@ // //----------------------------------------------------------------------- +using System.Linq; using Akka.Remote.TestKit; namespace Akka.MultiNodeTestRunner.Shared.Tests.MultiNodeTestRunnerDiscovery @@ -133,6 +134,30 @@ public FloodyChildSpec3(FloodyConfig config) : base(config) } } + public class NoReflectionConfig : MultiNodeConfig + { + public NoReflectionConfig() + { + foreach(var i in Enumerable.Range(1, 10)) + { + Role("node-" + i); + } + } + } + + public class NoReflectionSpec + { + public NoReflectionSpec(NoReflectionConfig config) + { + + } + + [MultiNodeFact(Skip = "Only for discovery tests")] + public void Dummy() + { + } + } + public class DiverseConfig : MultiNodeConfig { public RoleName RoleProp { get; set; } diff --git a/src/core/Akka.MultiNodeTestRunner.Shared.Tests/MultiNodeTestRunnerDiscovery/DiscoverySpec.cs b/src/core/Akka.MultiNodeTestRunner.Shared.Tests/MultiNodeTestRunnerDiscovery/DiscoverySpec.cs index 769428afdc3..8aa0c32d700 100644 --- a/src/core/Akka.MultiNodeTestRunner.Shared.Tests/MultiNodeTestRunnerDiscovery/DiscoverySpec.cs +++ b/src/core/Akka.MultiNodeTestRunner.Shared.Tests/MultiNodeTestRunnerDiscovery/DiscoverySpec.cs @@ -53,11 +53,11 @@ public void Discovered_count_equals_number_of_roles_mult_specs() Assert.Equal(5, discoveredSpecs[KeyFromSpecName(nameof(DiscoveryCases.FloodyChildSpec3))].Count); } - [Fact(DisplayName = "Only public props and fields are considered when looking for RoleNames")] - public void Public_props_and_fields_are_considered() + [Fact(DisplayName = "Only the MultiNodeConfig.Roles property is used to compute the number of Roles in MultiNodeFact")] + public void Only_MultiNodeConfig_role_count_used() { var discoveredSpecs = DiscoverSpecs(); - Assert.Equal(discoveredSpecs[KeyFromSpecName(nameof(DiscoveryCases.DiverseSpec))].Select(c => c.Role), new[] {"RoleProp", "RoleField"}); + Assert.Equal(10, discoveredSpecs[KeyFromSpecName(nameof(DiscoveryCases.NoReflectionSpec))].Select(c => c.Role).Count()); } private static Dictionary> DiscoverSpecs()