-
-
Notifications
You must be signed in to change notification settings - Fork 432
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 #884 from Cysharp/feature/HandleUnimplementedHubMe…
…thod Handling when calling a non-implemented hub method
- Loading branch information
Showing
6 changed files
with
80 additions
and
29 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
29 changes: 29 additions & 0 deletions
29
src/MagicOnion.Server/Hubs/Internal/StreamingHubPayloadBuilder.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,29 @@ | ||
using MagicOnion.Internal; | ||
using MagicOnion.Internal.Buffers; | ||
using MagicOnion.Serialization; | ||
|
||
namespace MagicOnion.Server.Hubs.Internal; | ||
|
||
internal static class StreamingHubPayloadBuilder | ||
{ | ||
public static StreamingHubPayload Build(int methodId, int messageId) | ||
{ | ||
using var buffer = ArrayPoolBufferWriter.RentThreadStaticWriter(); | ||
StreamingHubMessageWriter.WriteResponseMessage(buffer, methodId, messageId); | ||
return StreamingHubPayloadPool.Shared.RentOrCreate(buffer.WrittenSpan); | ||
} | ||
|
||
public static StreamingHubPayload Build<T>(int methodId, int messageId, T v, IMagicOnionSerializer messageSerializer) | ||
{ | ||
using var buffer = ArrayPoolBufferWriter.RentThreadStaticWriter(); | ||
StreamingHubMessageWriter.WriteResponseMessage(buffer, methodId, messageId, v, messageSerializer); | ||
return StreamingHubPayloadPool.Shared.RentOrCreate(buffer.WrittenSpan); | ||
} | ||
|
||
public static StreamingHubPayload BuildError(int messageId, int statusCode, string detail, Exception? ex, bool isReturnExceptionStackTraceInErrorDetail) | ||
{ | ||
using var buffer = ArrayPoolBufferWriter.RentThreadStaticWriter(); | ||
StreamingHubMessageWriter.WriteResponseMessageForError(buffer, messageId, statusCode, detail, ex, isReturnExceptionStackTraceInErrorDetail); | ||
return StreamingHubPayloadPool.Shared.RentOrCreate(buffer.WrittenSpan); | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
tests/MagicOnion.Integration.Tests/StreamingHubTest.UnknownMethodId.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,39 @@ | ||
using Grpc.Net.Client; | ||
using MagicOnion.Server.Hubs; | ||
|
||
namespace MagicOnion.Integration.Tests | ||
{ | ||
public partial class StreamingHubTest | ||
{ | ||
[Theory] | ||
[MemberData(nameof(EnumerateStreamingHubClientFactory))] | ||
public async Task UnknownMethodId(TestStreamingHubClientFactory clientFactory) | ||
{ | ||
// Arrange | ||
var httpClient = factory.CreateDefaultClient(); | ||
var channel = GrpcChannel.ForAddress("http://localhost", new GrpcChannelOptions() { HttpClient = httpClient }); | ||
|
||
var receiver = Substitute.For<IStreamingHubTestHubReceiver>(); | ||
var client = await clientFactory.CreateAndConnectAsync<MagicOnion.Integration.Tests.UnknownMethodIdTest.IStreamingHubTestHub, IStreamingHubTestHubReceiver>(channel, receiver); | ||
|
||
// Act | ||
var ex = await Record.ExceptionAsync(async () => await client.CustomMethodId().WaitAsync(TimeSpan.FromSeconds(1))); | ||
|
||
// Assert | ||
ex.Should().NotBeNull(); | ||
var rpcException = ex.Should().BeOfType<RpcException>().Subject; | ||
rpcException.Status.StatusCode.Should().Be(StatusCode.Unimplemented); | ||
factory.Logs.Should().Contain(x => x.Contains("HubMethodNotFound\tStreamingHub method '-1'")); | ||
} | ||
} | ||
|
||
} | ||
|
||
namespace MagicOnion.Integration.Tests.UnknownMethodIdTest | ||
{ | ||
public interface IStreamingHubTestHub : IStreamingHub<IStreamingHubTestHub, IStreamingHubTestHubReceiver> | ||
{ | ||
[MethodId(-1)] | ||
Task CustomMethodId(); | ||
} | ||
} |
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