Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions src/Orleans.Serialization.TestKit/CopierTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,34 @@ namespace Orleans.Serialization.TestKit
/// </summary>
[Trait("Category", "BVT")]
[ExcludeFromCodeCoverage]
public abstract class CopierTester<TValue, TCopier> where TCopier : class, IDeepCopier<TValue>
public abstract class CopierTester<TValue, TCopier> : SerializationTester where TCopier : class, IDeepCopier<TValue>
{
private readonly IServiceProvider _serviceProvider;
private readonly CodecProvider _codecProvider;

/// <summary>
/// Initializes a new <see cref="CopierTester{TValue, TCopier}"/> instance.
/// </summary>
protected CopierTester(ITestOutputHelper output)
protected CopierTester(ITestOutputHelper output) : base(output)
{
output.WriteLine($"Random seed: {RandomSeed}");
_codecProvider = ServiceProvider.GetRequiredService<CodecProvider>();
}

/// <summary>
/// Initializes a new <see cref="CopierTester{TValue, TCopier}"/> instance.
/// </summary>
protected CopierTester(ITestOutputHelper output, SerializationTesterFixture fixture) : base(output, fixture)
{
output.WriteLine($"Random seed: {RandomSeed}");
_codecProvider = ServiceProvider.GetRequiredService<CodecProvider>();
}

/// <inheritdoc/>
protected override IServiceProvider CreateServiceProvider()
=> CreateServiceProviderCore(Configure);

internal static IServiceProvider CreateServiceProviderCore(Action<ISerializerBuilder> configure)
{
#if NET6_0_OR_GREATER
var seed = Random.Shared.Next();
#else
var seed = new Random().Next();
#endif
output.WriteLine($"Random seed: {seed}");
Random = new(seed);
var services = new ServiceCollection();
_ = services.AddSerializer(builder => builder.Configure(config => config.Copiers.Add(typeof(TCopier))));

Expand All @@ -41,22 +52,11 @@ protected CopierTester(ITestOutputHelper output)
_ = services.AddSingleton<TCopier>();
}

_ = services.AddSerializer(Configure);
_ = services.AddSerializer(configure);

_serviceProvider = services.BuildServiceProvider();
_codecProvider = _serviceProvider.GetRequiredService<CodecProvider>();
return services.BuildServiceProvider();
}

/// <summary>
/// Gets the random number generator.
/// </summary>
protected Random Random { get; }

/// <summary>
/// Gets the service provider.
/// </summary>
protected IServiceProvider ServiceProvider => _serviceProvider;

/// <summary>
/// Gets a value indicating whether the type copied by this codec is immutable.
/// </summary>
Expand All @@ -77,7 +77,7 @@ protected virtual void Configure(ISerializerBuilder builder)
/// <summary>
/// Creates a copier instance for testing.
/// </summary>
protected virtual TCopier CreateCopier() => _serviceProvider.GetRequiredService<TCopier>();
protected virtual TCopier CreateCopier() => ServiceProvider.GetRequiredService<TCopier>();

/// <summary>
/// Creates a value to copy.
Expand Down Expand Up @@ -139,7 +139,7 @@ public void ReferencesAreAddedToCopyContext()

var value = CreateValue();
var array = new TValue[] { value, value };
var arrayCopier = _serviceProvider.GetRequiredService<DeepCopier<TValue[]>>();
var arrayCopier = ServiceProvider.GetRequiredService<DeepCopier<TValue[]>>();
var arrayCopy = arrayCopier.Copy(array);
Assert.Same(arrayCopy[0], arrayCopy[1]);

Expand All @@ -159,7 +159,7 @@ public void ReferencesAreAddedToCopyContext()
[Fact]
public void CanCopyTupleViaSerializer()
{
var copier = _serviceProvider.GetRequiredService<DeepCopier<(string, TValue, TValue, string)>>();
var copier = ServiceProvider.GetRequiredService<DeepCopier<(string, TValue, TValue, string)>>();

var original = (Guid.NewGuid().ToString(), CreateValue(), CreateValue(), Guid.NewGuid().ToString());

Expand Down Expand Up @@ -189,7 +189,7 @@ public void CanCopyTupleViaSerializer()
[Fact]
public void CanCopyUntypedTupleViaSerializer()
{
var copier = _serviceProvider.GetRequiredService<DeepCopier<(string, object, object, string)>>();
var copier = ServiceProvider.GetRequiredService<DeepCopier<(string, object, object, string)>>();
var value = ((IEnumerable<TValue>)TestValues).Reverse().Concat(new[] { CreateValue(), CreateValue() }).Take(2).ToArray();

var original = (Guid.NewGuid().ToString(), (object)value[0], (object)value[1], Guid.NewGuid().ToString());
Expand Down Expand Up @@ -220,7 +220,7 @@ public void CanCopyUntypedTupleViaSerializer()
[Fact]
public void CanCopyCollectionViaSerializer()
{
var copier = _serviceProvider.GetRequiredService<DeepCopier<List<TValue>>>();
var copier = ServiceProvider.GetRequiredService<DeepCopier<List<TValue>>>();

var original = new List<TValue>();
original.AddRange(TestValues);
Expand All @@ -247,7 +247,7 @@ public void CanCopyCollectionViaSerializer()
[Fact]
public void CanCopyCollectionViaUntypedSerializer()
{
var copier = _serviceProvider.GetRequiredService<DeepCopier<List<object>>>();
var copier = ServiceProvider.GetRequiredService<DeepCopier<List<object>>>();

var original = new List<object>();
foreach (var value in TestValues)
Expand All @@ -272,4 +272,5 @@ public void CanCopyCollectionViaUntypedSerializer()
}
}
}

}
80 changes: 39 additions & 41 deletions src/Orleans.Serialization.TestKit/FieldCodecTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,34 @@ namespace Orleans.Serialization.TestKit
/// </summary>
[Trait("Category", "BVT")]
[ExcludeFromCodeCoverage]
public abstract class FieldCodecTester<TValue, TCodec> : IDisposable where TCodec : class, IFieldCodec<TValue>
public abstract class FieldCodecTester<TValue, TCodec> : SerializationTester where TCodec : class, IFieldCodec<TValue>
{
private readonly IServiceProvider _serviceProvider;
private readonly SerializerSessionPool _sessionPool;

/// <summary>
/// Initializes a new instance of the <see cref="FieldCodecTester{TValue, TCodec}"/> class.
/// </summary>
protected FieldCodecTester(ITestOutputHelper output)
protected FieldCodecTester(ITestOutputHelper output) : base(output)
{
output.WriteLine($"Random seed: {RandomSeed}");
_sessionPool = ServiceProvider.GetRequiredService<SerializerSessionPool>();
}

/// <summary>
/// Initializes a new instance of the <see cref="FieldCodecTester{TValue, TCodec}"/> class.
/// </summary>
protected FieldCodecTester(ITestOutputHelper output, SerializationTesterFixture fixture) : base(output, fixture)
{
output.WriteLine($"Random seed: {RandomSeed}");
_sessionPool = ServiceProvider.GetRequiredService<SerializerSessionPool>();
}

/// <inheritdoc/>
protected override IServiceProvider CreateServiceProvider()
=> CreateServiceProviderCore(Configure);

internal static IServiceProvider CreateServiceProviderCore(Action<ISerializerBuilder> configure)
{
#if NET6_0_OR_GREATER
var seed = Random.Shared.Next();
#else
var seed = new Random().Next();
#endif
output.WriteLine($"Random seed: {seed}");
Random = new(seed);
var services = new ServiceCollection();
_ = services.AddSerializer(builder => builder.Configure(config => config.FieldCodecs.Add(typeof(TCodec))));

Expand All @@ -49,22 +60,11 @@ protected FieldCodecTester(ITestOutputHelper output)
_ = services.AddSingleton<TCodec>();
}

_ = services.AddSerializer(Configure);
_ = services.AddSerializer(configure);

_serviceProvider = services.BuildServiceProvider();
_sessionPool = _serviceProvider.GetService<SerializerSessionPool>();
return services.BuildServiceProvider();
}

/// <summary>
/// Gets the random number generator.
/// </summary>
protected Random Random { get; }

/// <summary>
/// Gets the service provider.
/// </summary>
protected IServiceProvider ServiceProvider => _serviceProvider;

/// <summary>
/// Gets the session pool.
/// </summary>
Expand All @@ -85,7 +85,7 @@ protected virtual void Configure(ISerializerBuilder builder)
/// <summary>
/// Creates a codec.
/// </summary>
protected virtual TCodec CreateCodec() => _serviceProvider.GetRequiredService<TCodec>();
protected virtual TCodec CreateCodec() => ServiceProvider.GetRequiredService<TCodec>();

/// <summary>
/// Creates a value.
Expand All @@ -107,9 +107,6 @@ protected virtual void Configure(ISerializerBuilder builder)
/// </summary>
protected virtual Action<Action<TValue>> ValueProvider { get; }

/// <inheritdoc/>
void IDisposable.Dispose() => (_serviceProvider as IDisposable)?.Dispose();

protected virtual TValue GetWriteCopy(TValue input) => input;

/// <summary>
Expand Down Expand Up @@ -221,7 +218,7 @@ public void CorrectlyAdvancesReferenceCounter()
[Fact]
public void CanRoundTripViaSerializer_StreamPooled()
{
var serializer = _serviceProvider.GetRequiredService<Serializer<TValue>>();
var serializer = ServiceProvider.GetRequiredService<Serializer<TValue>>();

foreach (var original in TestValues)
{
Expand Down Expand Up @@ -259,7 +256,7 @@ void Test(TValue original)
[Fact]
public void CanRoundTripViaSerializer_Span()
{
var serializer = _serviceProvider.GetRequiredService<Serializer<TValue>>();
var serializer = ServiceProvider.GetRequiredService<Serializer<TValue>>();

foreach (var original in TestValues)
{
Expand Down Expand Up @@ -295,7 +292,7 @@ void Test(TValue original)
[Fact]
public void CanRoundTripViaSerializer_Array()
{
var serializer = _serviceProvider.GetRequiredService<Serializer<TValue>>();
var serializer = ServiceProvider.GetRequiredService<Serializer<TValue>>();

foreach (var original in TestValues)
{
Expand Down Expand Up @@ -331,7 +328,7 @@ void Test(TValue original)
[Fact]
public void CanRoundTripViaSerializer_Memory()
{
var serializer = _serviceProvider.GetRequiredService<Serializer<TValue>>();
var serializer = ServiceProvider.GetRequiredService<Serializer<TValue>>();

foreach (var original in TestValues)
{
Expand Down Expand Up @@ -367,7 +364,7 @@ void Test(TValue original)
[Fact]
public void CanRoundTripViaSerializer_MemoryStream()
{
var serializer = _serviceProvider.GetRequiredService<Serializer<TValue>>();
var serializer = ServiceProvider.GetRequiredService<Serializer<TValue>>();

foreach (var original in TestValues)
{
Expand Down Expand Up @@ -410,7 +407,7 @@ void Test(TValue original)
[Fact]
public void CanRoundTripViaSerializer_ReadByteByByte()
{
var serializer = _serviceProvider.GetRequiredService<Serializer<TValue>>();
var serializer = ServiceProvider.GetRequiredService<Serializer<TValue>>();

foreach (var original in TestValues)
{
Expand Down Expand Up @@ -449,7 +446,7 @@ void Test(TValue original)
[Fact]
public void ProducesValidBitStream()
{
var serializer = _serviceProvider.GetRequiredService<Serializer<TValue>>();
var serializer = ServiceProvider.GetRequiredService<Serializer<TValue>>();
foreach (var value in TestValues)
{
Test(value);
Expand Down Expand Up @@ -484,7 +481,7 @@ void Test(TValue value)
[Fact]
public void WritersProduceSameResults()
{
var serializer = _serviceProvider.GetRequiredService<Serializer<TValue>>();
var serializer = ServiceProvider.GetRequiredService<Serializer<TValue>>();

foreach (var original in TestValues)
{
Expand Down Expand Up @@ -590,7 +587,7 @@ void Test(TValue original)
[Fact]
public void CanRoundTripCollectionViaSerializer()
{
var serializer = _serviceProvider.GetRequiredService<Serializer<List<TValue>>>();
var serializer = ServiceProvider.GetRequiredService<Serializer<List<TValue>>>();

var original = new List<TValue>();
var originalCopy = new List<TValue>();
Expand Down Expand Up @@ -634,7 +631,7 @@ public void CanRoundTripCollectionViaSerializer()
[Fact]
public void CanRoundTripWeaklyTypedCollectionViaSerializer()
{
var serializer = _serviceProvider.GetRequiredService<Serializer<List<object>>>();
var serializer = ServiceProvider.GetRequiredService<Serializer<List<object>>>();

var original = new List<object>();
var originalCopy = new List<object>();
Expand Down Expand Up @@ -682,7 +679,7 @@ public void CanRoundTripWeaklyTypedCollectionViaSerializer()
[Fact]
public void CanRoundTripTupleViaSerializer()
{
var serializer = _serviceProvider.GetRequiredService<Serializer<(string, TValue, TValue, string)>>();
var serializer = ServiceProvider.GetRequiredService<Serializer<(string, TValue, TValue, string)>>();

var original = (Guid.NewGuid().ToString(), CreateValue(), CreateValue(), Guid.NewGuid().ToString());
var originalCopy = (original.Item1, GetWriteCopy(original.Item2), GetWriteCopy(original.Item3), original.Item4);
Expand Down Expand Up @@ -721,7 +718,7 @@ public void CanRoundTripTupleViaSerializer()
[Fact]
public void CanRoundTripViaSerializer()
{
var serializer = _serviceProvider.GetRequiredService<Serializer<TValue>>();
var serializer = ServiceProvider.GetRequiredService<Serializer<TValue>>();

foreach (var original in TestValues)
{
Expand Down Expand Up @@ -759,7 +756,7 @@ void Test(TValue original)
[Fact]
public void CanRoundTripViaObjectSerializer()
{
var serializer = _serviceProvider.GetRequiredService<Serializer<object>>();
var serializer = ServiceProvider.GetRequiredService<Serializer<object>>();

var buffer = new byte[10240];

Expand Down Expand Up @@ -830,7 +827,7 @@ public void CanRoundTripViaObjectSerializer()
[Fact]
public void CorrectlyHandlesBuffers()
{
var testers = BufferTestHelper<TValue>.GetTestSerializers(_serviceProvider, MaxSegmentSizes);
var testers = BufferTestHelper<TValue>.GetTestSerializers(ServiceProvider, MaxSegmentSizes);

foreach (var tester in testers)
{
Expand Down Expand Up @@ -998,4 +995,5 @@ protected object RoundTripThroughUntypedSerializer(object original, out string f
return result;
}
}

}
Loading
Loading