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

Fix name collision in indexer cache (#2645) #2669

Merged
merged 1 commit into from
Feb 2, 2021
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
12 changes: 10 additions & 2 deletions src/Microsoft.Azure.WebJobs.Host/Indexers/FunctionIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@ public void Add(IFunctionDefinition function, FunctionDescriptor descriptor, Met
_functionsByMethod.Add(method, function);

// For compat, accept either the short name ("Class.Name") or log name (just "Name")
_functionsByName.Add(descriptor.LogName, function);
if (descriptor.ShortName != descriptor.LogName)
if (!_functionsByName.ContainsKey(descriptor.LogName))
Copy link
Member Author

@mathewc mathewc Feb 2, 2021

Choose a reason for hiding this comment

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

This fix retains the previous logic of first one wins that existed before cache was added. See previous logic here.

{
// since there can be duplicate method names across job classes, it's first one
// wins for this cache
_functionsByName.Add(descriptor.LogName, function);
}
if (descriptor.ShortName != descriptor.LogName &&
!_functionsByName.ContainsKey(descriptor.ShortName))
{
// we do a duplicate check here as well for completeness, though a conflict here
// is much less likely (could only happen if functions are coming from multiple assemblies).
_functionsByName.Add(descriptor.ShortName, function);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,41 @@ public void IsJobMethod_ReturnsFalse_IfMethodHasNoSdkAttributes()
Assert.Equal(false, actual);
}

[Fact]
public async Task IndexMethod_SameName_Succeeds()
{
FunctionIndex index = new FunctionIndex();
FunctionIndexer product = CreateProductUnderTest();

await product.IndexMethodAsync(typeof(ClassA).GetMethod("Test"), index, CancellationToken.None);
await product.IndexMethodAsync(typeof(ClassB).GetMethod("Test"), index, CancellationToken.None);

var functions = index.ReadAll().ToArray();
Assert.Equal(2, functions.Length);

Assert.Equal("ClassA.Test", index.LookupByName("Test").Descriptor.ShortName);
Assert.Equal("ClassA.Test", index.LookupByName("ClassA.Test").Descriptor.ShortName);
Assert.Equal("ClassB.Test", index.LookupByName("ClassB.Test").Descriptor.ShortName);
}

public class ClassA
{
[NoAutomaticTrigger]
public void Test(string test, ILogger logger)
{
logger.LogInformation("Test invoked!");
}
}

public class ClassB
{
[NoAutomaticTrigger]
public void Test(string test, ILogger logger)
{
logger.LogInformation("Test invoked!");
}
}

private class TestExtensionBindingProvider : IBindingProvider
{
public Task<IBinding> TryCreateAsync(BindingProviderContext context)
Expand Down