Skip to content

Commit

Permalink
Merge pull request #18 from mkolumb/feature/parallel-tests
Browse files Browse the repository at this point in the history
Add multitasking test limit per provider
  • Loading branch information
mkolumb authored Jul 19, 2022
2 parents c435cf3 + 190b147 commit 64cf61c
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Collections.Concurrent;
using System.Reflection;
using System.Text;
using EFCore.Extensions.SaveOptimizer.Model.Context;
using EFCore.Extensions.SaveOptimizer.Shared.Tests.Data;
Expand All @@ -10,28 +11,74 @@ public sealed class EntityCollectionAttribute : Attribute
{
private const string TestMultitaskingDisabledProviders = "TEST_MULTITASKING_DISABLED_PROVIDERS";

private const string TestMultitaskingProvidersLimit = "TEST_MULTITASKING_PROVIDERS_LIMIT";

private static readonly ConcurrentDictionary<string, string> CollectionNames = new();

private readonly Type[] _entityTypes;

private readonly string _provider;

private readonly string _realCollectionName;

public string CollectionName { get; }

public EntityCollectionAttribute(string provider, params Type[] entityTypes)
{
_provider = provider;

_entityTypes = entityTypes;

StringBuilder builder = new(entityTypes.Length + 1);
_realCollectionName = ResolveName();

CollectionName = GetCollectionName();
}

private string GetCollectionName()
{
if (CollectionNames.ContainsKey(_realCollectionName))
{
return CollectionNames[_realCollectionName];
}

var prefix = $"EntityCollection_{_provider}";

var providerLimit = GetProviderLimit();

string name;

if (providerLimit.HasValue)
{
var index = CollectionNames.Count % providerLimit;

name = $"{prefix}_{index}";
}
else
{
name = _realCollectionName;
}

CollectionNames[_realCollectionName] = name;

return name;
}

private string ResolveName()
{
StringBuilder builder = new(_entityTypes.Length + 1);

builder.Append("EntityCollection_");

builder.Append(provider);
builder.Append(_provider);

builder.Append(' ');

if (MultitaskingSupported())
{
builder.AppendJoin("_", entityTypes.Select(x => x.Name));
builder.AppendJoin("_", _entityTypes.Select(x => x.Name));
}

CollectionName = builder.ToString().Trim().Replace(" ", "_");
return builder.ToString().Trim().Replace(" ", "_");
}

public bool IsConnectedCollection(string collection)
Expand All @@ -45,6 +92,12 @@ public bool IsConnectedCollection(string collection)
return _entityTypes.Contains(argument);
}

private static bool MultitaskingSupported() =>
!TestDataHelper.IsDisabled(TestDataHelper.GetValues(TestMultitaskingDisabledProviders));
private static bool MultitaskingSupported() => !TestDataHelper.IsDisabled(TestMultitaskingDisabledProviders);

private static int? GetProviderLimit()
{
var value = TestDataHelper.GetProviderValue(TestMultitaskingProvidersLimit);

return value != null ? int.Parse(value) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,5 @@ public SkippableFactAttribute()
}
}

private static bool ShouldSkip()
{
var providers = TestDataHelper.GetValues(TestDisabledProviders);

return TestDataHelper.IsDisabled(providers);
}
private static bool ShouldSkip() => TestDataHelper.IsDisabled(TestDisabledProviders);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,5 @@ public SkippableTheoryAttribute()
}
}

private static bool ShouldSkip()
{
var providers = TestDataHelper.GetValues(TestDisabledProviders);

return TestDataHelper.IsDisabled(providers);
}
private static bool ShouldSkip() => TestDataHelper.IsDisabled(TestDisabledProviders);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ public static class SharedTheoryData

private static bool IsFullMode => TestDataHelper.GetValue(TestLoadMode) == FullLoadValue;

private static IEnumerable<string> DisabledFullLoadProviders =>
TestDataHelper.GetValues(TestFullLoadDisabledProviders);

public static IEnumerable<IEnumerable<object>> TransactionTestTheoryData
{
get
Expand Down Expand Up @@ -102,7 +99,7 @@ public static IEnumerable<IEnumerable<object>> BaseWriteTheoryData
}
}

if (TestDataHelper.IsDisabled(DisabledFullLoadProviders))
if (TestDataHelper.IsDisabled(TestFullLoadDisabledProviders))
{
yield break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,33 @@ public static string[] GetValues(string key)
return value == null ? Array.Empty<string>() : value.Split(";");
}

public static bool IsDisabled(IEnumerable<string> disabledList) => disabledList
.Any(p => !string.IsNullOrEmpty(p) &&
GetValue(ProjectName)!.Contains(p, StringComparison.InvariantCultureIgnoreCase));
public static bool IsDisabled(string variableName) => GetProviderConstraint(variableName) != null;

public static string? GetProviderValue(string variableName)
{
var value = GetProviderConstraint(variableName)?.Split(':').Last();

return string.IsNullOrWhiteSpace(value) ? null : value;
}

private static string? GetProviderConstraint(string variableName)
{
var list = GetValues(variableName);

IEnumerable<string> values = list.Where(p =>
!string.IsNullOrEmpty(GetConstraintName(p)) &&
GetValue(ProjectName)!.Contains(GetConstraintName(p), StringComparison.InvariantCultureIgnoreCase));

return values.MaxBy(x => x.Length);
}

private static string GetConstraintName(string? value)
{
if (string.IsNullOrWhiteSpace(value))
{
return string.Empty;
}

return value.Contains(':') ? value.Split(':').First() : value;
}
}
2 changes: 1 addition & 1 deletion EFCore.Extensions.SaveOptimizer/xunit.runner.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"parallelizeAssembly": true,
"parallelizeTestCollections": true,
"maxParallelThreads": 4
"maxParallelThreads": 8
}
5 changes: 3 additions & 2 deletions environment.settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"TEST_LOAD_MODE": "FULL",
"TEST_FULL_LOAD_DISABLED_PROVIDERS": "Oracle;Firebird",
"TEST_MULTITASKING_DISABLED_PROVIDERS": "Oracle;Firebird",
"TEST_DISABLED_PROVIDERS": "CockroachMulti"
"TEST_DISABLED_PROVIDERS": "CockroachMulti",
"TEST_MULTITASKING_DISABLED_PROVIDERS": "Firebird",
"TEST_MULTITASKING_PROVIDERS_LIMIT": "Oracle:4"
}
2 changes: 1 addition & 1 deletion github.runsettings
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<MaxCpuCount>4</MaxCpuCount>
<MaxCpuCount>8</MaxCpuCount>
<TargetFrameworkVersion>net6.0</TargetFrameworkVersion>
<TreatNoTestsAsError>true</TreatNoTestsAsError>
</RunConfiguration>
Expand Down

0 comments on commit 64cf61c

Please sign in to comment.