Browse Source

Improve test readability.

pull/613/head
Sebastian 5 years ago
parent
commit
3f2b56be11
  1. 112
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Indexes/AppsIndexIntegrationTests.cs
  2. 110
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/Indexes/SchemasIndexIntegrationTests.cs
  3. 7
      backend/tests/Squidex.Infrastructure.Tests/Commands/CommandRequestTests.cs
  4. 6
      backend/tests/Squidex.Infrastructure.Tests/Orleans/AsyncLocalTests.cs
  5. 38
      backend/tests/Squidex.Infrastructure.Tests/Orleans/ExceptionWrapperFilterTests.cs

112
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<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());
}
}

110
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<DomainId> SchemaId { get; } = NamedId.Of(DomainId.NewGuid(), "my-schema");
public bool ShouldBreak { get; set; }
public GrainRuntime()
public GrainEnvironment()
{
var indexGrain = A.Fake<ISchemasByAppIndexGrain>();
@ -83,10 +79,10 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes
var appEntity = A.Fake<ISchemaEntity>();
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<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 SchemasIndex 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 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<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 SchemasIndex(env.GrainFactory, cache);
}).ToArray();
var schemaId = currentRuntime.SchemaId;
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<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 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());
}
}

7
backend/tests/Squidex.Infrastructure.Tests/Commands/CommandRequestTests.cs

@ -65,6 +65,8 @@ namespace Squidex.Infrastructure.Commands
await cluster.DeployAsync();
try
{
var culture = CultureInfo.GetCultureInfo("de");
var cultureUI = CultureInfo.GetCultureInfo("it");
@ -81,5 +83,10 @@ namespace Squidex.Infrastructure.Commands
Assert.Equal(culture.Name, cultureFromGrain);
Assert.Equal(cultureUI.Name, cultureUIFromGrain);
}
finally
{
await Task.WhenAny(Task.Delay(2000), cluster.StopAllSilosAsync());
}
}
}
}

6
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<IAsyncLocalGrain>(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());
}
}
}
}

38
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<Configurator>()
.Build();
await cluster.DeployAsync();
var grain = cluster.GrainFactory.GetGrain<IExceptionGrain>(SingleGrain.Id);
var (cluster, grain) = await GetGrainAsync();
try
{
var ex = await Assert.ThrowsAsync<OrleansWrapperException>(() => grain.ThrowCustomAsync());
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<OrleansWrapperException>(() => 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<IExceptionGrain>(SingleGrain.Id);
var ex = await Assert.ThrowsAsync<OrleansWrapperException>(() => grain.ThrowMongoAsync());
Assert.Equal(typeof(MongoWriteException), ex.ExceptionType);
return (cluster, cluster.GrainFactory.GetGrain<IExceptionGrain>(SingleGrain.Id));
}
}
}

Loading…
Cancel
Save