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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Non-windows platforms working #4861

Merged
merged 11 commits into from Jul 21, 2017
Expand Up @@ -13,4 +13,5 @@ In all other cases, you should define your types as classes.
NServiceBus.LogicalAddress violates the following rules:
- The following fields are reference types, which are potentially mutable:
- Field <EndpointInstance>k__BackingField of type NServiceBus.Routing.EndpointInstance is a reference type.
- The size cannot be determined because there are fields that are reference types.

72 changes: 50 additions & 22 deletions src/NServiceBus.Core.Tests/StructConventionsTests.cs
Expand Up @@ -30,34 +30,46 @@ public void ApproveStructsWhichDontFollowStructGuidelines()
");

var assembly = typeof(Endpoint).Assembly;

foreach (var type in assembly.GetTypes().OrderBy(t => t.FullName))
{
if (!type.IsValueType || type.IsEnum || type.IsSpecialName|| type.Namespace == null || !type.Namespace.StartsWith("NServiceBus") || type.FullName.Contains("__")) continue;
if (!type.IsValueType || type.IsEnum || type.IsSpecialName || type.Namespace == null || !type.Namespace.StartsWith("NServiceBus") || type.FullName.Contains("__"))
{
continue;
}

var violatedRules = new List<string> { $"{type.FullName} violates the following rules:" };

InspectSizeOfStruct(type, violatedRules);
InspectWhetherStructViolatesMutabilityRules(type, violatedRules);
InspectWhetherStructContainsPublicFields(type, violatedRules);
InspectWhetherStructContainsWritableProperties(type, violatedRules);
var containsRefereneceTypes = InspectWhetherStructContainsReferenceTypes(type, violatedRules);

if (containsRefereneceTypes)
{
violatedRules.Add(" - The size cannot be determined because there are fields that are reference types.");
}
else
{
InspectSizeOfStruct(type, violatedRules);
}

if (violatedRules.Count <= 1)
{
continue;
}

if (violatedRules.Count <= 1) continue;
foreach (var violatedRule in violatedRules)
{
approvalBuilder.AppendLine(violatedRule);
}

approvalBuilder.AppendLine();
}

Approvals.Verify(approvalBuilder.ToString());
}

static void InspectWhetherStructViolatesMutabilityRules(Type type, List<string> violatedRules)
{
InspectWhetherStructContainsReferenceTypes(type, violatedRules);
InspectWhetherStructContainsPublicFields(type, violatedRules);
InspectWhetherStructContainsWritableProperties(type, violatedRules);
}

static void InspectWhetherStructContainsReferenceTypes(Type type, List<string> violatedRules)
static bool InspectWhetherStructContainsReferenceTypes(Type type, List<string> violatedRules)
{
var mutabilityRules = new List<string> { " - The following fields are reference types, which are potentially mutable:" };

Expand All @@ -76,11 +88,17 @@ static void InspectWhetherStructContainsReferenceTypes(Type type, List<string> v
}
}

if(mutabilityRules.Count > 1)
if (mutabilityRules.Count > 1)
{
violatedRules.AddRange(mutabilityRules);

return true;
}

return false;
}

static void InspectWhetherStructContainsPublicFields(Type type, List<string> violatedRules)
static bool InspectWhetherStructContainsPublicFields(Type type, List<string> violatedRules)
{
var mutabilityRules = new List<string> { " - The following fields are public, so the type is not immutable:" };

Expand All @@ -95,10 +113,16 @@ static void InspectWhetherStructContainsPublicFields(Type type, List<string> vio
}

if (mutabilityRules.Count > 1)
{
violatedRules.AddRange(mutabilityRules);

return true;
}

return false;
}

static void InspectWhetherStructContainsWritableProperties(Type type, List<string> violatedRules)
static bool InspectWhetherStructContainsWritableProperties(Type type, List<string> violatedRules)
{
var mutabilityRules = new List<string> { " - The following properties can be written to, so the type is not immutable:" };

Expand All @@ -113,17 +137,24 @@ static void InspectWhetherStructContainsWritableProperties(Type type, List<strin
}

if (mutabilityRules.Count > 1)
{
violatedRules.AddRange(mutabilityRules);

return true;
}

return false;
}

static void InspectSizeOfStruct(Type type, List<string> violatedRules)
{
try
{
var sizeOf = Marshal.SizeOf(type);
if (IsLargerThanSixteenBytes(sizeOf))
var size = Marshal.SizeOf(type);
Copy link
Contributor

Choose a reason for hiding this comment

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

can you share some background on the Marshal.SizeOf differences across the platforms?

Copy link
Member Author

Choose a reason for hiding this comment

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

I updated the root comment to include information on the Marshal.SizeOf differences.


if (IsLargerThanSixteenBytes(size))
{
violatedRules.Add($" - The size is {sizeOf} bytes, which exceeds the recommended maximum of 16 bytes.");
violatedRules.Add($" - The size is {size} bytes, which exceeds the recommended maximum of 16 bytes.");
}
}
catch (Exception)
Expand All @@ -132,9 +163,6 @@ static void InspectSizeOfStruct(Type type, List<string> violatedRules)
}
}

static bool IsLargerThanSixteenBytes(int sizeOf)
{
return sizeOf > 16;
}
static bool IsLargerThanSixteenBytes(int size) => size > 16;
}
}