-
Notifications
You must be signed in to change notification settings - Fork 647
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AssemblyScanner doesn't scan message assemblies that reference Messag…
…e Interfaces (#7081) (#7089) * Add a test to verify the messages referencing core are scanned * Failing test for messages that reference message interfaces * Unify in one test due avoid assembly loading issues * Cleanup * AssemblyScanner should scan assemblies that reference the message interfaces assembly to make sure messages using those interfaces can be discovered and do not act like unobtrusive messages * Extract into method with a huge comment and inline hints * Add a type forwarding test as a safety net * Update src/NServiceBus.Core.Tests/AssemblyScanner/When_using_type_forwarding.cs Co-authored-by: Phil Bastian <[email protected]> * Apply suggestions from code review Co-authored-by: Brandon Ording <[email protected]> * string.Equals Co-authored-by: Brandon Ording <[email protected]> --------- Co-authored-by: danielmarbach <[email protected]> Co-authored-by: Phil Bastian <[email protected]> Co-authored-by: Brandon Ording <[email protected]> (cherry picked from commit 3a7c74c)
- Loading branch information
1 parent
37ad2ee
commit 71fe44b
Showing
6 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...AssemblyScanner/When_directory_with_messages_referencing_core_or_interfaces_is_scanned.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace NServiceBus.Core.Tests.AssemblyScanner; | ||
|
||
using System.IO; | ||
using System.Linq; | ||
using Hosting.Helpers; | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class When_directory_with_messages_referencing_core_or_interfaces_is_scanned | ||
{ | ||
[Test] | ||
public void Assemblies_should_be_scanned() | ||
{ | ||
var scanner = new AssemblyScanner(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestDlls", "Messages")) | ||
{ | ||
ScanAppDomainAssemblies = false | ||
}; | ||
|
||
var result = scanner.GetScannableAssemblies(); | ||
var assemblyFullNames = result.Assemblies.Select(a => a.GetName().Name).ToList(); | ||
|
||
CollectionAssert.Contains(assemblyFullNames, "Messages.Referencing.Core"); | ||
CollectionAssert.Contains(assemblyFullNames, "Messages.Referencing.MessageInterfaces"); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/NServiceBus.Core.Tests/AssemblyScanner/When_using_type_forwarding.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace NServiceBus.Core.Tests.AssemblyScanner; | ||
|
||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection.Metadata; | ||
using System.Reflection.PortableExecutable; | ||
using Hosting.Helpers; | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class When_using_type_forwarding | ||
{ | ||
// This test is not perfect since it relies on existing binaries to covered assembly scanning scenarios. Since we | ||
// already use those though the idea of this test is to make sure that the assembly scanner is able to scan all | ||
// assemblies that have a type forwarding rule within the core assembly. This might turn out to be a broad assumption | ||
// in the future, and we might have to explicitly remove some but in the meantime this test would have covered us | ||
// when we moved ICommand, IEvent and IMessages to the message interfaces assembly. | ||
[Test] | ||
public void Should_scan_assemblies_indicated_by_the_forwarding_metadata() | ||
{ | ||
using var fs = File.OpenRead(typeof(AssemblyScanner).Assembly.Location); | ||
using var peReader = new PEReader(fs); | ||
var metadataReader = peReader.GetMetadataReader(); | ||
|
||
// Exported types only contains a small subset of types, so it's safe to enumerate all of them | ||
var assemblyNamesOfForwardedTypes = metadataReader.ExportedTypes | ||
.Select(exportedTypeHandle => metadataReader.GetExportedType(exportedTypeHandle)) | ||
.Where(exportedType => exportedType.IsForwarder) | ||
.Select(exportedType => (AssemblyReferenceHandle)exportedType.Implementation) | ||
.Select(assemblyReferenceHandle => metadataReader.GetAssemblyReference(assemblyReferenceHandle)) | ||
.Select(assemblyReference => metadataReader.GetString(assemblyReference.Name)) | ||
.Where(assemblyName => assemblyName.StartsWith("NServiceBus") || assemblyName.StartsWith("Particular")) | ||
.Distinct() | ||
.ToList(); | ||
|
||
var scanner = new AssemblyScanner(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestDlls")) | ||
{ | ||
ScanAppDomainAssemblies = false | ||
}; | ||
|
||
var result = scanner.GetScannableAssemblies(); | ||
var assemblyFullNames = result.Assemblies.Select(a => a.GetName().Name).ToList(); | ||
|
||
CollectionAssert.IsSubsetOf(assemblyNamesOfForwardedTypes, assemblyFullNames); | ||
} | ||
} |
Binary file added
BIN
+4 KB
src/NServiceBus.Core.Tests/TestDlls/Messages/Messages.Referencing.Core.dll
Binary file not shown.
Binary file added
BIN
+4.5 KB
src/NServiceBus.Core.Tests/TestDlls/Messages/Messages.Referencing.MessageInterfaces.dll
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters