Skip to content

Commit

Permalink
Added test to ensure that even if cancellation token is provided, it'…
Browse files Browse the repository at this point in the history
…ll handle the mapping properly

Signed-off-by: Whit Waldo <[email protected]>
  • Loading branch information
WhitWaldo committed Nov 1, 2024
1 parent 60907c9 commit ad9c8b5
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,35 @@ public async Task MapDaprScheduledJobHandler_ValidRequest_ExecutesAction()
Assert.Equal(serializedPayload, validator.SerializedPayload);
}

[Fact]
public async Task MapDaprScheduleJobHandler_HandleMissingCancellationToken()
{
var server = CreateTestServer2();
var client = server.CreateClient();

var serializedPayload = JsonSerializer.Serialize(new SamplePayload("Dapr", 789));
var serializedPayloadBytes = Encoding.UTF8.GetBytes(serializedPayload);
var jobDetails = new DaprJobDetails(new DaprJobSchedule("0 0 * * *"))
{
RepeatCount = 5,
DueTime = DateTimeOffset.UtcNow,
Ttl = DateTimeOffset.UtcNow.AddHours(1),
Payload = serializedPayloadBytes
};
var content = new StringContent(JsonSerializer.Serialize(jobDetails), Encoding.UTF8, "application/json");

const string jobName = "testJob";
var response = await client.PostAsync($"/job/{jobName}", content);

response.EnsureSuccessStatusCode();

//Validate the job name and payload
var validator = server.Services.GetRequiredService<Validator>();
Assert.Equal(jobName, validator.JobName);
Assert.Equal(serializedPayload, validator.SerializedPayload);
}


[Fact]
public async Task MapDaprScheduledJobHandler_InvalidPayload()
{
Expand Down Expand Up @@ -117,4 +146,34 @@ private static TestServer CreateTestServer()

return new TestServer(builder);
}

private static TestServer CreateTestServer2()
{
var builder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddSingleton<Validator>();
services.AddRouting();
})
.Configure(app =>
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapDaprScheduledJobHandler(async (string? jobName, Validator validator, DaprJobDetails? jobDetails) =>
{
if (jobName is not null)
validator.JobName = jobName;
if (jobDetails?.Payload is not null)
{
var payloadString = Encoding.UTF8.GetString(jobDetails.Payload);
validator.SerializedPayload = payloadString;
}
await Task.CompletedTask;
});
});
});

return new TestServer(builder);
}
}

0 comments on commit ad9c8b5

Please sign in to comment.