From 3f2b56be112a68fdaff31d0ab149aa928fdc17ef Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 30 Dec 2020 12:09:16 +0100 Subject: [PATCH] Improve test readability. --- .../Apps/Indexes/AppsIndexIntegrationTests.cs | 112 +++++------------- .../Indexes/SchemasIndexIntegrationTests.cs | 110 +++++------------ .../Commands/CommandRequestTests.cs | 27 +++-- .../Orleans/AsyncLocalTests.cs | 6 + .../Orleans/ExceptionWrapperFilterTests.cs | 42 ++++--- 5 files changed, 114 insertions(+), 183 deletions(-) diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsIndexIntegrationTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsIndexIntegrationTests.cs index 78a9ced11..777775e3a 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsIndexIntegrationTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsIndexIntegrationTests.cs @@ -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 AppId { get; } = NamedId.Of(DomainId.NewGuid(), "my-app"); - public bool ShouldBreak { get; set; } - - public GrainRuntime() + public GrainEnvironment() { var indexGrain = A.Fake(); @@ -80,10 +72,10 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes var appEntity = A.Fake(); 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(); - } - } - - private class NoopPubSub : IPubSub - { - public Task PublishAsync(object? payload) - { - return Task.CompletedTask; - } - - public Task SubscribeAsync(Action subscriber) - { - return Task.CompletedTask; - } - } - - protected sealed class SiloHandle : IStartupTask, IDisposable - { - private static readonly ConcurrentDictionary AllSilos = new ConcurrentDictionary(); - - public AppsIndex Index { get; } - - public static ICollection 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() + .Select(x => + { + var pubSub = + shouldBreak ? + A.Fake() : + x.SiloHost.Services.GetRequiredService(); + + 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()); - 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()); } } diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/Indexes/SchemasIndexIntegrationTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/Indexes/SchemasIndexIntegrationTests.cs index cd61b027e..855ee88c7 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/Indexes/SchemasIndexIntegrationTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/Indexes/SchemasIndexIntegrationTests.cs @@ -34,9 +34,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes [Trait("Category", "Dependencies")] public class SchemasIndexIntegrationTests { - private static GrainRuntime currentRuntime; - - public class GrainRuntime + public class GrainEnvironment { private Schema schema = new Schema("my-schema"); @@ -46,9 +44,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes public NamedId SchemaId { get; } = NamedId.Of(DomainId.NewGuid(), "my-schema"); - public bool ShouldBreak { get; set; } - - public GrainRuntime() + public GrainEnvironment() { var indexGrain = A.Fake(); @@ -83,10 +79,10 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes var appEntity = A.Fake(); A.CallTo(() => appEntity.Id) - .Returns(currentRuntime.SchemaId.Id); + .Returns(SchemaId.Id); A.CallTo(() => appEntity.AppId) - .Returns(currentRuntime.AppId); + .Returns(AppId); A.CallTo(() => appEntity.SchemaDef) .Returns(schema); @@ -100,62 +96,6 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes public void Configure(ISiloBuilder siloBuilder) { siloBuilder.AddOrleansPubSub(); - siloBuilder.AddStartupTask(); - } - } - - private class NoopPubSub : IPubSub - { - public Task PublishAsync(object? payload) - { - return Task.CompletedTask; - } - - public Task SubscribeAsync(Action subscriber) - { - return Task.CompletedTask; - } - } - - protected sealed class SiloHandle : IStartupTask, IDisposable - { - private static readonly ConcurrentDictionary AllSilos = new ConcurrentDictionary(); - - public SchemasIndex Index { get; } - - public static ICollection 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 SchemasIndex(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 _); } } @@ -164,7 +104,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas.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) @@ -175,26 +115,42 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes try { - var appId = currentRuntime.AppId; + var indexes = + cluster.Silos.OfType() + .Select(x => + { + var pubSub = + shouldBreak ? + A.Fake() : + x.SiloHost.Services.GetRequiredService(); + + var cache = + new ReplicatedCache( + new MemoryCache(Options.Create(new MemoryCacheOptions())), + pubSub, + Options.Create(new ReplicatedCacheOptions { Enable = true })); - var schemaId = currentRuntime.SchemaId; + return new SchemasIndex(env.GrainFactory, cache); + }).ToArray(); + + var appId = env.AppId; var random = new Random(); for (var i = 0; i < numRuns; i++) { var fieldName = Guid.NewGuid().ToString(); - var fieldCommand = new AddField { Name = fieldName, SchemaId = schemaId, AppId = appId }; + var fieldCommand = new AddField { Name = fieldName, SchemaId = env.SchemaId, AppId = env.AppId }; var commandContext = new CommandContext(fieldCommand, A.Fake()); - 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 AddField command) { - currentRuntime.HandleCommand(command); + env.HandleCommand(command); } x.Complete(true); @@ -202,12 +158,12 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes return Task.CompletedTask; }); - foreach (var silo in SiloHandle.All) + foreach (var index in indexes) { - var schemaById = await silo.Index.GetSchemaAsync(appId.Id, schemaId.Id, true); - var schemaByName = await silo.Index.GetSchemaByNameAsync(appId.Id, schemaId.Name, true); + var schemaById = await index.GetSchemaAsync(appId.Id, env.SchemaId.Id, true); + var schemaByName = await index.GetSchemaByNameAsync(appId.Id, env.SchemaId.Name, true); - if (silo == randomSilo || !currentRuntime.ShouldBreak || i == 0) + if (index == randomIndex || !shouldBreak || i == 0) { Assert.True(schemaById?.SchemaDef.FieldsByName.ContainsKey(fieldName)); Assert.True(schemaByName?.SchemaDef.FieldsByName.ContainsKey(fieldName)); @@ -220,12 +176,10 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes } } - currentRuntime.VerifyGrainAccess(expectedCounts); + env.VerifyGrainAccess(expectedCounts); } finally { - SiloHandle.Clear(); - await Task.WhenAny(Task.Delay(2000), cluster.StopAllSilosAsync()); } } diff --git a/backend/tests/Squidex.Infrastructure.Tests/Commands/CommandRequestTests.cs b/backend/tests/Squidex.Infrastructure.Tests/Commands/CommandRequestTests.cs index dd115f1de..ba2bc2a82 100644 --- a/backend/tests/Squidex.Infrastructure.Tests/Commands/CommandRequestTests.cs +++ b/backend/tests/Squidex.Infrastructure.Tests/Commands/CommandRequestTests.cs @@ -65,21 +65,28 @@ namespace Squidex.Infrastructure.Commands await cluster.DeployAsync(); - var culture = CultureInfo.GetCultureInfo("de"); - var cultureUI = CultureInfo.GetCultureInfo("it"); + try + { + var culture = CultureInfo.GetCultureInfo("de"); + var cultureUI = CultureInfo.GetCultureInfo("it"); - CultureInfo.CurrentCulture = culture; - CultureInfo.CurrentUICulture = cultureUI; + CultureInfo.CurrentCulture = culture; + CultureInfo.CurrentUICulture = cultureUI; - var grain = cluster.Client.GetGrain("Default"); + var grain = cluster.Client.GetGrain("Default"); - var request = CommandRequest.Create(null!); + var request = CommandRequest.Create(null!); - var cultureFromGrain = await grain.GetCultureAsync(request); - var cultureUIFromGrain = await grain.GetCultureUIAsync(request); + var cultureFromGrain = await grain.GetCultureAsync(request); + var cultureUIFromGrain = await grain.GetCultureUIAsync(request); - Assert.Equal(culture.Name, cultureFromGrain); - Assert.Equal(cultureUI.Name, cultureUIFromGrain); + Assert.Equal(culture.Name, cultureFromGrain); + Assert.Equal(cultureUI.Name, cultureUIFromGrain); + } + finally + { + await Task.WhenAny(Task.Delay(2000), cluster.StopAllSilosAsync()); + } } } } diff --git a/backend/tests/Squidex.Infrastructure.Tests/Orleans/AsyncLocalTests.cs b/backend/tests/Squidex.Infrastructure.Tests/Orleans/AsyncLocalTests.cs index 90a632d84..a371444a5 100644 --- a/backend/tests/Squidex.Infrastructure.Tests/Orleans/AsyncLocalTests.cs +++ b/backend/tests/Squidex.Infrastructure.Tests/Orleans/AsyncLocalTests.cs @@ -42,6 +42,8 @@ namespace Squidex.Infrastructure.Orleans await cluster.DeployAsync(); + try + { var grain = cluster.GrainFactory.GetGrain(SingleGrain.Id); var result1 = await grain.GetValueAsync(); @@ -55,6 +57,10 @@ namespace Squidex.Infrastructure.Orleans Assert.Equal(1, result1); Assert.Equal(1, result2); Assert.Equal(1, result3); + finally + { + await Task.WhenAny(Task.Delay(2000), cluster.StopAllSilosAsync()); + } } } } diff --git a/backend/tests/Squidex.Infrastructure.Tests/Orleans/ExceptionWrapperFilterTests.cs b/backend/tests/Squidex.Infrastructure.Tests/Orleans/ExceptionWrapperFilterTests.cs index 8a42c48bc..97119fa40 100644 --- a/backend/tests/Squidex.Infrastructure.Tests/Orleans/ExceptionWrapperFilterTests.cs +++ b/backend/tests/Squidex.Infrastructure.Tests/Orleans/ExceptionWrapperFilterTests.cs @@ -113,22 +113,38 @@ namespace Squidex.Infrastructure.Orleans [Fact, Trait("Category", "Dependencies")] public async Task Simple_grain_tests() { - var cluster = - new TestClusterBuilder(1) - .AddSiloBuilderConfigurator() - .Build(); - - await cluster.DeployAsync(); + var (cluster, grain) = await GetGrainAsync(); - var grain = cluster.GrainFactory.GetGrain(SingleGrain.Id); - - var ex = await Assert.ThrowsAsync(() => grain.ThrowCustomAsync()); + try + { + var ex = await Assert.ThrowsAsync(() => grain.ThrowCustomAsync()); - Assert.Equal(typeof(InvalidException), ex.ExceptionType); + Assert.Equal(typeof(InvalidException), ex.ExceptionType); + } + finally + { + await Task.WhenAny(Task.Delay(2000), cluster.StopAllSilosAsync()); + } } [Fact, Trait("Category", "Dependencies")] public async Task Simple_grain_tests_with_mongo_exception() + { + var (cluster, grain) = await GetGrainAsync(); + + try + { + var ex = await Assert.ThrowsAsync(() => grain.ThrowMongoAsync()); + + Assert.Equal(typeof(MongoWriteException), ex.ExceptionType); + } + finally + { + await Task.WhenAny(Task.Delay(2000), cluster.StopAllSilosAsync()); + } + } + + private static async Task<(TestCluster, IExceptionGrain)> GetGrainAsync() { var cluster = new TestClusterBuilder(1) @@ -137,11 +153,7 @@ namespace Squidex.Infrastructure.Orleans await cluster.DeployAsync(); - var grain = cluster.GrainFactory.GetGrain(SingleGrain.Id); - - var ex = await Assert.ThrowsAsync(() => grain.ThrowMongoAsync()); - - Assert.Equal(typeof(MongoWriteException), ex.ExceptionType); + return (cluster, cluster.GrainFactory.GetGrain(SingleGrain.Id)); } } }