Skip to content

Commit

Permalink
Add a test for PurgeOrchestrationHistory
Browse files Browse the repository at this point in the history
- Fix bug with PurgeOrchestrationHistory relating to null start date
  • Loading branch information
andystaples committed Jan 30, 2025
1 parent 35cffa7 commit eebcb30
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/WebJobs.Extensions.DurableTask/ProtobufUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,10 @@ internal static PurgeInstanceFilter ToPurgeInstanceFilter(P.PurgeInstancesReques
statusFilter = request.PurgeInstanceFilter.RuntimeStatus?.Select(status => (OrchestrationStatus)status).ToList();
}

// This ternary condition is necessary because the protobuf spec __insists__ that CreatedTimeFrom may never be null,
// but nonetheless if you pass null in function code, the value will be null here
return new PurgeInstanceFilter(
request.PurgeInstanceFilter.CreatedTimeFrom.ToDateTime(),
request.PurgeInstanceFilter.CreatedTimeFrom == null ? DateTime.MinValue : request.PurgeInstanceFilter.CreatedTimeTo.ToDateTime(),
request.PurgeInstanceFilter.CreatedTimeTo?.ToDateTime(),
statusFilter);
}
Expand Down
38 changes: 38 additions & 0 deletions test/e2e/Apps/BasicDotNetIsolated/PurgeOrchestrationHistory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.DurableTask.Client;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.Durable.Tests.E2E
{
public static class PurgeOrchestrationHistory
{
[Function(nameof(PurgeOrchestrationHistory))]
public static async Task<HttpResponseData> PurgeHistory(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
[DurableClient] DurableTaskClient client,
FunctionContext executionContext)
{
ILogger logger = executionContext.GetLogger("HelloCities_HttpStart");

logger.LogInformation("Starting purge all instance history");

var requestPurgeResult = await client.PurgeAllInstancesAsync(new PurgeInstancesFilter(null, DateTime.UtcNow, new List<OrchestrationRuntimeStatus>{
OrchestrationRuntimeStatus.Completed,
OrchestrationRuntimeStatus.Failed,
OrchestrationRuntimeStatus.Terminated
}));

logger.LogInformation("Finished purge all instance history");

var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain");
await response.WriteStringAsync($"Purged {requestPurgeResult.PurgedInstanceCount} records");
return response;
}
}
}
49 changes: 49 additions & 0 deletions test/e2e/Tests/Tests/PurgeInstancesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.Azure.Durable.Tests.DotnetIsolatedE2E;

[Collection(Constants.FunctionAppCollectionName)]
public class PurgeInstancesTests
{
private readonly FunctionAppFixture _fixture;
private readonly ITestOutputHelper _output;

public PurgeInstancesTests(FunctionAppFixture fixture, ITestOutputHelper testOutputHelper)
{
_fixture = fixture;
_fixture.TestLogs.UseTestLogger(testOutputHelper);
_output = testOutputHelper;
}

// Due to some kind of asynchronous race condition in XUnit, when running these tests in pipelines,
// the output may be disposed before the message is written. Just ignore these types of errors for now.
private void WriteOutput(string message)
{
try
{
_output.WriteLine(message);
}
catch
{
// Ignore
}
}

[Theory]
[InlineData("PurgeOrchestrationHistory", HttpStatusCode.OK, @"^Purged [0-9]* records$")]
public async Task HttpTriggerTests(string functionName, HttpStatusCode expectedStatusCode, string responseRegex)
{
using HttpResponseMessage response = await HttpHelpers.InvokeHttpTrigger(functionName, "");
string actualMessage = await response.Content.ReadAsStringAsync();
Assert.Matches(responseRegex, actualMessage);
Assert.Equal(expectedStatusCode, response.StatusCode);
}
}

0 comments on commit eebcb30

Please sign in to comment.