Skip to content

Commit

Permalink
Merge pull request #730 from Cysharp/feature/net8.0
Browse files Browse the repository at this point in the history
Adopt to .NET 8
  • Loading branch information
mayuki authored Jan 11, 2024
2 parents 290da51 + ba58c8f commit 8004edb
Show file tree
Hide file tree
Showing 30 changed files with 586 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DefineConstants>CLIENT;$(DefineConstants)</DefineConstants>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DefineConstants>SERVER;$(DefineConstants)</DefineConstants>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion samples/ChatApp/ChatApp.Server/ChatApp.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ static void DefineMethods(TypeBuilder typeBuilder, Type interfaceType, Type rece
{
il.Emit(OpCodes.Newobj, typeof(ValueTask).GetConstructor(new [] { typeof(Task) })!);
}
else if (def.MethodInfo.IsGenericMethod && def.MethodInfo.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>))
else if (def.MethodInfo.ReturnType.IsGenericType && def.MethodInfo.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>))
{
var returnTypeOfT = def.MethodInfo.ReturnType.GetGenericArguments()[0];
il.Emit(OpCodes.Newobj, typeof(ValueTask<>).MakeGenericType(returnTypeOfT).GetConstructor(new [] { typeof(Task<>).MakeGenericType(returnTypeOfT) })!);
Expand Down Expand Up @@ -533,7 +533,7 @@ static void DefineMethodsFireAndForget(TypeBuilder typeBuilder, Type interfaceTy
{
il.Emit(OpCodes.Newobj, typeof(ValueTask).GetConstructor(new [] { typeof(Task) })!);
}
else if (def.MethodInfo.IsGenericMethod && def.MethodInfo.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>))
else if (def.MethodInfo.ReturnType.IsGenericType && def.MethodInfo.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>))
{
var returnTypeOfT = def.MethodInfo.ReturnType.GetGenericArguments()[0];
il.Emit(OpCodes.Newobj, typeof(ValueTask<>).MakeGenericType(returnTypeOfT).GetConstructor(new [] { typeof(Task<>).MakeGenericType(returnTypeOfT) })!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ static void DefineMethods(TypeBuilder typeBuilder, Type interfaceType, Type rece
{
il.Emit(OpCodes.Newobj, typeof(ValueTask).GetConstructor(new [] { typeof(Task) })!);
}
else if (def.MethodInfo.IsGenericMethod && def.MethodInfo.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>))
else if (def.MethodInfo.ReturnType.IsGenericType && def.MethodInfo.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>))
{
var returnTypeOfT = def.MethodInfo.ReturnType.GetGenericArguments()[0];
il.Emit(OpCodes.Newobj, typeof(ValueTask<>).MakeGenericType(returnTypeOfT).GetConstructor(new [] { typeof(Task<>).MakeGenericType(returnTypeOfT) })!);
Expand Down Expand Up @@ -533,7 +533,7 @@ static void DefineMethodsFireAndForget(TypeBuilder typeBuilder, Type interfaceTy
{
il.Emit(OpCodes.Newobj, typeof(ValueTask).GetConstructor(new [] { typeof(Task) })!);
}
else if (def.MethodInfo.IsGenericMethod && def.MethodInfo.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>))
else if (def.MethodInfo.ReturnType.IsGenericType && def.MethodInfo.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>))
{
var returnTypeOfT = def.MethodInfo.ReturnType.GetGenericArguments()[0];
il.Emit(OpCodes.Newobj, typeof(ValueTask<>).MakeGenericType(returnTypeOfT).GetConstructor(new [] { typeof(Task<>).MakeGenericType(returnTypeOfT) })!);
Expand Down
2 changes: 1 addition & 1 deletion src/MagicOnion.Client/MagicOnion.Client.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>

<LangVersion>$(_LangVersionUnityBaseline)</LangVersion>
<Nullable>enable</Nullable>
Expand Down
2 changes: 1 addition & 1 deletion src/MagicOnion.Internal/MagicOnion.Internal.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>

<LangVersion>$(_LangVersionUnityBaseline)</LangVersion>
<Nullable>enable</Nullable>
Expand Down
2 changes: 1 addition & 1 deletion src/MagicOnion.Server/MagicOnion.Server.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>

<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
2 changes: 1 addition & 1 deletion src/MagicOnion.Shared/MagicOnion.Shared.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>

<LangVersion>$(_LangVersionUnityBaseline)</LangVersion>
<Nullable>enable</Nullable>
Expand Down
2 changes: 1 addition & 1 deletion src/MagicOnion/MagicOnion.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<OutputType>Library</OutputType>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
29 changes: 29 additions & 0 deletions tests/MagicOnion.Client.Tests/ChannelAsyncStreamReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Threading.Channels;

namespace MagicOnion.Client.Tests;

class ChannelAsyncStreamReader<T> : IAsyncStreamReader<T>
{
readonly ChannelReader<T> reader;

public T Current { get; private set; } = default!;

public ChannelAsyncStreamReader(Channel<T> channel)
{
reader = channel.Reader;
}

public async Task<bool> MoveNext(CancellationToken cancellationToken)
{
if (await reader.WaitToReadAsync(cancellationToken))
{
if (reader.TryRead(out var item))
{
Current = item;
return true;
}
}

return false;
}
}
27 changes: 27 additions & 0 deletions tests/MagicOnion.Client.Tests/ChannelClientStreamWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Threading.Channels;

namespace MagicOnion.Client.Tests;

class ChannelClientStreamWriter<T> : IClientStreamWriter<T>
{
readonly ChannelWriter<T> writer;

public WriteOptions? WriteOptions { get; set; }

public ChannelClientStreamWriter(ChannelWriter<T> writer)
{
this.writer = writer;
}

public Task CompleteAsync()
{
writer.Complete();
return Task.CompletedTask;
}

public Task WriteAsync(T message)
{
writer.TryWrite(message);
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
Expand Down
111 changes: 111 additions & 0 deletions tests/MagicOnion.Client.Tests/StreamingHubClientTestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System.Buffers;
using System.Diagnostics;
using System.Threading.Channels;

namespace MagicOnion.Client.Tests;

class StreamingHubClientTestHelper<TStreamingHub, TReceiver>
where TStreamingHub : IStreamingHub<TStreamingHub, TReceiver>
where TReceiver : class
{
readonly Channel<byte[]> requestChannel;
readonly Channel<byte[]> responseChannel;
readonly CallInvoker callInvokerMock;
readonly TReceiver receiver;
readonly IStreamingHubClientFactoryProvider? factoryProvider;

public TReceiver Receiver => receiver;
public CallInvoker CallInvoker => callInvokerMock;

public StreamingHubClientTestHelper(IStreamingHubClientFactoryProvider? factoryProvider = null)
{
requestChannel = Channel.CreateUnbounded<byte[]>();
var requestStream = new ChannelClientStreamWriter<byte[]>(requestChannel);
responseChannel = Channel.CreateUnbounded<byte[]>();
var responseStream = new ChannelAsyncStreamReader<byte[]>(responseChannel);

this.factoryProvider = factoryProvider;

callInvokerMock = Substitute.For<CallInvoker>();
callInvokerMock.AsyncDuplexStreamingCall(default(Method<byte[], byte[]>)!, default, default)
.ReturnsForAnyArgs(x =>
{
return new AsyncDuplexStreamingCall<byte[], byte[]>(
requestStream,
responseStream,
_ => Task.FromResult(new Metadata { { "x-magiconion-streaminghub-version", "2" } }),
_ => Status.DefaultSuccess,
_ => Metadata.Empty,
_ => { },
new object());
});

receiver = Substitute.For<TReceiver>();
}

public static async Task<(StreamingHubClientTestHelper<TStreamingHub, TReceiver> Helper, TStreamingHub Client)> CreateAndConnectAsync(CancellationToken cancellationToken = default)
{
var helper = new StreamingHubClientTestHelper<TStreamingHub, TReceiver>();
return (helper, await helper.ConnectAsync(cancellationToken));
}

public async Task<TStreamingHub> ConnectAsync(CancellationToken cancellationToken = default)
{
return await StreamingHubClient.ConnectAsync<TStreamingHub, TReceiver>(
callInvokerMock,
receiver,
cancellationToken: cancellationToken,
factoryProvider: factoryProvider
);
}

public async Task<(int MessageId, int MethodId, T Requst)> ReadRequestAsync<T>()
{
var requestPayload = await requestChannel.Reader.ReadAsync();
return ReadRequestPayload<T>(requestPayload);
}

public async Task<(int MethodId, T Requst)> ReadFireAndForgetRequestAsync<T>()
{
var requestPayload = await requestChannel.Reader.ReadAsync();
return ReadFireAndForgetRequestPayload<T>(requestPayload);
}

public void WriteResponse<T>(int messageId, int methodId, T response)
{
responseChannel.Writer.TryWrite(BuildResponsePayload(messageId, methodId, response));
}

static byte[] BuildResponsePayload<T>(int messageId, int methodId, T response)
{
var bufferWriter = new ArrayBufferWriter<byte>();
var messagePackWriter = new MessagePackWriter(bufferWriter);
messagePackWriter.WriteArrayHeader(3);
messagePackWriter.Write(messageId);
messagePackWriter.Write(methodId);
MessagePackSerializer.Serialize(ref messagePackWriter, response);
messagePackWriter.Flush();
return bufferWriter.WrittenSpan.ToArray();
}

static (int MessageId, int MethodId, T Body) ReadRequestPayload<T>(ReadOnlyMemory<byte> payload)
{
// Array[3][messageId (int), methodId (int), request body...]
var messagePackReader = new MessagePackReader(payload);
var arraySize = messagePackReader.ReadArrayHeader();
Debug.Assert(arraySize == 3);
var messageId = messagePackReader.ReadInt32();
var methodId = messagePackReader.ReadInt32();
return (messageId, methodId, MessagePackSerializer.Deserialize<T>(ref messagePackReader));
}

static (int MethodId, T Body) ReadFireAndForgetRequestPayload<T>(ReadOnlyMemory<byte> payload)
{
// Array[2][methodId (int), request body...]
var messagePackReader = new MessagePackReader(payload);
var arraySize = messagePackReader.ReadArrayHeader();
Debug.Assert(arraySize == 2);
var methodId = messagePackReader.ReadInt32();
return (methodId, MessagePackSerializer.Deserialize<T>(ref messagePackReader));
}
}
Loading

0 comments on commit 8004edb

Please sign in to comment.