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

Can Get Shell By Case Insensitive Name. #12120

Merged
Show file tree
Hide file tree
Changes from 6 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
42 changes: 21 additions & 21 deletions src/OrchardCore/OrchardCore/Shell/ShellHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public class ShellHost : IShellHost, IDisposable
private readonly ILogger _logger;

private bool _initialized;
private readonly ConcurrentDictionary<string, ShellContext> _shellContexts = new ConcurrentDictionary<string, ShellContext>();
private readonly ConcurrentDictionary<string, ShellSettings> _shellSettings = new ConcurrentDictionary<string, ShellSettings>();
private readonly ConcurrentDictionary<string, SemaphoreSlim> _shellSemaphores = new ConcurrentDictionary<string, SemaphoreSlim>();
private SemaphoreSlim _initializingSemaphore = new SemaphoreSlim(1);
private readonly ConcurrentDictionary<string, ShellContext> _shellContexts = new(StringComparer.OrdinalIgnoreCase);
private readonly ConcurrentDictionary<string, ShellSettings> _shellSettings = new(StringComparer.OrdinalIgnoreCase);
private readonly ConcurrentDictionary<string, SemaphoreSlim> _shellSemaphores = new();
private readonly SemaphoreSlim _initializingSemaphore = new(1);

public ShellHost(
IShellSettingsManager shellSettingsManager,
Expand All @@ -55,23 +55,25 @@ public ShellHost(

public async Task InitializeAsync()
{
if (!_initialized)
if (_initialized)
{
// Prevent concurrent requests from creating all shells multiple times
await _initializingSemaphore.WaitAsync();
try
{
if (!_initialized)
{
await PreCreateAndRegisterShellsAsync();
}
}
finally
return;
}

// Prevent concurrent requests from creating all shells multiple times
await _initializingSemaphore.WaitAsync();
try
{
if (!_initialized)
{
_initialized = true;
_initializingSemaphore.Release();
await PreCreateAndRegisterShellsAsync();
}
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
}
finally
{
_initialized = true;
_initializingSemaphore.Release();
}
}

public async Task<ShellContext> GetOrCreateShellContextAsync(ShellSettings settings)
Expand Down Expand Up @@ -183,10 +185,8 @@ public async Task ReloadShellContextAsync(ShellSettings settings, bool eventSour
}

var count = 0;
while (count < ReloadShellMaxRetriesCount)
while (count++ < ReloadShellMaxRetriesCount)
{
count++;

if (_shellContexts.TryRemove(settings.Name, out var context))
{
_runningShellTable.Remove(settings);
Expand Down Expand Up @@ -449,7 +449,7 @@ private bool CanCreateShell(ShellSettings shellSettings)
/// <summary>
/// Whether or not a shell can be activated and added to the running shells.
/// </summary>
private bool CanRegisterShell(ShellSettings shellSettings)
private static bool CanRegisterShell(ShellSettings shellSettings)
{
return
shellSettings.State == TenantState.Running ||
Expand Down
40 changes: 40 additions & 0 deletions test/OrchardCore.Tests/Shell/ShellHostTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Environment.Shell;
using OrchardCore.Environment.Shell.Models;
using OrchardCore.Tests.Apis.Context;
using Xunit;

namespace OrchardCore.Tests.Shell;

public class ShellHostTests : SiteContext
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
{
public static IShellHost ShellHost { get; }
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved

static ShellHostTests()
{
ShellHost = Site.Services.GetRequiredService<IShellHost>();
}

[Theory]
[InlineData("Tenant1", "tEnAnT1")]
[InlineData(ShellHelper.DefaultShellName, "dEfAuLt")]
public static async Task ShellCanBeFoundByCaseInsensitiveName(string name, string searchName)
{
var shellContext = await ShellHost.GetOrCreateShellContextAsync(
new ShellSettings()
{
Name = name,
State = TenantState.Uninitialized,
});

var foundShellSettings = ShellHost.GetSettings(searchName);
ShellHost.TryGetShellContext(searchName, out var foundShellContext);

Assert.NotNull(shellContext);
Assert.NotEqual(name, searchName);
Assert.Same(foundShellSettings, shellContext.Settings);
Assert.Same(foundShellContext.Settings, shellContext.Settings);
Assert.Same(foundShellContext, shellContext);
}
}
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved