-
-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #730 from Cysharp/feature/net8.0
Adopt to .NET 8
- Loading branch information
Showing
30 changed files
with
586 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
perf/BenchmarkApp/PerformanceTest.Server/PerformanceTest.Server.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/MagicOnion.Abstractions.Tests/MagicOnion.Abstractions.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
tests/MagicOnion.Client.Tests/ChannelClientStreamWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
tests/MagicOnion.Client.Tests/StreamingHubClientTestHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.