Skip to content

Commit

Permalink
Fixed unit tests, fixed duplicate cancellation token in dynamic invoc…
Browse files Browse the repository at this point in the history
…ation

Signed-off-by: Whit Waldo <[email protected]>
  • Loading branch information
WhitWaldo committed Sep 30, 2024
1 parent 10cc382 commit 4c252d5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
9 changes: 4 additions & 5 deletions src/Dapr.Jobs/Extensions/EndpointRouteBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,13 @@ public static IEndpointRouteBuilder MapDaprScheduledJobHandler(this IEndpointRou
var parameters = new List<object?> { jobName, jobPayload };
var actionParameters = action.Method.GetParameters().Skip(2).ToArray();
parameters.AddRange(actionParameters.Select(parameter => context.RequestServices.GetService(parameter.ParameterType)));
parameters.AddRange(actionParameters
.Where(parameter => parameter.ParameterType != typeof(CancellationToken))
.Select(parameter => context.RequestServices.GetService(parameter.ParameterType)));
parameters.Add(cancellationToken);
var result = action.DynamicInvoke(parameters.ToArray());
if (result is Task task)
{
await task;
}
if (result is Task task) await task;
});

return endpoints;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
// limitations under the License.
// ------------------------------------------------------------------------

#nullable enable

using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Dapr.Jobs.Extensions;
using Dapr.Jobs.Models;
Expand Down Expand Up @@ -80,9 +83,9 @@ private sealed record SamplePayload(string Name, int Count);

public sealed class Validator
{
public string JobName { get; set; }
public string? JobName { get; set; }

public string SerializedPayload { get; set; }
public string? SerializedPayload { get; set; }
}

private static TestServer CreateTestServer()
Expand All @@ -98,11 +101,10 @@ private static TestServer CreateTestServer()
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapDaprScheduledJobHandler(async (serviceProvider, jobName, jobDetails) =>
endpoints.MapDaprScheduledJobHandler(async (string? jobName, DaprJobDetails? jobDetails, Validator validator, CancellationToken cancellationToken) =>
{
var validator = serviceProvider.GetRequiredService<Validator>();
validator.JobName = jobName;
if (jobName is not null)
validator.JobName = jobName;
if (jobDetails?.Payload is not null)
{
var payloadString = Encoding.UTF8.GetString(jobDetails.Payload);
Expand Down

0 comments on commit 4c252d5

Please sign in to comment.