Skip to content

Commit

Permalink
Test the .NET 8 build of the library
Browse files Browse the repository at this point in the history
microsoft#1033 added a .NET 8 target to the library. With this change, we also explicitly run tests on it.

Because .NET 8 has removed the `BinaryFormatter`, the tests (which used it as a baseline for testing serializability of types) had to be updated to use a stand-in.
  • Loading branch information
AArnott committed Apr 30, 2024
1 parent 8ffa668 commit 6f4c440
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 12 deletions.
3 changes: 3 additions & 0 deletions test/StreamJsonRpc.Tests/JsonRpcClient20InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,9 @@ public PrivateSerializableException(string message, Exception inner)
{
}

#if NET8_0_OR_GREATER
[Obsolete]
#endif
protected PrivateSerializableException(
SerializationInfo info,
StreamingContext context)
Expand Down
12 changes: 12 additions & 0 deletions test/StreamJsonRpc.Tests/JsonRpcTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4113,6 +4113,9 @@ public LyingException(string message)
{
}

#if NET8_0_OR_GREATER
[Obsolete]
#endif
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
// Arrange to change the ClassName value.
Expand Down Expand Up @@ -4153,12 +4156,18 @@ public ThrowOnSerializeException(string message)
{
}

#if NET8_0_OR_GREATER
[Obsolete]
#endif
protected ThrowOnSerializeException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
// Unlikely to ever be called since serialization throws, but complete the pattern so we test exactly what we mean to.
}

#if NET8_0_OR_GREATER
[Obsolete]
#endif
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
throw new InvalidOperationException("This exception always throws when serialized.");
Expand Down Expand Up @@ -4194,6 +4203,9 @@ public PrivateSerializableException(string message, Exception inner)
{
}

#if NET8_0_OR_GREATER
[Obsolete]
#endif
protected PrivateSerializableException(
SerializationInfo info,
StreamingContext context)
Expand Down
6 changes: 6 additions & 0 deletions test/StreamJsonRpc.Tests/MarshalableProxyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,9 @@ public ExceptionWithAsyncEnumerable(IAsyncEnumerable<int> enumeration)
this.Enumerable = enumeration;
}

#if NET8_0_OR_GREATER
[Obsolete]
#endif
protected ExceptionWithAsyncEnumerable(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Expand All @@ -1508,6 +1511,9 @@ protected ExceptionWithAsyncEnumerable(SerializationInfo info, StreamingContext

internal IAsyncEnumerable<int>? Enumerable { get; set; }

#if NET8_0_OR_GREATER
[Obsolete]
#endif
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
Expand Down
2 changes: 1 addition & 1 deletion test/StreamJsonRpc.Tests/StreamJsonRpc.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('Windows'))">$(TargetFrameworks);net472</TargetFrameworks>
<RootNamespace />
</PropertyGroup>
Expand Down
67 changes: 56 additions & 11 deletions test/StreamJsonRpc.Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
using System.Runtime.Serialization;
using Microsoft.VisualStudio.Threading;
using Xunit;
using Xunit.Abstractions;

public abstract class TestBase : IDisposable
{
Expand Down Expand Up @@ -112,15 +111,22 @@ protected static Task WhenAllSucceedOrAnyFault(params Task[] tasks)
}

protected static T BinaryFormatterRoundtrip<T>(T original)
where T : notnull
where T : notnull, ISerializable
{
var bf = new BinaryFormatter();
var ms = new MemoryStream();
#pragma warning disable SYSLIB0011 // Type or member is obsolete
bf.Serialize(ms, original);
ms.Position = 0;
return (T)bf.Deserialize(ms);
#pragma warning restore SYSLIB0011 // Type or member is obsolete
Assert.NotNull(typeof(T).GetCustomAttribute<SerializableAttribute>());

#pragma warning disable SYSLIB0050
StreamingContext context = new(StreamingContextStates.Remoting);
SerializationInfo info = new SerializationInfo(typeof(T), new RoundtripFormatter());
original.GetObjectData(info, new StreamingContext(StreamingContextStates.Remoting));
#pragma warning restore SYSLIB0050
ConstructorInfo? ctor = typeof(T).GetConstructor(
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
binder: null,
[typeof(SerializationInfo), typeof(StreamingContext)],
modifiers: null);
Assert.NotNull(ctor);
return (T)ctor.Invoke([info, context])!;
}

protected async Task AssertWeakReferenceGetsCollectedAsync(WeakReference weakReference)
Expand Down Expand Up @@ -191,4 +197,43 @@ protected virtual async Task CheckGCPressureAsync(Func<Task> scenario, int maxBy

Assert.True(passingAttemptObserved);
}

#pragma warning disable SYSLIB0050 // Type or member is obsolete
private class RoundtripFormatter : IFormatterConverter
#pragma warning restore SYSLIB0050 // Type or member is obsolete
{
public object Convert(object value, Type type) => value;

public object Convert(object value, TypeCode typeCode) => value;

public bool ToBoolean(object value) => (bool)value;

public byte ToByte(object value) => (byte)value;

public char ToChar(object value) => (char)value;

public DateTime ToDateTime(object value) => (DateTime)value;

public decimal ToDecimal(object value) => (decimal)value;

public double ToDouble(object value) => (double)value;

public short ToInt16(object value) => (short)value;

public int ToInt32(object value) => (int)value;

public long ToInt64(object value) => (long)value;

public sbyte ToSByte(object value) => (sbyte)value;

public float ToSingle(object value) => (float)value;

public string? ToString(object value) => (string)value;

public ushort ToUInt16(object value) => (ushort)value;

public uint ToUInt32(object value) => (uint)value;

public ulong ToUInt64(object value) => (ulong)value;
}
}

0 comments on commit 6f4c440

Please sign in to comment.