Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace reflection magic in MNTR with reading of MultiNodeConfig properties #4902

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't believe we need the property checking anymore using reflection - a full MNTR run (which this PR will trigger) will show up with many fewer tests and much lower execution time if this change made several tests suddenly stop showing up.

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made this property public so the data is just available to anyone who needs it - preferred this over friend assembly magic.

{
get { return _roles; }
}
Expand Down