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 1 commit
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
45 changes: 22 additions & 23 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,22 +55,21 @@ 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
{
_initialized = true;
_initializingSemaphore.Release();
}
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
return;
}

// Prevent concurrent requests from creating all shells multiple times
await _initializingSemaphore.WaitAsync();
try
{
await PreCreateAndRegisterShellsAsync();
_initialized = true;
}
finally
{
_initializingSemaphore.Release();
}
}

Expand Down Expand Up @@ -183,10 +182,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 +446,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 All @@ -473,6 +470,8 @@ public void Dispose()
{
shell.Dispose();
}

GC.SuppressFinalize(this);
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
63 changes: 63 additions & 0 deletions test/OrchardCore.Tests/Shell/ShellHostTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Environment.Shell;
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>();
}

[Fact]
public static async Task FindCaseInsensitiveShellSettings()
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
{
const string exactName = "Test";

var testSettings = new ShellSettings()
{
Name = exactName,
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
};

await ShellHost.GetOrCreateShellContextAsync(testSettings);

ShellHost.TryGetSettings(exactName, out var foundExactShellSettings);
ShellHost.TryGetSettings(exactName, out var foundExactShellShellContext);
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved

ShellHost.TryGetSettings("test", out var foundMixedShellSettings);
ShellHost.TryGetSettings("test", out var foundMixedShellShellContext);
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved

ShellHost.TryGetSettings("missing", out var missingShellSettings);
ShellHost.TryGetSettings("missing", out var missingShellShellContext);

Assert.NotNull(foundExactShellSettings);
Assert.NotNull(foundExactShellShellContext);

Assert.NotNull(foundMixedShellSettings);
Assert.NotNull(foundMixedShellShellContext);

Assert.Null(missingShellSettings);
Assert.Null(missingShellShellContext);
}

[Fact]
public static async Task FindCaseInsensitiveDefaultShellSettings()
{
var defaultSettings = new ShellSettings()
{
Name = ShellHelper.DefaultShellName,
};

await ShellHost.GetOrCreateShellContextAsync(defaultSettings);

ShellHost.TryGetSettings("dEFaUlT", out var defaultTenant);

Assert.NotNull(defaultTenant);
Assert.True(defaultTenant.IsDefaultShell());
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
}
}
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved