Skip to content

Commit

Permalink
Merge pull request #103 from freever/master
Browse files Browse the repository at this point in the history
Added test for creating and mocking multiple grain probes
  • Loading branch information
seniorquico authored Dec 22, 2020
2 parents c637fb2 + 8226f05 commit f279cfc
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 2 deletions.
13 changes: 13 additions & 0 deletions test/OrleansTestKit.Tests/Grains/IUnknownGrainResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Orleans;

namespace TestGrains
{
public interface IUnknownGrainResolver : IGrainWithStringKey
{
Task<List<string>> GetResolvedUnknownGrainIdsAsync();

Task CreateAndPingMultiple();
}
}
7 changes: 6 additions & 1 deletion test/OrleansTestKit.Tests/Grains/PingGrain.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Orleans;
using TestInterfaces;

namespace TestGrains
{
public class PingGrain : Grain, IPing
{


public Task Ping()
{
var pong = GrainFactory.GetGrain<IPong>(22);
Expand All @@ -19,5 +23,6 @@ public Task PingCompound()

return pong.Pong();
}

}
}
23 changes: 23 additions & 0 deletions test/OrleansTestKit.Tests/Grains/UnknownGrainResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Orleans;
using TestInterfaces;

namespace TestGrains
{
public class UnknownGrainResolver : Grain, IUnknownGrainResolver
{
private List<string> _resolvedIds = new List<string>();

public Task<List<string>> GetResolvedUnknownGrainIdsAsync() => Task.FromResult(_resolvedIds);

public async Task CreateAndPingMultiple()
{
var unknownGrainOne = GrainFactory.GetGrain<IUnknownGrain>("unknownGrainOne");
var unknownGrainTwo = GrainFactory.GetGrain<IUnknownGrain>("unknownGrainTwo");

_resolvedIds.Add(await unknownGrainOne.WhatsMyId());
_resolvedIds.Add(await unknownGrainTwo.WhatsMyId());
}
}
}
2 changes: 1 addition & 1 deletion test/OrleansTestKit.Tests/Interfaces/IPong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ public interface IPong : IGrainWithIntegerKey
{
Task Pong();
}
}
}
14 changes: 14 additions & 0 deletions test/OrleansTestKit.Tests/Interfaces/IUnknownGrain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Threading.Tasks;
using Orleans;

namespace TestInterfaces
{
public interface IUnknownGrain : IGrainWithStringKey
{
//this grain's identity is not known when it is resolved
//for instance, if GrainA needs to create another NEW IUnknownGrain it would generate a new id and ask the GrainFactory for a reference
//in this case a test of GrainA would not know the id in advance and would not be able to set up a probe using the id.

Task<string> WhatsMyId();
}
}
30 changes: 30 additions & 0 deletions test/OrleansTestKit.Tests/Tests/GrainProbeTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
Expand Down Expand Up @@ -142,6 +143,35 @@ public async Task FactoryProbe()
pong.Verify(p => p.Pong(), Times.Once);
}

[Fact]
public async Task FactoryProbeWithMulipleNewProbes()
{
// useful for when a grain needs to create other grains whose identities are not known beforehand
// and probes for the new grains need to return certain values

var firstUnknownGrain = new Mock<IUnknownGrain>();
var secondUnknownGrain = new Mock<IUnknownGrain>();
var probeQueue = new Queue<Mock<IUnknownGrain>>(new [] {firstUnknownGrain, secondUnknownGrain});

this.Silo.AddProbe<IUnknownGrain>(identity =>
{
var nextProbe = probeQueue.Dequeue();
nextProbe.Setup(probe => probe.WhatsMyId())
.ReturnsAsync(identity.PrimaryKeyString);
return nextProbe;
});

var grain = await this.Silo.CreateGrainAsync<UnknownGrainResolver>("1");

await grain.CreateAndPingMultiple();

var resolvedIds = await grain.GetResolvedUnknownGrainIdsAsync();
resolvedIds[0].Should().Be("unknownGrainOne");
resolvedIds[1].Should().Be("unknownGrainTwo");
}

[Fact]
public async Task ProbeWithClassPrefix()
{
Expand Down

0 comments on commit f279cfc

Please sign in to comment.