Skip to content

Commit

Permalink
correct the double call to OnActivateAsync on IRemindable grains
Browse files Browse the repository at this point in the history
Test grain for tracking OnActivateAsync call count
Test OnActivateAsync is only called once
  • Loading branch information
amccool committed Jul 30, 2024
1 parent cf64b8d commit 41f1504
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/OrleansTestKit/TestKitSilo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,11 @@ public async Task<T> CreateGrainAsync<T>(IdSpan identity, CancellationToken canc
// Used to enable reminder context on during activate
using var reminderContext =
await GetReminderActivationContext(grain, cancellation).ConfigureAwait(false);

await grain.OnActivateAsync(cancellation).ConfigureAwait(false);
_activatedGrains.Add(grain);
}

await grain.OnActivateAsync(cancellation).ConfigureAwait(false);
_activatedGrains.Add(grain);


return (T)grain;
}
}
38 changes: 38 additions & 0 deletions test/OrleansTestKit.Tests/Grains/ActivationCountWithReminder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Orleans.Runtime;
using Orleans.Streams;

namespace TestGrains;

public sealed class ActivationCountWithReminder : Grain, IGrainWithIntegerKey, IRemindable
{
private int _activationCount;

public override Task OnActivateAsync(CancellationToken cancellationToken)
{
_activationCount++;
return base.OnActivateAsync(cancellationToken);
}

public Task<int> GetActivationCount() => Task.FromResult(_activationCount);


Task IRemindable.ReceiveReminder(string reminderName, TickStatus status)
{
return Task.CompletedTask;
}

public Task RegisterReminder(string reminderName, TimeSpan dueTime, TimeSpan period)
{
return this.RegisterOrUpdateReminder(reminderName, dueTime, period);
}

public async Task UnregisterReminder(string reminderName)
{
var reminder = await this.GetReminder(reminderName);

if (reminder != null)
{
await this.UnregisterReminder(reminder);
}
}
}
12 changes: 12 additions & 0 deletions test/OrleansTestKit.Tests/Tests/ActivationGrainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@ public async Task ShouldThrowNotSupportedException()
await Assert.ThrowsAsync<NotSupportedException>(
async () => await Silo.CreateGrainAsync<StatefulUnsupportedActivationGrain>(0));
}

[Fact]
public async Task OnActivateAsyncShouldBeCalledOnceOnRemindable()
{
// Arrange

// Act
var grain = await Silo.CreateGrainAsync<ActivationCountWithReminder>(1);
var value = await grain.GetActivationCount();

Assert.Equal(1, value);
}
}

0 comments on commit 41f1504

Please sign in to comment.