Skip to content

Commit 6f07643

Browse files
authored
Refactored to make deserialization for GetJobAsync testable. Added unit test to validate reported customer issue. (#1497)
Signed-off-by: Whit Waldo <[email protected]>
1 parent 55895fa commit 6f07643

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/Dapr.Jobs/DaprJobsGrpcClient.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,7 @@ public override async Task<DaprJobDetails> GetJobAsync(string jobName, Cancellat
163163
var envelope = new Autogenerated.GetJobRequest { Name = jobName };
164164
var grpcCallOptions = DaprClientUtilities.ConfigureGrpcCallOptions(typeof(DaprJobsClient).Assembly, this.DaprApiToken, cancellationToken);
165165
var response = await Client.GetJobAlpha1Async(envelope, grpcCallOptions);
166-
var schedule = DateTime.TryParse(response.Job.DueTime, out var dueTime)
167-
? DaprJobSchedule.FromDateTime(dueTime)
168-
: new DaprJobSchedule(response.Job.Schedule);
169-
170-
return new DaprJobDetails(schedule)
171-
{
172-
DueTime = !string.IsNullOrWhiteSpace(response.Job.DueTime) ? DateTime.Parse(response.Job.DueTime) : null,
173-
Ttl = !string.IsNullOrWhiteSpace(response.Job.Ttl) ? DateTime.Parse(response.Job.Ttl) : null,
174-
RepeatCount = (int?)response.Job.Repeats,
175-
Payload = response.Job.Data.ToByteArray()
176-
};
166+
return DeserializeJobResponse(response);
177167
}
178168
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
179169
{
@@ -192,6 +182,29 @@ public override async Task<DaprJobDetails> GetJobAsync(string jobName, Cancellat
192182
throw new DaprException("Get job operation failed: the Dapr endpoint did not return the expected value.");
193183
}
194184

185+
/// <summary>
186+
/// Testable method for performing job response deserialization.
187+
/// </summary>
188+
/// <remarks>
189+
/// This is exposed strictly for testing purposes.
190+
/// </remarks>
191+
/// <param name="response">The job response to deserialize.</param>
192+
/// <returns>The deserialized job response.</returns>
193+
internal static DaprJobDetails DeserializeJobResponse(Autogenerated.GetJobResponse response)
194+
{
195+
var schedule = DateTime.TryParse(response.Job.DueTime, out var dueTime)
196+
? DaprJobSchedule.FromDateTime(dueTime)
197+
: new DaprJobSchedule(response.Job.Schedule);
198+
199+
return new DaprJobDetails(schedule)
200+
{
201+
DueTime = !string.IsNullOrWhiteSpace(response.Job.DueTime) ? DateTime.Parse(response.Job.DueTime) : null,
202+
Ttl = !string.IsNullOrWhiteSpace(response.Job.Ttl) ? DateTime.Parse(response.Job.Ttl) : null,
203+
RepeatCount = (int?)response.Job.Repeats ?? 0,
204+
Payload = response.Job.Data?.ToByteArray() ?? null
205+
};
206+
}
207+
195208
/// <summary>
196209
/// Deletes the specified job.
197210
/// </summary>

test/Dapr.Jobs.Test/DaprJobsGrpcClientTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
using System;
1515
using System.Net.Http;
16+
using Dapr.Client.Autogen.Grpc.v1;
1617
using Dapr.Jobs.Models;
1718
using Moq;
1819
using Xunit;
@@ -167,6 +168,21 @@ public void DeleteJobAsync_NameCannotBeEmpty()
167168
});
168169
#pragma warning restore CS0618 // Type or member is obsolete
169170
}
171+
172+
[Fact]
173+
public void ShouldDeserialize_EveryExpression()
174+
{
175+
const string scheduleText = "@every 1m";
176+
var response = new GetJobResponse { Job = new Job { Name = "test", Schedule = scheduleText } };
177+
var schedule = DaprJobSchedule.FromExpression(scheduleText);
178+
179+
var jobDetails = DaprJobsGrpcClient.DeserializeJobResponse(response);
180+
Assert.Null(jobDetails.Payload);
181+
Assert.Equal(0, jobDetails.RepeatCount);
182+
Assert.Null(jobDetails.Ttl);
183+
Assert.Null(jobDetails.DueTime);
184+
Assert.Equal(jobDetails.Schedule.ExpressionValue, schedule.ExpressionValue);
185+
}
170186

171187
private sealed record TestPayload(string Name, string Color);
172188
}

0 commit comments

Comments
 (0)