-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use generic object for keyed services (#169)
Co-authored-by: Rinke Blootens <[email protected]> Co-authored-by: Kyle Dodson <[email protected]>
- Loading branch information
1 parent
01d1388
commit e25c6ac
Showing
7 changed files
with
212 additions
and
107 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using TestInterfaces; | ||
|
||
namespace TestGrains; | ||
|
||
public interface IDependency | ||
{ | ||
string? GetValue(); | ||
} | ||
|
||
public sealed class DependencyGrain : Grain, IDependencyGrain | ||
{ | ||
private readonly IDependency? _firstKeyedDependency; | ||
|
||
private readonly IDependency? _secondKeyedDependency; | ||
|
||
private readonly IDependency? _unkeyedDependency; | ||
|
||
public DependencyGrain( | ||
IDependency? unkeyedDependency, | ||
[FromKeyedServices("first")] IDependency? firstKeyedDependency, | ||
[FromKeyedServices("second")] IDependency? secondKeyedDependency) | ||
{ | ||
_unkeyedDependency = unkeyedDependency; | ||
_firstKeyedDependency = firstKeyedDependency; | ||
_secondKeyedDependency = secondKeyedDependency; | ||
} | ||
|
||
public Task<string?> GetFirstKeyedServiceValue() => | ||
Task.FromResult(_firstKeyedDependency?.GetValue()); | ||
|
||
public Task<string?> GetSecondKeyedServiceValue() => | ||
Task.FromResult(_secondKeyedDependency?.GetValue()); | ||
|
||
public Task<string?> GetUnkeyedServiceValue() => | ||
Task.FromResult(_unkeyedDependency?.GetValue()); | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace TestInterfaces; | ||
|
||
public interface IDependencyGrain : IGrainWithGuidKey | ||
{ | ||
Task<string?> GetFirstKeyedServiceValue(); | ||
|
||
Task<string?> GetSecondKeyedServiceValue(); | ||
|
||
Task<string?> GetUnkeyedServiceValue(); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using FluentAssertions; | ||
using Moq; | ||
using TestGrains; | ||
using Xunit; | ||
|
||
namespace Orleans.TestKit.Tests; | ||
|
||
public class DependencyGrainTests : TestKitBase | ||
{ | ||
[Fact] | ||
public async Task GrainWithoutServices() | ||
{ | ||
// Arrange | ||
var grain = await Silo.CreateGrainAsync<DependencyGrain>(Guid.NewGuid()); | ||
|
||
// Act | ||
var unkeyedServiceValue = await grain.GetUnkeyedServiceValue(); | ||
var firstKeyedServiceValue = await grain.GetFirstKeyedServiceValue(); | ||
var secondKeyedServiceValue = await grain.GetSecondKeyedServiceValue(); | ||
|
||
// Assert | ||
unkeyedServiceValue.Should().BeNull(); | ||
firstKeyedServiceValue.Should().BeNull(); | ||
secondKeyedServiceValue.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public async Task GrainWithServices() | ||
{ | ||
// Arrange | ||
var unkeyedService = new Mock<IDependency>(MockBehavior.Strict); | ||
unkeyedService.Setup(x => x.GetValue()).Returns(""); | ||
Silo.ServiceProvider.AddServiceProbe(unkeyedService); | ||
|
||
var firstKeyedService = new Mock<IDependency>(MockBehavior.Strict); | ||
firstKeyedService.Setup(x => x.GetValue()).Returns("first"); | ||
Silo.ServiceProvider.AddKeyedServiceProbe("first", firstKeyedService); | ||
|
||
var secondKeyedService = new Mock<IDependency>(MockBehavior.Strict); | ||
secondKeyedService.Setup(x => x.GetValue()).Returns("second"); | ||
Silo.ServiceProvider.AddKeyedServiceProbe("second", secondKeyedService); | ||
|
||
var grain = await Silo.CreateGrainAsync<DependencyGrain>(Guid.NewGuid()); | ||
|
||
// Act | ||
var unkeyedServiceValue = await grain.GetUnkeyedServiceValue(); | ||
var firstKeyedServiceValue = await grain.GetFirstKeyedServiceValue(); | ||
var secondKeyedServiceValue = await grain.GetSecondKeyedServiceValue(); | ||
|
||
// Assert | ||
unkeyedServiceValue.Should().BeEmpty(); | ||
unkeyedService.Verify(x => x.GetValue(), Times.Once); | ||
|
||
firstKeyedServiceValue.Should().Be("first"); | ||
firstKeyedService.Verify(x => x.GetValue(), Times.Once); | ||
|
||
secondKeyedServiceValue.Should().Be("second"); | ||
secondKeyedService.Verify(x => x.GetValue(), Times.Once); | ||
} | ||
} |