Skip to content

Commit

Permalink
Add acceptance tests for microsoft#960
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Aug 1, 2024
1 parent 7a6977f commit b3f761d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
<MessagePackVersion>2.5.108</MessagePackVersion>
<MessagePackVersion>2.5.172</MessagePackVersion>
<MicroBuildVersion>2.0.162</MicroBuildVersion>
<VisualStudioThreadingVersion>17.10.48</VisualStudioThreadingVersion>
</PropertyGroup>
Expand All @@ -29,7 +29,7 @@
<PackageVersion Include="System.IO.Pipelines" Version="8.0.0" />
<PackageVersion Include="System.IO.Pipes" Version="4.3.0" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Text.Json" Version="8.0.3" />
<PackageVersion Include="System.Text.Json" Version="8.0.4" />
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="6.0.0" />
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
<PackageVersion Include="xunit.combinatorial" Version="1.6.24" />
Expand Down
50 changes: 50 additions & 0 deletions test/StreamJsonRpc.Tests/JsonRpcMessagePackLengthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ internal interface IMessagePackServer
IAsyncEnumerable<UnionBaseClass> GetAsyncEnumerableOfUnionType(CancellationToken cancellationToken);

Task<bool> IsExtensionArgNonNull(CustomExtensionType extensionValue);

Task<string?> GetAPropertyFromStruct([MessagePackFormatter(typeof(ProprietaryStructFormatter))] ProprietaryStruct value, CancellationToken cancellationToken);
}

protected override Type FormatterExceptionType => typeof(MessagePackSerializationException);
Expand Down Expand Up @@ -384,6 +386,23 @@ public async Task VerboseLoggingDoesNotFailWhenArgsDoNotDeserializePrimitively(b
Assert.True(await clientProxy.IsExtensionArgNonNull(new CustomExtensionType()));
}

[Fact]
public async Task FormatterOnParameter_Interface()
{
IMessagePackServer clientProxy = this.clientRpc.Attach<IMessagePackServer>();
const string expected = "Beehive";
string? actual = await clientProxy.GetAPropertyFromStruct(new ProprietaryStruct { A = expected }, this.TimeoutToken);
Assert.Equal(expected, actual);
}

[Fact]
public async Task FormatterOnParameter_NoInterface()
{
const string expected = "Beehive";
string? actual = await this.clientRpc.InvokeWithCancellationAsync<string?>(nameof(MessagePackServer.GetAPropertyFromStructNoInterface), [expected], this.TimeoutToken);
Assert.Equal(expected, actual);
}

protected override void InitializeFormattersAndHandlers(
Stream serverStream,
Stream clientStream,
Expand All @@ -409,6 +428,14 @@ protected override void InitializeFormattersAndHandlers(
: new LengthHeaderMessageHandler(clientStream, clientStream, clientMessageFormatter);
}

/// <summary>
/// A struct that intentionally uses no MessagePack attributes so that a custom formatter will have to be provided.
/// </summary>
internal struct ProprietaryStruct
{
public string? A { get; set; }
}

[MessagePackObject]
[Union(0, typeof(UnionDerivedClass))]
public abstract class UnionBaseClass
Expand Down Expand Up @@ -512,6 +539,16 @@ public async IAsyncEnumerable<UnionBaseClass> GetAsyncEnumerableOfUnionType([Enu
}

public Task<bool> IsExtensionArgNonNull(CustomExtensionType extensionValue) => Task.FromResult(extensionValue is not null);

public Task<string?> GetAPropertyFromStruct(ProprietaryStruct value, CancellationToken cancellationToken)
{
return Task.FromResult(value.A);
}

public Task<string?> GetAPropertyFromStructNoInterface([MessagePackFormatter(typeof(ProprietaryStructFormatter))] ProprietaryStruct value, CancellationToken cancellationToken)
{
return Task.FromResult(value.A);
}
}

private class DelayedFlushingHandler : LengthHeaderMessageHandler, IControlledFlushHandler
Expand All @@ -532,4 +569,17 @@ protected override async ValueTask FlushAsync(CancellationToken cancellationToke
await base.FlushAsync(cancellationToken);
}
}

private class ProprietaryStructFormatter : IMessagePackFormatter<ProprietaryStruct>
{
public ProprietaryStruct Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
return new ProprietaryStruct { A = reader.ReadString() };
}

public void Serialize(ref MessagePackWriter writer, ProprietaryStruct value, MessagePackSerializerOptions options)
{
writer.Write(value.A);
}
}
}

0 comments on commit b3f761d

Please sign in to comment.