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 2 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
95 changes: 95 additions & 0 deletions test/OrchardCore.Tests/Shell/ShellHostTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
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>();
}

[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
State = TenantState.Uninitialized,
};

await ShellHost.GetOrCreateShellContextAsync(testSettings);

ShellHost.TryGetSettings(exactName, out var foundExactShellSettings);
ShellHost.TryGetSettings("test", out var foundMixedShellSettings);
ShellHost.TryGetSettings("missing", out var missingShellSettings);

Assert.NotNull(foundExactShellSettings);
Assert.NotNull(foundMixedShellSettings);
Assert.Null(missingShellSettings);
}

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

await ShellHost.GetOrCreateShellContextAsync(defaultSettings);

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

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


MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved

[Fact]
public static async Task FindCaseInsensitiveShellContext()
{
const string exactName = "Test";

var testSettings = new ShellSettings()
{
Name = exactName,
State = TenantState.Uninitialized,
};

await ShellHost.GetOrCreateShellContextAsync(testSettings);

ShellHost.TryGetShellContext(exactName, out var foundExactShellShellContext);
ShellHost.TryGetShellContext("test", out var foundMixedShellShellContext);
ShellHost.TryGetShellContext("missing", out var missingShellShellContext);

Assert.NotNull(foundExactShellShellContext);
Assert.NotNull(foundMixedShellShellContext);
Assert.Null(missingShellShellContext);
}

[Fact]
public static async Task FindCaseInsensitiveDefaultShellContext()
{
var defaultSettings = new ShellSettings()
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
{
Name = ShellHelper.DefaultShellName,
State = TenantState.Uninitialized,
};

await ShellHost.GetOrCreateShellContextAsync(defaultSettings);

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

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