Skip to content

Commit

Permalink
Add more tests for [Alias("x")] attribute (#8926)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenBond committed Apr 9, 2024
1 parent 2960a3b commit 50eba30
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
Expand Up @@ -6,7 +6,6 @@
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO.Pipelines;
Expand Down
Expand Up @@ -90,7 +90,7 @@ public interface IProxyAliasTestGrain
ValueTask OtherMethod();
}

[GenerateMethodSerializers(typeof(MyInvokableProxyBase))]
[Alias("test.IGenericProxyAliasTestGrain`3"), GenerateMethodSerializers(typeof(MyInvokableProxyBase))]
public interface IGenericProxyAliasTestGrain<T, U, V>
{
[Id(777)]
Expand Down
2 changes: 1 addition & 1 deletion test/Orleans.Serialization.UnitTests/Models.cs
Expand Up @@ -7,7 +7,7 @@
using Newtonsoft.Json;
using Orleans;

[GenerateSerializer]
[Alias("test.person.alias"), GenerateSerializer]
public record Person([property: Id(0)] int Age, [property: Id(1)] string Name)
{
[Id(2)]
Expand Down
18 changes: 17 additions & 1 deletion test/Orleans.Serialization.UnitTests/TypeEncodingTests.cs
Expand Up @@ -91,7 +91,7 @@ public void GeneratedProxyClassesHaveExpectedCompoundTypeNames_Generic()
Assert.True(res.IsCompletedSuccessfully);
var method = calls.Dequeue();
var (payload, bitStream) = SerializePayload(method);
var expectedString = "(\"inv\",[_my_proxy_base_],[Orleans.Serialization.UnitTests.IGenericProxyAliasTestGrain`3,Orleans.Serialization.UnitTests],\"777\")`6[[int],[string],[double],[int],[_custom_type_alias_],[int]]";
var expectedString = "(\"inv\",[_my_proxy_base_],[test.IGenericProxyAliasTestGrain`3],\"777\")`6[[int],[string],[double],[int],[_custom_type_alias_],[int]]";
var expectedEncoding = Encoding.UTF8.GetBytes(expectedString).AsSpan();
Assert.True(payload.AsSpan().IndexOf(expectedEncoding) >= 0, $"Expected to find string \"{expectedString}\" in bitstream (formatted: {bitStream})");
}
Expand All @@ -103,6 +103,22 @@ public void GeneratedProxyClassesHaveExpectedCompoundTypeNames_Generic()
var bitStream = BitStreamFormatter.Format(payload, session);
return (payload, bitStream);
}

[Fact]
public void AliasAttributeIsApplied()
{
var original = new Person(2, "harry");

var bytes = _serializer.SerializeToArray(original.GetType());
var resultType = _serializer.Deserialize<Type>(bytes);

var alias = original.GetType().GetCustomAttributes(false).OfType<AliasAttribute>().SingleOrDefault();
Assert.NotNull(alias);
Assert.NotNull(alias.Alias);
Assert.Equal(typeof(Person), resultType);
var expectedBytes = Encoding.UTF8.GetBytes(alias.Alias);
Assert.True(bytes.AsSpan().IndexOf(expectedBytes) >= 0);
}
}

[Alias("_custom_type_alias_")]
Expand Down

0 comments on commit 50eba30

Please sign in to comment.