Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class GreetingService(IApiClient ApiClient)

[NexusOperationHandler]
public IOperationHandler<string, string> SayHello2() =>
// Advanced, potentially asynchronous operations can
// Advanced, potentially asynchronous operations
new SayHello2Handler(ApiClient);

public class SayHello2Handler(IApiClient ApiClient) : IOperationHandler<string, string>
Expand All @@ -86,12 +86,6 @@ public class GreetingService(IApiClient ApiClient)
OperationStartContext context, string name) =>
throw new NotImplementedException("Excluded for brevity");

public Task<string> FetchResultAsync(OperationFetchResultContext context) =>
throw new NotImplementedException("Excluded for brevity");

public Task<OperationInfo> FetchInfoAsync(OperationFetchInfoContext context) =>
throw new NotImplementedException("Excluded for brevity");

public Task CancelAsync(OperationCancelContext context) =>
throw new NotImplementedException("Excluded for brevity");
}
Expand Down Expand Up @@ -141,4 +135,4 @@ To help show full stdout/stderr, this is also available as an in-proc test progr

Extra args can be added after `--`, e.g. `-- -verbose` would show verbose logs and `-- --help` would show other
options. If the arguments are anything but `--help`, the current assembly is prepended to the args before sending to the
xUnit runner.
xUnit runner.
33 changes: 0 additions & 33 deletions src/NexusRpc/Handlers/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,39 +96,6 @@ public async Task<OperationStartResult<HandlerContent>> StartOperationAsync(
new HandlerContent(resultContent.Data, resultContent.Headers));
}

/// <inheritdoc/>
public async Task<HandlerContent> FetchOperationResultAsync(OperationFetchResultContext context)
{
// Get handler
var instance = GetInstance(context);
var handler = GetInterceptedHandler(context, instance);
var opDef = instance.Definition.Operations[context.Operation];

// Fetch result
var result = await handler.FetchResultAsync(context).ConfigureAwait(false);

// Change sync result value to NoValue if return type is void
if (opDef.OutputType == typeof(void))
{
result = default(NoValue);
}

// Serialize
var resultContent = await serializer.SerializeAsync(result).ConfigureAwait(false);
return new(resultContent.Data, resultContent.Headers);
}

/// <inheritdoc/>
public Task<OperationInfo> FetchOperationInfoAsync(OperationFetchInfoContext context)
{
// Get handler
var instance = GetInstance(context);
var handler = GetInterceptedHandler(context, instance);

// Return info
return handler.FetchInfoAsync(context);
}

/// <inheritdoc/>
public Task CancelOperationAsync(OperationCancelContext context)
{
Expand Down
19 changes: 0 additions & 19 deletions src/NexusRpc/Handlers/IHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,6 @@ Task<OperationStartResult<HandlerContent>> StartOperationAsync(
OperationStartContext context,
HandlerContent input);

/// <summary>
/// Fetch operation result, waiting if necessary.
/// </summary>
/// <param name="context">Context.</param>
/// <returns>Task with result contents.</returns>
/// <exception cref="OperationStillRunningException">Operation still running (after optional
/// wait).</exception>
/// <exception cref="OperationException">Operation failed.</exception>
/// <exception cref="HandlerException">Unexpected handler failure.</exception>
Task<HandlerContent> FetchOperationResultAsync(OperationFetchResultContext context);

/// <summary>
/// Fetch operation info.
/// </summary>
/// <param name="context">Context.</param>
/// <returns>Task with operation info.</returns>
/// <exception cref="HandlerException">Unexpected handler failure.</exception>
Task<OperationInfo> FetchOperationInfoAsync(OperationFetchInfoContext context);

/// <summary>
/// Request operation cancel.
/// </summary>
Expand Down
19 changes: 0 additions & 19 deletions src/NexusRpc/Handlers/IOperationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,6 @@ Task<OperationStartResult<TResult>> StartAsync(
OperationStartContext context,
TInput input);

/// <summary>
/// Fetch operation result, waiting if necessary.
/// </summary>
/// <param name="context">Context.</param>
/// <returns>Task with result contents.</returns>
/// <exception cref="OperationStillRunningException">Operation still running (after optional
/// wait).</exception>
/// <exception cref="OperationException">Operation failed.</exception>
/// <exception cref="HandlerException">Unexpected handler failure.</exception>
Task<TResult> FetchResultAsync(OperationFetchResultContext context);

/// <summary>
/// Fetch operation info.
/// </summary>
/// <param name="context">Context.</param>
/// <returns>Task with operation info.</returns>
/// <exception cref="HandlerException">Unexpected handler failure.</exception>
Task<OperationInfo> FetchInfoAsync(OperationFetchInfoContext context);

/// <summary>
/// Request operation cancel.
/// </summary>
Expand Down
20 changes: 0 additions & 20 deletions src/NexusRpc/Handlers/OperationFetchInfoContext.cs

This file was deleted.

34 changes: 0 additions & 34 deletions src/NexusRpc/Handlers/OperationFetchResultContext.cs

This file was deleted.

12 changes: 0 additions & 12 deletions src/NexusRpc/Handlers/OperationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ public async Task<OperationStartResult<TResult>> StartAsync(
return OperationStartResult.SyncResult(result);
}

public Task<TResult> FetchResultAsync(OperationFetchResultContext context) =>
throw new NotImplementedException("Not supported on sync operation");

public Task<OperationInfo> FetchInfoAsync(OperationFetchInfoContext context) =>
throw new NotImplementedException("Not supported on sync operation");

public Task CancelAsync(OperationCancelContext context) =>
throw new NotImplementedException("Not supported on sync operation");
}
Expand All @@ -119,12 +113,6 @@ public GenericOperationHandler(IOperationHandler<TInput, TResult> underlying) =>
return new(result.SyncResultValue, result.AsyncOperationToken);
}

public async Task<object?> FetchResultAsync(OperationFetchResultContext context) =>
await Underlying.FetchResultAsync(context).ConfigureAwait(false);

public Task<OperationInfo> FetchInfoAsync(OperationFetchInfoContext context) =>
Underlying.FetchInfoAsync(context);

public Task CancelAsync(OperationCancelContext context) =>
Underlying.CancelAsync(context);
}
Expand Down
9 changes: 0 additions & 9 deletions src/NexusRpc/OperationInfo.cs

This file was deleted.

17 changes: 0 additions & 17 deletions src/NexusRpc/OperationStillRunningException.cs

This file was deleted.

89 changes: 7 additions & 82 deletions tests/NexusRpc.Tests/Handlers/HandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,7 @@ public async Task Handler_SyncResult_Works()
"\"Hello, some-name\"",
Encoding.UTF8.GetString(result.SyncResultValue!.ConsumeAllBytes()));

// Ensure other operation calls fail
await Assert.ThrowsAsync<NotImplementedException>(() =>
handler.FetchOperationResultAsync(new(
Service: "SimpleService",
Operation: "SayHello",
CancellationToken: default,
OperationToken: Guid.NewGuid().ToString())));
await Assert.ThrowsAsync<NotImplementedException>(() =>
handler.FetchOperationInfoAsync(new(
Service: "SimpleService",
Operation: "SayHello",
CancellationToken: default,
OperationToken: Guid.NewGuid().ToString())));
// Ensure cancel operation call fails
await Assert.ThrowsAsync<NotImplementedException>(() =>
handler.CancelOperationAsync(new(
Service: "SimpleService",
Expand All @@ -88,19 +76,6 @@ public async Task<OperationStartResult<string>> StartAsync(
return OperationStartResult.AsyncResult<string>($"{input}-token");
}

public async Task<string> FetchResultAsync(OperationFetchResultContext context)
{
Calls.Add(context);
var name = context.OperationToken.Substring(0, context.OperationToken.Length - "-token".Length);
return $"Hello, {name}";
}

public async Task<OperationInfo> FetchInfoAsync(OperationFetchInfoContext context)
{
Calls.Add(context);
return new(context.OperationToken, OperationState.Running);
}

public async Task CancelAsync(OperationCancelContext context) =>
Calls.Add(context);
}
Expand All @@ -125,18 +100,6 @@ private class TrackingHandler(
return Next.StartAsync(context, input);
}

public Task<object?> FetchResultAsync(OperationFetchResultContext context)
{
Calls.Add(context);
return Next.FetchResultAsync(context);
}

public Task<OperationInfo> FetchInfoAsync(OperationFetchInfoContext context)
{
Calls.Add(context);
return Next.FetchInfoAsync(context);
}

public Task CancelAsync(OperationCancelContext context)
{
Calls.Add(context);
Expand Down Expand Up @@ -171,7 +134,7 @@ public async Task Handler_AsyncResultAndMiddleware_Works()
new InterceptCallTrackingMiddleware("first", interceptCalls),
]);

// Make the 4 calls
// Make the 2 calls
var startResult = await handler.StartOperationAsync(
new(
Service: "SimpleService",
Expand All @@ -182,21 +145,6 @@ public async Task Handler_AsyncResultAndMiddleware_Works()
Assert.Null(startResult.SyncResultValue);
Assert.NotNull(startResult.AsyncOperationToken);
Assert.Equal(["first", "second"], interceptCalls);
var result = await handler.FetchOperationResultAsync(new(
Service: "SimpleService",
Operation: "SayHello",
CancellationToken: default,
OperationToken: startResult.AsyncOperationToken));
Assert.Equal(
"\"Hello, some-name\"",
Encoding.UTF8.GetString(result.ConsumeAllBytes()));
var infoResult = await handler.FetchOperationInfoAsync(new(
Service: "SimpleService",
Operation: "SayHello",
CancellationToken: default,
OperationToken: startResult.AsyncOperationToken));
Assert.Equal(startResult.AsyncOperationToken, infoResult.Token);
Assert.Equal(OperationState.Running, infoResult.State);
await handler.CancelOperationAsync(new(
Service: "SimpleService",
Operation: "SayHello",
Expand All @@ -206,11 +154,10 @@ await handler.CancelOperationAsync(new(
// Confirm contexts
void AssertCalls(ICollection<OperationContext> calls)
{
Assert.Equal(4, calls.Count);
Assert.Equal(2, calls.Count);
Assert.Equal(
[
typeof(OperationStartContext), typeof(OperationFetchResultContext),
typeof(OperationFetchInfoContext), typeof(OperationCancelContext),
typeof(OperationStartContext), typeof(OperationCancelContext),
],
service.Calls.Select(c => c.GetType()).ToList());
Assert.All(calls, call => Assert.Equal("SimpleService", call.Service));
Expand All @@ -234,7 +181,7 @@ public async Task Handler_ManuallyCreated_Works()
});
var handler = new Handler([instance], new NexusJsonSerializer());

// Start and check result
// Start operation
var startResult = await handler.StartOperationAsync(
new(
Service: "ManualService",
Expand All @@ -243,14 +190,6 @@ public async Task Handler_ManuallyCreated_Works()
RequestId: Guid.NewGuid().ToString()),
new(Encoding.UTF8.GetBytes("\"some-name\"")));
Assert.NotNull(startResult.AsyncOperationToken);
var result = await handler.FetchOperationResultAsync(new(
Service: "ManualService",
Operation: "ManualSayHello",
CancellationToken: default,
OperationToken: startResult.AsyncOperationToken));
Assert.Equal(
"\"Hello, some-name\"",
Encoding.UTF8.GetString(result.ConsumeAllBytes()));
}

[Fact]
Expand Down Expand Up @@ -304,12 +243,6 @@ public async Task<OperationStartResult<NoValue>> StartAsync(
OperationStartContext context, NoValue input) =>
OperationStartResult.SyncResult(default(NoValue));

public async Task<NoValue> FetchResultAsync(
OperationFetchResultContext context) => default;

public Task<OperationInfo> FetchInfoAsync(
OperationFetchInfoContext context) => throw new NotImplementedException();

public Task CancelAsync(
OperationCancelContext context) => throw new NotImplementedException();
}
Expand All @@ -332,16 +265,8 @@ public async Task Handler_VoidService_Works()
new(Array.Empty<byte>()));
Assert.Empty(startResult.SyncResultValue!.ConsumeAllBytes());

var result = await handler.FetchOperationResultAsync(
new(
Service: "VoidService",
Operation: "NoReturnNoParam",
CancellationToken: default,
OperationToken: Guid.NewGuid().ToString()));
Assert.Empty(result.ConsumeAllBytes());

// Check that serialize was called with the start and fetch results
Assert.Equal([default(NoValue), default(NoValue)], serializer.SerializeCalls.ToArray());
// Check that serialize was called with the start result
Assert.Equal([default(NoValue)], serializer.SerializeCalls.ToArray());

// Check that deserialize was called with the start param
var (content, type) = serializer.DeserializeCalls.Single();
Expand Down