Skip to content

Commit

Permalink
Ensure that all in-built codecs and copiers have corresponding tests,…
Browse files Browse the repository at this point in the history
… fix issues found (#8769)
  • Loading branch information
ReubenBond committed Dec 13, 2023
1 parent 64cc3aa commit eca20f5
Show file tree
Hide file tree
Showing 6 changed files with 1,688 additions and 1,106 deletions.
3 changes: 2 additions & 1 deletion src/Orleans.Serialization.TestKit/CopierTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected CopierTester(ITestOutputHelper output)
protected IServiceProvider ServiceProvider => _serviceProvider;

protected virtual bool IsImmutable => false;
protected virtual bool IsPooled => false;

protected virtual void Configure(ISerializerBuilder builder)
{
Expand Down Expand Up @@ -83,7 +84,7 @@ void Test(TValue original)
[Fact]
public void ReferencesAreAddedToCopyContext()
{
if (typeof(TValue).IsValueType)
if (typeof(TValue).IsValueType || IsPooled)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override void ConvertToSurrogate(ImmutableSortedDictionary<TKey, TValue>
{
surrogate.Values = new(value);
surrogate.KeyComparer = value.KeyComparer != Comparer<TKey>.Default ? value.KeyComparer : null;
surrogate.ValueComparer = value.ValueComparer != EqualityComparer<TKey>.Default ? value.ValueComparer : null;
surrogate.ValueComparer = value.ValueComparer != EqualityComparer<TValue>.Default ? value.ValueComparer : null;
}
}

Expand All @@ -56,6 +56,10 @@ public struct ImmutableSortedDictionarySurrogate<TKey, TValue>
/// <value>The key comparer.</value>
[Id(1)]
public IComparer<TKey> KeyComparer;

/// <summary>
/// Gets or sets the value comparer.
/// </summary>
[Id(2)]
public IEqualityComparer<TValue> ValueComparer;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Orleans.Serialization/Codecs/ImmutableStackCodec.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Orleans.Serialization.Cloning;
using Orleans.Serialization.GeneratedCodeHelpers;
using Orleans.Serialization.Serializers;
Expand All @@ -22,7 +24,7 @@ public ImmutableStackCodec(IValueSerializer<ImmutableStackSurrogate<T>> surrogat
}

/// <inheritdoc/>
public override ImmutableStack<T> ConvertFromSurrogate(ref ImmutableStackSurrogate<T> surrogate) => ImmutableStack.CreateRange(surrogate.Values);
public override ImmutableStack<T> ConvertFromSurrogate(ref ImmutableStackSurrogate<T> surrogate) => ImmutableStack.CreateRange(Enumerable.Reverse(surrogate.Values));

/// <inheritdoc/>
public override void ConvertToSurrogate(ImmutableStack<T> value, ref ImmutableStackSurrogate<T> surrogate) => surrogate.Values = new(value);
Expand Down
2 changes: 1 addition & 1 deletion src/Orleans.Serialization/Codecs/SortedListCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public SortedListCodec(IValueSerializer<SortedListSurrogate<TKey, TValue>> surro
}
else
{
var result = new SortedList<TKey, TValue>(surrogate.Comparer);
var result = new SortedList<TKey, TValue>(surrogate.Values.Count, surrogate.Comparer);
foreach (var kvp in surrogate.Values)
{
result.Add(kvp.Key, kvp.Value);
Expand Down
17 changes: 17 additions & 0 deletions src/Orleans.Serialization/Codecs/ValueTupleCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ public ValueTuple<T> ReadValue<TInput>(ref Reader<TInput> reader, Field field)
}
}

/// <summary>
/// Copier for <see cref="ValueTuple"/>.
/// </summary>
[RegisterCopier]
public sealed class ValueTupleCopier : IDeepCopier<ValueTuple>, IOptionalDeepCopier
{
/// <inheritdoc />
public bool IsShallowCopyable() => true;

/// <inheritdoc />
object IDeepCopier.DeepCopy(object input, CopyContext context) => input;

/// <inheritdoc />
public ValueTuple DeepCopy(ValueTuple input, CopyContext context) => input;
}

/// <summary>
/// Copier for <see cref="ValueTuple{T}"/>.
/// </summary>
Expand All @@ -115,6 +131,7 @@ public sealed class ValueTupleCopier<T> : IDeepCopier<ValueTuple<T>>, IOptionalD
/// <param name="copier">The copier.</param>
public ValueTupleCopier(IDeepCopier<T> copier) => _copier = OrleansGeneratedCodeHelper.GetOptionalCopier(copier);

/// <inheritdoc />
public bool IsShallowCopyable() => _copier is null;

object IDeepCopier.DeepCopy(object input, CopyContext context) => IsShallowCopyable() ? input : DeepCopy((ValueTuple<T>)input, context);
Expand Down
Loading

0 comments on commit eca20f5

Please sign in to comment.