Skip to content

Commit

Permalink
Can Get Shell By Case Insensitive Name. (#12120)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Aug 1, 2022
1 parent 0b41196 commit 12fcd58
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 21 deletions.
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();
}
}
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
42 changes: 42 additions & 0 deletions test/OrchardCore.Tests/Shell/ShellHostTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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
{
public static IShellHost ShellHost { get; }

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

[Theory]
[InlineData("Tenant1", "tEnAnT1")]
[InlineData(ShellHelper.DefaultShellName, "dEfAuLt")]
public static async Task CanGetShellByCaseInsensitiveName(string name, string searchName)
{
await ShellHost.InitializeAsync();

var shellContext = await ShellHost.GetOrCreateShellContextAsync(
new ShellSettings()
{
Name = name,
State = TenantState.Uninitialized,
});

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

Assert.NotNull(shellContext);
Assert.NotEqual(name, searchName);

Assert.Same(foundShellSettings, shellContext.Settings);
Assert.Same(foundShellContext, shellContext);
}
}

0 comments on commit 12fcd58

Please sign in to comment.