|
|
|
@ -6,10 +6,7 @@ |
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Collections.Concurrent; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using FakeItEasy; |
|
|
|
using Microsoft.Extensions.Caching.Memory; |
|
|
|
@ -17,7 +14,6 @@ using Microsoft.Extensions.DependencyInjection; |
|
|
|
using Microsoft.Extensions.Options; |
|
|
|
using Orleans; |
|
|
|
using Orleans.Hosting; |
|
|
|
using Orleans.Runtime; |
|
|
|
using Orleans.TestingHost; |
|
|
|
using Squidex.Caching; |
|
|
|
using Squidex.Domain.Apps.Core.Apps; |
|
|
|
@ -33,9 +29,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes |
|
|
|
[Trait("Category", "Dependencies")] |
|
|
|
public class AppsIndexIntegrationTests |
|
|
|
{ |
|
|
|
private static GrainRuntime currentRuntime; |
|
|
|
|
|
|
|
public class GrainRuntime |
|
|
|
public class GrainEnvironment |
|
|
|
{ |
|
|
|
private AppContributors contributors = AppContributors.Empty; |
|
|
|
|
|
|
|
@ -43,9 +37,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes |
|
|
|
|
|
|
|
public NamedId<DomainId> AppId { get; } = NamedId.Of(DomainId.NewGuid(), "my-app"); |
|
|
|
|
|
|
|
public bool ShouldBreak { get; set; } |
|
|
|
|
|
|
|
public GrainRuntime() |
|
|
|
public GrainEnvironment() |
|
|
|
{ |
|
|
|
var indexGrain = A.Fake<IAppsByNameIndexGrain>(); |
|
|
|
|
|
|
|
@ -80,10 +72,10 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes |
|
|
|
var appEntity = A.Fake<IAppEntity>(); |
|
|
|
|
|
|
|
A.CallTo(() => appEntity.Id) |
|
|
|
.Returns(currentRuntime.AppId.Id); |
|
|
|
.Returns(AppId.Id); |
|
|
|
|
|
|
|
A.CallTo(() => appEntity.Name) |
|
|
|
.Returns(currentRuntime.AppId.Name); |
|
|
|
.Returns(AppId.Name); |
|
|
|
|
|
|
|
A.CallTo(() => appEntity.Contributors) |
|
|
|
.Returns(new AppContributors(contributors.ToDictionary())); |
|
|
|
@ -97,62 +89,6 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes |
|
|
|
public void Configure(ISiloBuilder siloBuilder) |
|
|
|
{ |
|
|
|
siloBuilder.AddOrleansPubSub(); |
|
|
|
siloBuilder.AddStartupTask<SiloHandle>(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private class NoopPubSub : IPubSub |
|
|
|
{ |
|
|
|
public Task PublishAsync(object? payload) |
|
|
|
{ |
|
|
|
return Task.CompletedTask; |
|
|
|
} |
|
|
|
|
|
|
|
public Task SubscribeAsync(Action<object?> subscriber) |
|
|
|
{ |
|
|
|
return Task.CompletedTask; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected sealed class SiloHandle : IStartupTask, IDisposable |
|
|
|
{ |
|
|
|
private static readonly ConcurrentDictionary<SiloHandle, SiloHandle> AllSilos = new ConcurrentDictionary<SiloHandle, SiloHandle>(); |
|
|
|
|
|
|
|
public AppsIndex Index { get; } |
|
|
|
|
|
|
|
public static ICollection<SiloHandle> All => AllSilos.Keys; |
|
|
|
|
|
|
|
public SiloHandle(IPubSub pubSub) |
|
|
|
{ |
|
|
|
if (currentRuntime.ShouldBreak) |
|
|
|
{ |
|
|
|
pubSub = new NoopPubSub(); |
|
|
|
} |
|
|
|
|
|
|
|
var cache = |
|
|
|
new ReplicatedCache( |
|
|
|
new MemoryCache(Options.Create(new MemoryCacheOptions())), |
|
|
|
pubSub, |
|
|
|
Options.Create(new ReplicatedCacheOptions { Enable = true })); |
|
|
|
|
|
|
|
Index = new AppsIndex(currentRuntime.GrainFactory, cache); |
|
|
|
} |
|
|
|
|
|
|
|
public static void Clear() |
|
|
|
{ |
|
|
|
AllSilos.Clear(); |
|
|
|
} |
|
|
|
|
|
|
|
public Task Execute(CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
AllSilos.TryAdd(this, this); |
|
|
|
|
|
|
|
return Task.CompletedTask; |
|
|
|
} |
|
|
|
|
|
|
|
public void Dispose() |
|
|
|
{ |
|
|
|
AllSilos.TryRemove(this, out _); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -161,7 +97,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes |
|
|
|
[InlineData(3, 100, 102, true)] |
|
|
|
public async Task Should_distribute_and_cache_domain_objects(short numSilos, int numRuns, int expectedCounts, bool shouldBreak) |
|
|
|
{ |
|
|
|
currentRuntime = new GrainRuntime { ShouldBreak = shouldBreak }; |
|
|
|
var env = new GrainEnvironment(); |
|
|
|
|
|
|
|
var cluster = |
|
|
|
new TestClusterBuilder(numSilos) |
|
|
|
@ -172,7 +108,25 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
var appId = currentRuntime.AppId; |
|
|
|
var indexes = |
|
|
|
cluster.Silos.OfType<InProcessSiloHandle>() |
|
|
|
.Select(x => |
|
|
|
{ |
|
|
|
var pubSub = |
|
|
|
shouldBreak ? |
|
|
|
A.Fake<IPubSub>() : |
|
|
|
x.SiloHost.Services.GetRequiredService<IPubSub>(); |
|
|
|
|
|
|
|
var cache = |
|
|
|
new ReplicatedCache( |
|
|
|
new MemoryCache(Options.Create(new MemoryCacheOptions())), |
|
|
|
pubSub, |
|
|
|
Options.Create(new ReplicatedCacheOptions { Enable = true })); |
|
|
|
|
|
|
|
return new AppsIndex(env.GrainFactory, cache); |
|
|
|
}).ToArray(); |
|
|
|
|
|
|
|
var appId = env.AppId; |
|
|
|
|
|
|
|
var random = new Random(); |
|
|
|
|
|
|
|
@ -183,13 +137,13 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes |
|
|
|
|
|
|
|
var commandContext = new CommandContext(contributorCommand, A.Fake<ICommandBus>()); |
|
|
|
|
|
|
|
var randomSilo = SiloHandle.All.ElementAt(random.Next(numSilos)); |
|
|
|
var randomIndex = indexes[random.Next(numSilos)]; |
|
|
|
|
|
|
|
await randomSilo.Index.HandleAsync(commandContext, x => |
|
|
|
await randomIndex.HandleAsync(commandContext, x => |
|
|
|
{ |
|
|
|
if (x.Command is AssignContributor command) |
|
|
|
{ |
|
|
|
currentRuntime.HandleCommand(command); |
|
|
|
env.HandleCommand(command); |
|
|
|
} |
|
|
|
|
|
|
|
x.Complete(true); |
|
|
|
@ -197,12 +151,12 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes |
|
|
|
return Task.CompletedTask; |
|
|
|
}); |
|
|
|
|
|
|
|
foreach (var silo in SiloHandle.All) |
|
|
|
foreach (var index in indexes) |
|
|
|
{ |
|
|
|
var appById = await silo.Index.GetAppAsync(appId.Id, true); |
|
|
|
var appByName = await silo.Index.GetAppByNameAsync(appId.Name, true); |
|
|
|
var appById = await index.GetAppAsync(appId.Id, true); |
|
|
|
var appByName = await index.GetAppByNameAsync(appId.Name, true); |
|
|
|
|
|
|
|
if (silo == randomSilo || !currentRuntime.ShouldBreak || i == 0) |
|
|
|
if (index == randomIndex || !shouldBreak || i == 0) |
|
|
|
{ |
|
|
|
Assert.True(appById?.Contributors.ContainsKey(contributorId)); |
|
|
|
Assert.True(appByName?.Contributors.ContainsKey(contributorId)); |
|
|
|
@ -215,12 +169,10 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
currentRuntime.VerifyGrainAccess(expectedCounts); |
|
|
|
env.VerifyGrainAccess(expectedCounts); |
|
|
|
} |
|
|
|
finally |
|
|
|
{ |
|
|
|
SiloHandle.Clear(); |
|
|
|
|
|
|
|
await Task.WhenAny(Task.Delay(2000), cluster.StopAllSilosAsync()); |
|
|
|
} |
|
|
|
} |
|
|
|
|