Skip to content

Commit

Permalink
replace reflection magic in MNTR with reading of MultiNodeConfig pr…
Browse files Browse the repository at this point in the history
…operties (#4902)

* close #4901 - replace reflection magic in MNTR with reading of MultiNodeConfig properties

* fixed outdated DiscoverySpec
  • Loading branch information
Aaronontheweb authored Apr 6, 2021
1 parent 4091508 commit e7b3fb2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// </copyright>
//-----------------------------------------------------------------------

using System.Linq;
using Akka.Remote.TestKit;

namespace Akka.MultiNodeTestRunner.Shared.Tests.MultiNodeTestRunnerDiscovery
Expand Down Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, List<NodeTest>> DiscoverSpecs()
Expand Down
10 changes: 2 additions & 8 deletions src/core/Akka.MultiNodeTestRunner/Discovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,8 @@ private IEnumerable<RoleName> 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)
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Remote.TestKit/MultiNodeSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ internal ImmutableList<string> Deployments(RoleName node)
return deployments == null ? _allDeploy : deployments.AddRange(_allDeploy);
}

internal ImmutableList<RoleName> Roles
public ImmutableList<RoleName> Roles
{
get { return _roles; }
}
Expand Down

0 comments on commit e7b3fb2

Please sign in to comment.