Skip to content

Commit

Permalink
Fix settings constructor backward compatibility issue (#214)
Browse files Browse the repository at this point in the history
* Fix .ctor backward compatibility issue

* Add Akka integration spec

* Use common.props for nuget package versions

* Add API Approver spec
  • Loading branch information
Arkatufus authored Apr 19, 2021
1 parent e620a15 commit 8d6bffd
Show file tree
Hide file tree
Showing 10 changed files with 944 additions and 8 deletions.
18 changes: 15 additions & 3 deletions Hyperion.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2005
# Visual Studio Version 16
VisualStudioVersion = 16.0.31112.23
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hyperion", "src\Hyperion\Hyperion.csproj", "{7AF8D2B6-9F1F-4A1C-8673-48E533108385}"
EndProject
Expand All @@ -19,7 +19,11 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Hyperion.Tests.FSharpData",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hyperion.Benchmarks", "src\Hyperion.Benchmarks\Hyperion.Benchmarks.csproj", "{CAE2DB69-0BE7-4B11-96DB-D5B61D35607F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hyperion.Tests", "src\Hyperion.Tests\Hyperion.Tests.csproj", "{EA096BA2-3B4C-4878-B9AD-5A88D164EBF0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hyperion.Tests", "src\Hyperion.Tests\Hyperion.Tests.csproj", "{EA096BA2-3B4C-4878-B9AD-5A88D164EBF0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hyperion.Akka.Integration.Tests", "src\Hyperion.Akka.Integration.Tests\Hyperion.Akka.Integration.Tests.csproj", "{3DA946AF-0CCD-49A9-AA77-67FA515A83EE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hyperion.API.Tests", "src\Hyperion.API.Tests\Hyperion.API.Tests.csproj", "{767FBBD2-869C-4AAD-ABF4-F23EC0D381AA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -43,6 +47,14 @@ Global
{EA096BA2-3B4C-4878-B9AD-5A88D164EBF0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA096BA2-3B4C-4878-B9AD-5A88D164EBF0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA096BA2-3B4C-4878-B9AD-5A88D164EBF0}.Release|Any CPU.Build.0 = Release|Any CPU
{3DA946AF-0CCD-49A9-AA77-67FA515A83EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3DA946AF-0CCD-49A9-AA77-67FA515A83EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3DA946AF-0CCD-49A9-AA77-67FA515A83EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3DA946AF-0CCD-49A9-AA77-67FA515A83EE}.Release|Any CPU.Build.0 = Release|Any CPU
{767FBBD2-869C-4AAD-ABF4-F23EC0D381AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{767FBBD2-869C-4AAD-ABF4-F23EC0D381AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{767FBBD2-869C-4AAD-ABF4-F23EC0D381AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{767FBBD2-869C-4AAD-ABF4-F23EC0D381AA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
40 changes: 40 additions & 0 deletions src/Hyperion.API.Tests/CoreAPISpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using ApprovalTests;
using ApprovalTests.Reporters;
using PublicApiGenerator;
using Xunit;

namespace Hyperion.API.Tests
{
#if(DEBUG)
[UseReporter(typeof(DiffReporter), typeof(AllFailingTestsClipboardReporter))]
#else
[UseReporter(typeof(DiffReporter))]
#endif
public class CoreApiSpec
{
[Fact]
[MethodImpl(MethodImplOptions.NoInlining)]
public void ApproveApi()
{
var publicApi = Filter(typeof(Serializer).Assembly.GeneratePublicApi());
Approvals.Verify(publicApi);
}

static string Filter(string text)
{
return string.Join(Environment.NewLine, text.Split(new[]
{
Environment.NewLine
}, StringSplitOptions.RemoveEmptyEntries)
.Where(l =>
!l.StartsWith("[assembly: ReleaseDateAttribute(")
&& !l.StartsWith("[assembly: System.Security")
&& !l.StartsWith("[assembly: System.Runtime.Versioning.TargetFramework("))
.Where(l => !string.IsNullOrWhiteSpace(l))
);
}
}
}
693 changes: 693 additions & 0 deletions src/Hyperion.API.Tests/CoreApiSpec.ApproveApi.approved.txt

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions src/Hyperion.API.Tests/Hyperion.API.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\common.props" />

<PropertyGroup>
<TargetFrameworks>net461;netcoreapp3.1;net5.0</TargetFrameworks>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ApprovalTests" Version="5.4.7" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
<PackageReference Include="PublicApiGenerator" Version="10.2.0" />
<PackageReference Include="xunit" Version="$(XunitVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitRunnerVersion)">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Hyperion\Hyperion.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\common.props" />

<PropertyGroup>
<TargetFrameworks>net461;netcoreapp3.1;net5.0</TargetFrameworks>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Akka" Version="$(AkkaVersion)" />
<PackageReference Include="Akka.Serialization.Hyperion" Version="$(AkkaVersion)" />
<PackageReference Include="Akka.TestKit.Xunit2" Version="$(AkkaVersion)" />
<PackageReference Include="FluentAssertions" Version="$(FluentAssertionsVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
<PackageReference Include="xunit" Version="$(XunitVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitRunnerVersion)">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Hyperion\Hyperion.csproj" />
</ItemGroup>

</Project>
69 changes: 69 additions & 0 deletions src/Hyperion.Akka.Integration.Tests/IntegrationSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using Akka.Actor;
using Akka.Configuration;
using Akka.Serialization;
using Xunit;
using Akka.TestKit;
using Akka.TestKit.Xunit2;
using FluentAssertions;
using Xunit.Abstractions;
using AkkaSerializer = Akka.Serialization.Serializer;

namespace Hyperion.Akka.Integration.Tests
{
public class IntegrationSpec : TestKit
{
private static readonly Config Config = ConfigurationFactory.ParseString(@"
akka {
loglevel = WARNING
stdout-loglevel = WARNING
serialize-messages = on
actor {
serializers {
hyperion = ""Akka.Serialization.HyperionSerializer, Akka.Serialization.Hyperion""
}
serialization-bindings {
""System.Object"" = hyperion
}
}
}
# use random ports to avoid race conditions with binding contention
akka.remote.dot-netty.tcp.port = 0");

private readonly AkkaSerializer _serializer;

public IntegrationSpec(ITestOutputHelper output)
: base(Config, nameof(IntegrationSpec), output)
{
_serializer = Sys.Serialization.FindSerializerForType(typeof(object));
}

[Fact]
public void Akka_should_load_Hyperion_correctly()
{
_serializer.Should().BeOfType<HyperionSerializer>();
}

[Fact]
public void Akka_HyperionSerializer_should_serialize_properly()
{
var myObject = new MyPocoClass
{
Name = "John Doe",
Count = 24
};

var serialized = _serializer.ToBinary(myObject);
var deserialized = _serializer.FromBinary<MyPocoClass>(serialized);

deserialized.Name.Should().Be("John Doe");
deserialized.Count.Should().Be(24);
}

private class MyPocoClass
{
public string Name { get; set; }
public int Count { get; set; }
}
}
}
4 changes: 2 additions & 2 deletions src/Hyperion.Tests/CrossFrameworkSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public CrossFrameworkSerializationTests(ITestOutputHelper log)
// to convert netcore System.Drawing.Primitives to netfx
// System.Drawing package.
#if NETFX
_serializer = new Serializer(new SerializerOptions(
packageNameOverrides: new List<Func<string, string>>
_serializer = new Serializer(SerializerOptions.Default.WithPackageNameOverrides(
new List<Func<string, string>>
{
str => str.Contains("System.Drawing.Primitives") ? str.Replace(".Primitives", "") : str
}));
Expand Down
2 changes: 1 addition & 1 deletion src/Hyperion.Tests/Hyperion.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="FluentAssertions" Version="$(FluentAssertionsVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
<PackageReference Include="xunit" Version="$(XunitVersion)" />
Expand Down
63 changes: 61 additions & 2 deletions src/Hyperion/SerializerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ namespace Hyperion
{
public class SerializerOptions
{
public static readonly SerializerOptions Default = new SerializerOptions(
versionTolerance: false,
preserveObjectReferences: false,
surrogates: null,
serializerFactories: null,
knownTypes: null,
ignoreISerializable: false,
packageNameOverrides: null);

internal static List<Func<string, string>> DefaultPackageNameOverrides()
{
return new List<Func<string, string>>
Expand All @@ -33,7 +42,6 @@ internal static List<Func<string, string>> DefaultPackageNameOverrides()
}

internal static readonly Surrogate[] EmptySurrogates = new Surrogate[0];


private static readonly ValueSerializerFactory[] DefaultValueSerializerFactories =
{
Expand Down Expand Up @@ -72,7 +80,25 @@ internal static List<Func<string, string>> DefaultPackageNameOverrides()
internal readonly List<Func<string, string>> CrossFrameworkPackageNameOverrides =
DefaultPackageNameOverrides();

public SerializerOptions(bool versionTolerance = false, bool preserveObjectReferences = false, IEnumerable<Surrogate> surrogates = null, IEnumerable<ValueSerializerFactory> serializerFactories = null, IEnumerable<Type> knownTypes = null, bool ignoreISerializable = false, IEnumerable<Func<string, string>> packageNameOverrides = null)
[Obsolete]
public SerializerOptions(
bool versionTolerance = false,
bool preserveObjectReferences = false,
IEnumerable<Surrogate> surrogates = null,
IEnumerable<ValueSerializerFactory> serializerFactories = null,
IEnumerable<Type> knownTypes = null,
bool ignoreISerializable = false)
: this(versionTolerance, preserveObjectReferences, surrogates, serializerFactories, knownTypes, ignoreISerializable, null)
{ }

public SerializerOptions(
bool versionTolerance,
bool preserveObjectReferences,
IEnumerable<Surrogate> surrogates,
IEnumerable<ValueSerializerFactory> serializerFactories,
IEnumerable<Type> knownTypes,
bool ignoreISerializable,
IEnumerable<Func<string, string>> packageNameOverrides)
{
VersionTolerance = versionTolerance;
Surrogates = surrogates?.ToArray() ?? EmptySurrogates;
Expand All @@ -94,5 +120,38 @@ public SerializerOptions(bool versionTolerance = false, bool preserveObjectRefer
if(packageNameOverrides != null)
CrossFrameworkPackageNameOverrides.AddRange(packageNameOverrides);
}

public SerializerOptions WithVersionTolerance(bool versionTolerance)
=> Copy(versionTolerance: versionTolerance);
public SerializerOptions WithPreserveObjectReferences(bool preserveObjectReferences)
=> Copy(preserveObjectReferences: preserveObjectReferences);
public SerializerOptions WithSurrogates(IEnumerable<Surrogate> surrogates)
=> Copy(surrogates: surrogates);
public SerializerOptions WithSerializerFactory(IEnumerable<ValueSerializerFactory> serializerFactories)
=> Copy(serializerFactories: serializerFactories);
public SerializerOptions WithKnownTypes(IEnumerable<Type> knownTypes)
=> Copy(knownTypes: knownTypes);
public SerializerOptions WithIgnoreSerializable(bool ignoreISerializable)
=> Copy(ignoreISerializable: ignoreISerializable);
public SerializerOptions WithPackageNameOverrides(IEnumerable<Func<string, string>> packageNameOverrides)
=> Copy(packageNameOverrides: packageNameOverrides);

private SerializerOptions Copy(
bool? versionTolerance = null,
bool? preserveObjectReferences = null,
IEnumerable<Surrogate> surrogates = null,
IEnumerable<ValueSerializerFactory> serializerFactories = null,
IEnumerable<Type> knownTypes = null,
bool? ignoreISerializable = null,
IEnumerable<Func<string, string>> packageNameOverrides = null)
=> new SerializerOptions(
versionTolerance ?? VersionTolerance,
preserveObjectReferences ?? PreserveObjectReferences,
surrogates ?? Surrogates,
serializerFactories ?? ValueSerializerFactories,
knownTypes ?? KnownTypes,
ignoreISerializable ?? IgnoreISerializable,
packageNameOverrides ?? CrossFrameworkPackageNameOverrides
);
}
}
3 changes: 3 additions & 0 deletions src/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>
<PropertyGroup>
<AkkaVersion>1.4.18</AkkaVersion>

<FluentAssertionsVersion>5.10.3</FluentAssertionsVersion>
<XunitVersion>2.4.1</XunitVersion>
<XunitRunnerVersion>2.4.3</XunitRunnerVersion>
<TestSdkVersion>16.9.4</TestSdkVersion>
Expand Down

0 comments on commit 8d6bffd

Please sign in to comment.