Skip to content

Commit

Permalink
+semver:minor - Fix disposing Class + Assembly shared data sources (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
thomhurst authored Dec 16, 2024
1 parent feddeb2 commit 8c55e0d
Show file tree
Hide file tree
Showing 15 changed files with 346 additions and 189 deletions.
36 changes: 19 additions & 17 deletions TUnit.Core/Attributes/TestData/ClassDataSourceAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,60 @@ namespace TUnit.Core;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]
public sealed class ClassDataSourceAttribute<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T> : DataSourceGeneratorAttribute<T> where T : new()
{
private DataGeneratorMetadata? _dataGeneratorMetadata;

public SharedType Shared { get; set; } = SharedType.None;
public string Key { get; set; } = string.Empty;
public override IEnumerable<Func<T>> GenerateDataSources(DataGeneratorMetadata dataGeneratorMetadata)
{
_dataGeneratorMetadata = dataGeneratorMetadata;

yield return () =>
{
var item = ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId)
var item = ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId)
.Get<T>(Shared, dataGeneratorMetadata.TestClassType, Key);

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestRegistered += async (_, context) =>
{
await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered(
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered(
context.TestContext,
_dataGeneratorMetadata?.PropertyInfo?.GetAccessors()[0].IsStatic == true,
dataGeneratorMetadata?.PropertyInfo?.GetAccessors()[0].IsStatic == true,
Shared,
Key,
item);
};

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestStart += async (_, context) =>
{
await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart(
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart(
context,
_dataGeneratorMetadata?.PropertyInfo?.GetAccessors()[0].IsStatic == true,
dataGeneratorMetadata?.PropertyInfo?.GetAccessors()[0].IsStatic == true,
Shared,
Key,
item);
};

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestSkipped += async (_, context) =>
{
await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd(Shared, Key, item);
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd(Shared, Key, item);
};

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestEnd += async (_, context) =>
{
await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd(Shared, Key, item);
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd(Shared, Key, item);
};

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, context) =>
if (Shared is SharedType.PerClass)
{
await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).IfLastTestInClass<T>(Shared);
};
dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, context) =>
{
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnLastTestInClass<T>(context.Item2);
};
}

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, context) =>
if (Shared is SharedType.PerAssembly)
{
await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).IfLastTestInAssembly<T>(Shared);
};
dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, context) =>
{
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnLastTestInAssembly<T>(context.Item2);
};
}

return item;
};
Expand Down
56 changes: 32 additions & 24 deletions TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,35 @@ public sealed class ClassDataSourceAttribute<
where T1 : new()
where T2 : new()
{
private DataGeneratorMetadata? _dataGeneratorMetadata;

public SharedType[] Shared { get; set; } = [SharedType.None, SharedType.None, SharedType.None, SharedType.None, SharedType.None];
public string[] Keys { get; set; } = [string.Empty, string.Empty, string.Empty, string.Empty, string.Empty];

public override IEnumerable<Func<(T1, T2)>> GenerateDataSources(DataGeneratorMetadata dataGeneratorMetadata)
{
_dataGeneratorMetadata = dataGeneratorMetadata;

yield return () =>
{
(
(T1 T, SharedType SharedType, string Key),
(T2 T, SharedType SharedType, string Key)
) itemsWithMetadata = (
ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId)
ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId)
.GetItemForIndex<T1>(0, dataGeneratorMetadata.TestClassType, Shared, Keys),
ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId)
ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId)
.GetItemForIndex<T2>(1, dataGeneratorMetadata.TestClassType, Shared, Keys)
);

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestRegistered += async (_, context) =>
{
var testContext = context.TestContext;

await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered(
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered(
testContext,
false,
itemsWithMetadata.Item1.SharedType,
itemsWithMetadata.Item1.Key,
itemsWithMetadata.Item1.T);

await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegistered(
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered(
testContext,
false,
itemsWithMetadata.Item2.SharedType,
Expand All @@ -52,14 +48,14 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestRegister

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestStart += async (_, context) =>
{
await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart(
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart(
context,
false,
itemsWithMetadata.Item1.SharedType,
itemsWithMetadata.Item1.Key,
itemsWithMetadata.Item1.T);

await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart(
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart(
context,
false,
itemsWithMetadata.Item2.SharedType,
Expand All @@ -69,44 +65,56 @@ await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestStart(

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestSkipped += async (_, _) =>
{
await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd(
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd(
itemsWithMetadata.Item1.SharedType,
itemsWithMetadata.Item1.Key,
itemsWithMetadata.Item1.T);

await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd(
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd(
itemsWithMetadata.Item2.SharedType,
itemsWithMetadata.Item2.Key,
itemsWithMetadata.Item2.T);
};

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestEnd += async (_, _) =>
{
await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd(
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd(
itemsWithMetadata.Item1.SharedType,
itemsWithMetadata.Item1.Key,
itemsWithMetadata.Item1.T);

await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId).OnTestEnd(
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd(
itemsWithMetadata.Item2.SharedType,
itemsWithMetadata.Item2.Key,
itemsWithMetadata.Item2.T);
};

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, _) =>
dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInClass += async (_, context) =>
{
await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId)
.IfLastTestInClass<T1>(itemsWithMetadata.Item1.SharedType);
await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId)
.IfLastTestInClass<T2>(itemsWithMetadata.Item2.SharedType);
if (Shared.ElementAtOrDefault(0) is SharedType.PerClass)
{
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnLastTestInClass<T1>(context.Item2);
}

if (Shared.ElementAtOrDefault(1) is SharedType.PerClass)
{
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnLastTestInClass<T2>(context.Item2);
}
};

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, _) =>
dataGeneratorMetadata.TestBuilderContext.Current.Events.OnLastTestInAssembly += async (_, context) =>
{
await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId)
.IfLastTestInAssembly<T1>(itemsWithMetadata.Item1.SharedType);
await ClassDataSources.Get(_dataGeneratorMetadata!.TestSessionId)
.IfLastTestInAssembly<T2>(itemsWithMetadata.Item2.SharedType);
if (Shared.ElementAtOrDefault(0) is SharedType.PerAssembly)
{
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId)
.OnLastTestInAssembly<T1>(context.Item2);
}

if (Shared.ElementAtOrDefault(1) is SharedType.PerAssembly)
{
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId)
.OnLastTestInAssembly<T2>(context.Item2);
}
};

return (
Expand Down
Loading

0 comments on commit 8c55e0d

Please sign in to comment.