-
Notifications
You must be signed in to change notification settings - Fork 361
Fixed unit tests validating actor reminder deserialization #1475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7ae9398
Fixed unit test missing check against one of the properties (e.g. it …
WhitWaldo ec0a936
File formatting tweak
WhitWaldo 8e02728
Tweaks to unit tests to make more reliable
WhitWaldo 740d012
Removed unused usings
WhitWaldo 63bb451
Added Dapr copyright header
WhitWaldo debd4fe
Removed unused variables
WhitWaldo f135a59
Merge branch 'master' into deserialize-actor-reminder
WhitWaldo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
288 changes: 148 additions & 140 deletions
288
test/Dapr.Actors.Test/Runtime/DefaultActorTimerManagerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,173 +1,181 @@ | ||
| // ------------------------------------------------------------ | ||
| // ------------------------------------------------------------------------ | ||
| // Copyright 2025 The Dapr Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ------------------------------------------------------------------------ | ||
|
|
||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Text.Json; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Dapr.Actors.Serialization; | ||
| using Moq; | ||
| using Xunit; | ||
| using JsonSerializer = System.Text.Json.JsonSerializer; | ||
|
|
||
| namespace Dapr.Actors.Runtime | ||
| namespace Dapr.Actors.Runtime; | ||
|
|
||
| public sealed class DefaultActorTimerManagerTests | ||
| { | ||
| public sealed class DefaultActorTimerManagerTests | ||
| /// <summary> | ||
| /// When register reminder is called, interactor is called with correct data. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| [Fact] | ||
| public async Task RegisterReminderAsync_CallsInteractor_WithCorrectData() | ||
| { | ||
| /// <summary> | ||
| /// When register reminder is called, interactor is called with correct data. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| [Fact] | ||
| public async Task RegisterReminderAsync_CallsInteractor_WithCorrectData() | ||
| { | ||
| var actorId = "123"; | ||
| var actorType = "abc"; | ||
| var interactor = new Mock<TestDaprInteractor>(); | ||
| var defaultActorTimerManager = new DefaultActorTimerManager(interactor.Object); | ||
| var actorReminder = new ActorReminder(actorType, new ActorId(actorId), "remindername", new byte[] { }, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1)); | ||
| var actualData = string.Empty; | ||
| var actorId = "123"; | ||
| var actorType = "abc"; | ||
| var interactor = new Mock<TestDaprInteractor>(); | ||
| var defaultActorTimerManager = new DefaultActorTimerManager(interactor.Object); | ||
| var actorReminder = new ActorReminder(actorType, new ActorId(actorId), "remindername", Array.Empty<byte>(), TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1)); | ||
| var actualData = string.Empty; | ||
|
|
||
| interactor | ||
| .Setup(d => d.RegisterReminderAsync(actorType, actorId, "remindername", It.Is<string>(data => !string.IsNullOrEmpty(data)), It.IsAny<CancellationToken>())) | ||
| .Callback<string, string, string, string, CancellationToken>((actorType, actorID, reminderName, data, token) => { | ||
| actualData = data; | ||
| }) | ||
| .Returns(Task.CompletedTask); | ||
| interactor | ||
| .Setup(d => d.RegisterReminderAsync(actorType, actorId, "remindername", It.Is<string>(data => !string.IsNullOrEmpty(data)), It.IsAny<CancellationToken>())) | ||
| .Callback<string, string, string, string, CancellationToken>((innerType, innerId, reminderName, data, token) => { | ||
| actualData = data; | ||
| }) | ||
| .Returns(Task.CompletedTask); | ||
|
|
||
| await defaultActorTimerManager.RegisterReminderAsync(actorReminder); | ||
| await defaultActorTimerManager.RegisterReminderAsync(actorReminder); | ||
|
|
||
| JsonElement json = JsonSerializer.Deserialize<dynamic>(actualData); | ||
| JsonElement json = JsonSerializer.Deserialize<dynamic>(actualData); | ||
|
|
||
| var isPeriodSet = json.TryGetProperty("period", out var period); | ||
| var isdDueTimeSet = json.TryGetProperty("dueTime", out var dueTime); | ||
| var isPeriodSet = json.TryGetProperty("period", out var period); | ||
| var isdDueTimeSet = json.TryGetProperty("dueTime", out var dueTime); | ||
|
|
||
| Assert.True(isPeriodSet); | ||
| Assert.True(isdDueTimeSet); | ||
| Assert.True(isPeriodSet); | ||
| Assert.True(isdDueTimeSet); | ||
|
|
||
| Assert.Equal("0h1m0s0ms", period.GetString()); | ||
| Assert.Equal("0h1m0s0ms", dueTime.GetString()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// When register reminder is called with repetition, interactor is called with correct data. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| [Fact] | ||
| public async Task RegisterReminderAsync_WithRepetition_CallsInteractor_WithCorrectData() | ||
| { | ||
| var actorId = "123"; | ||
| var actorType = "abc"; | ||
| var interactor = new Mock<TestDaprInteractor>(); | ||
| var defaultActorTimerManager = new DefaultActorTimerManager(interactor.Object); | ||
| var actorReminder = new ActorReminder(actorType, new ActorId(actorId), "remindername", new byte[] { }, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1), 10); | ||
| var actualData = string.Empty; | ||
|
|
||
| interactor | ||
| .Setup(d => d.RegisterReminderAsync(actorType, actorId, "remindername", It.Is<string>(data => !string.IsNullOrEmpty(data)), It.IsAny<CancellationToken>())) | ||
| .Callback<string, string, string, string, CancellationToken>((actorType, actorID, reminderName, data, token) => { | ||
| actualData = data; | ||
| }) | ||
| .Returns(Task.CompletedTask); | ||
|
|
||
| await defaultActorTimerManager.RegisterReminderAsync(actorReminder); | ||
|
|
||
| JsonElement json = JsonSerializer.Deserialize<dynamic>(actualData); | ||
|
|
||
| var isPeriodSet = json.TryGetProperty("period", out var period); | ||
| var isdDueTimeSet = json.TryGetProperty("dueTime", out var dueTime); | ||
| Assert.Equal("0h1m0s0ms", period.GetString()); | ||
| Assert.Equal("0h1m0s0ms", dueTime.GetString()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// When register reminder is called with repetition, interactor is called with correct data. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| [Fact] | ||
| public async Task RegisterReminderAsync_WithRepetition_CallsInteractor_WithCorrectData() | ||
| { | ||
| const string actorId = "123"; | ||
| const string actorType = "abc"; | ||
| var interactor = new Mock<TestDaprInteractor>(); | ||
| var defaultActorTimerManager = new DefaultActorTimerManager(interactor.Object); | ||
| var actorReminder = new ActorReminder(actorType, new ActorId(actorId), "remindername", new byte[] { }, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1), 10); | ||
| var actualData = string.Empty; | ||
|
|
||
| interactor | ||
| .Setup(d => d.RegisterReminderAsync(actorType, actorId, "remindername", It.Is<string>(data => !string.IsNullOrEmpty(data)), It.IsAny<CancellationToken>())) | ||
| .Callback<string, string, string, string, CancellationToken>((innerType, innerActorId, reminderName, data, token) => { | ||
| actualData = data; | ||
| }) | ||
| .Returns(Task.CompletedTask); | ||
|
|
||
| await defaultActorTimerManager.RegisterReminderAsync(actorReminder); | ||
|
|
||
| JsonElement json = JsonSerializer.Deserialize<dynamic>(actualData); | ||
|
|
||
| var isPeriodSet = json.TryGetProperty("period", out var period); | ||
| var isdDueTimeSet = json.TryGetProperty("dueTime", out var dueTime); | ||
|
|
||
| Assert.True(isPeriodSet); | ||
| Assert.True(isdDueTimeSet); | ||
| Assert.True(isPeriodSet); | ||
| Assert.True(isdDueTimeSet); | ||
|
|
||
| Assert.Equal("R10/PT1M", period.GetString()); | ||
| Assert.Equal("0h1m0s0ms", dueTime.GetString()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Get the GetReminder method is called without a registered reminder, it should return null. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task GetReminderAsync_ReturnsNullWhenUnavailable() | ||
| { | ||
| const string actorId = "123"; | ||
| const string actorType = "abc"; | ||
| const string reminderName = "reminderName"; | ||
| var interactor = new Mock<TestDaprInteractor>(); | ||
| interactor | ||
| .Setup(d => d.GetReminderAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.InternalServerError)); | ||
| var defaultActorTimerManager = new DefaultActorTimerManager(interactor.Object); | ||
|
|
||
| var reminderResult = await defaultActorTimerManager.GetReminderAsync(new ActorReminderToken(actorType, new ActorId(actorId), reminderName)); | ||
| Assert.Null(reminderResult); | ||
| } | ||
| Assert.Equal("R10/PT1M", period.GetString()); | ||
| Assert.Equal("0h1m0s0ms", dueTime.GetString()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Get the GetReminder method is called without a registered reminder, it should return null. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task GetReminderAsync_ReturnsNullWhenUnavailable() | ||
| { | ||
| const string actorId = "123"; | ||
| const string actorType = "abc"; | ||
| const string reminderName = "reminderName"; | ||
| var interactor = new Mock<TestDaprInteractor>(); | ||
| interactor | ||
| .Setup(d => d.GetReminderAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.InternalServerError)); | ||
| var defaultActorTimerManager = new DefaultActorTimerManager(interactor.Object); | ||
|
|
||
| var reminderResult = await defaultActorTimerManager.GetReminderAsync(new ActorReminderToken(actorType, new ActorId(actorId), reminderName)); | ||
| Assert.Null(reminderResult); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetReminderAsync_ReturnsNullWhenDeserialziationFails() | ||
| { | ||
| const string actorId = "123"; | ||
| const string actorType = "abc"; | ||
| const string reminderName = "reminderName"; | ||
| var interactor = new Mock<TestDaprInteractor>(); | ||
| var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{}") }; | ||
| [Fact] | ||
| public async Task GetReminderAsync_ReturnsNullWhenDeserialziationFails() | ||
| { | ||
| const string actorId = "123"; | ||
| const string actorType = "abc"; | ||
| const string reminderName = "reminderName"; | ||
| var interactor = new Mock<TestDaprInteractor>(); | ||
| var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{}") }; | ||
|
|
||
| interactor | ||
| .Setup(d => d.GetReminderAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(response); | ||
| var defaultActorTimerManager = new DefaultActorTimerManager(interactor.Object); | ||
|
|
||
| var reminderResult = await defaultActorTimerManager.GetReminderAsync(new ActorReminderToken(actorType, new ActorId(actorId), reminderName)); | ||
| Assert.Null(reminderResult); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetReminderAsync_ReturnsResultWhenAvailable() | ||
| { | ||
| const string actorId = "123"; | ||
| const string actorType = "abc"; | ||
| const string reminderName = "reminderName"; | ||
| var interactor = new Mock<TestDaprInteractor>(); | ||
| interactor | ||
| .Setup(d => d.GetReminderAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), | ||
| It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(response); | ||
| var defaultActorTimerManager = new DefaultActorTimerManager(interactor.Object); | ||
|
|
||
| var reminderResult = await defaultActorTimerManager.GetReminderAsync(new ActorReminderToken(actorType, new ActorId(actorId), reminderName)); | ||
| Assert.Null(reminderResult); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetReminderAsync_ReturnsResultWhenAvailable() | ||
| { | ||
| const string actorId = "123"; | ||
| const string actorType = "abc"; | ||
| const string reminderName = "reminderName"; | ||
| var interactor = new Mock<TestDaprInteractor>(); | ||
|
|
||
| //Create the reminder we'll return | ||
| var state = Array.Empty<byte>(); | ||
| var dueTime = TimeSpan.FromMinutes(1); | ||
| var period = TimeSpan.FromMinutes(1); | ||
| var actorReminder = new ActorReminder(actorType, new ActorId(actorId), "remindername", state, dueTime, period, 10); | ||
| //Create the reminder we'll return | ||
| var state = Array.Empty<byte>(); | ||
| var dueTime = TimeSpan.FromMinutes(1); | ||
| var period = TimeSpan.FromMinutes(1); | ||
| var actorReminder = new ActorReminder(actorType, new ActorId(actorId), "remindername", state, dueTime, period, 10); | ||
|
|
||
| //Serialize and create the response value | ||
| var actorReminderInfo = new ReminderInfo(actorReminder.State, actorReminder.DueTime, actorReminder.Period, | ||
| actorReminder.Repetitions, actorReminder.Ttl); | ||
| var serializedActorReminderInfo = await actorReminderInfo.SerializeAsync(); | ||
| var reminderResponse = new HttpResponseMessage(HttpStatusCode.OK) | ||
| { | ||
| Content = new StringContent(serializedActorReminderInfo) | ||
| }; | ||
| //Serialize and create the response value | ||
| var actorReminderInfo = new ReminderInfo(actorReminder.State, actorReminder.DueTime, actorReminder.Period, | ||
| actorReminder.Repetitions, actorReminder.Ttl); | ||
| var serializedActorReminderInfo = await actorReminderInfo.SerializeAsync(); | ||
| var reminderResponse = new HttpResponseMessage(HttpStatusCode.OK) | ||
| { | ||
| Content = new StringContent(serializedActorReminderInfo) | ||
| }; | ||
|
|
||
| //Register the response | ||
| var actualData = string.Empty; | ||
| interactor | ||
| .Setup(d => d.GetReminderAsync(actorType, actorId, reminderName, It.IsAny<CancellationToken>())) | ||
| .Callback<string, string, string, CancellationToken>((type, id, name, token) => { | ||
| }) | ||
| .Returns(Task.FromResult(reminderResponse)); | ||
| var defaultActorTimerManager = new DefaultActorTimerManager(interactor.Object); | ||
| //Register the response | ||
| interactor | ||
| .Setup(d => d.GetReminderAsync(actorType, actorId, reminderName, It.IsAny<CancellationToken>())) | ||
| .Callback<string, string, string, CancellationToken>((type, id, name, token) => { | ||
| }) | ||
| .Returns(Task.FromResult(reminderResponse)); | ||
| var defaultActorTimerManager = new DefaultActorTimerManager(interactor.Object); | ||
|
|
||
| var reminderResult = await defaultActorTimerManager.GetReminderAsync(new ActorReminderToken(actorType, new ActorId(actorId), reminderName)); | ||
| Assert.NotNull(reminderResult); | ||
| var reminderResult = await defaultActorTimerManager.GetReminderAsync(new ActorReminderToken(actorType, new ActorId(actorId), reminderName)); | ||
| Assert.NotNull(reminderResult); | ||
|
|
||
| Assert.Equal(period, reminderResult.Period); | ||
| Assert.Equal(state, reminderResult.State); | ||
| Assert.Equal(period, reminderResult.Period); | ||
| Assert.Equal(reminderName, reminderResult.Name); | ||
| } | ||
| Assert.Equal(dueTime, reminderResult.DueTime); | ||
| Assert.Equal(state, reminderResult.State); | ||
| Assert.Equal(period, reminderResult.Period); | ||
| Assert.Equal(reminderName, reminderResult.Name); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be a nitpick, is the indentation off here ? I see variables placed directly under the bracket (line 29)nvm