-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81e4e1e
commit 9baf2fe
Showing
18 changed files
with
355 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
| ||
namespace Nixie.Tests.Actors; | ||
|
||
internal sealed class DiAwareActor : IActor<string> | ||
{ | ||
private int receivedMessages; | ||
|
||
private readonly ISomeService someService; | ||
|
||
public DiAwareActor(IActorContext<DiAwareActor, string> _, ISomeService someService) | ||
{ | ||
this.someService = someService; | ||
} | ||
|
||
public int GetMessages() | ||
{ | ||
return receivedMessages; | ||
} | ||
|
||
public void IncrMessage(int number) | ||
{ | ||
receivedMessages += number; | ||
} | ||
|
||
public async Task Receive(string message) | ||
{ | ||
await Task.Yield(); | ||
|
||
IncrMessage(someService.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,35 @@ | ||
| ||
namespace Nixie.Tests.Actors; | ||
|
||
internal sealed class DiAwareArgsActor : IActor<string> | ||
{ | ||
private int receivedMessages; | ||
|
||
private readonly ISomeService someService; | ||
|
||
private readonly int extra; | ||
|
||
public DiAwareArgsActor(IActorContext<DiAwareArgsActor, string> _, ISomeService someService, int extra) | ||
{ | ||
this.someService = someService; | ||
this.extra = extra; | ||
} | ||
|
||
public int GetMessages() | ||
{ | ||
return receivedMessages; | ||
} | ||
|
||
public void IncrMessage(int number) | ||
{ | ||
receivedMessages += number; | ||
} | ||
|
||
public async Task Receive(string message) | ||
{ | ||
await Task.Yield(); | ||
|
||
IncrMessage(someService.GetValue() + extra); | ||
} | ||
} | ||
|
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,22 @@ | ||
| ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Nixie.Tests.Actors; | ||
|
||
public class LoggingActor : IActor<string> | ||
{ | ||
private readonly IActorContext<LoggingActor, string> context; | ||
|
||
public LoggingActor(IActorContext<LoggingActor, string> context) | ||
{ | ||
this.context = context; | ||
} | ||
|
||
public async Task Receive(string message) | ||
{ | ||
await Task.Yield(); | ||
|
||
context.Logger?.LogInformation("Message: {Message}", message); | ||
} | ||
} | ||
|
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 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,112 @@ | ||
| ||
using Microsoft.Extensions.DependencyInjection; | ||
using Nixie.Tests.Actors; | ||
|
||
namespace Nixie.Tests; | ||
|
||
internal interface ISomeService | ||
{ | ||
public int GetValue(); | ||
} | ||
|
||
internal sealed class SomeService : ISomeService | ||
{ | ||
public int GetValue() | ||
{ | ||
return 5; | ||
} | ||
} | ||
|
||
public class TestDI | ||
{ | ||
[Fact] | ||
public async Task TestBuildActorWithDependencyInjection() | ||
{ | ||
IServiceCollection services = new ServiceCollection(); | ||
|
||
services.AddSingleton<ISomeService, SomeService>(); | ||
|
||
ServiceProvider serviceProvider = services.BuildServiceProvider(); | ||
|
||
using ActorSystem asx = new(serviceProvider); | ||
|
||
IActorRef<DiAwareActor, string> actor = asx.Spawn<DiAwareActor, string>(); | ||
|
||
Assert.IsAssignableFrom<DiAwareActor>(actor.Runner.Actor); | ||
|
||
actor.Send("message"); | ||
|
||
await asx.Wait(); | ||
|
||
Assert.Equal(5, ((DiAwareActor)actor.Runner.Actor!).GetMessages()); | ||
} | ||
|
||
[Fact] | ||
public async Task TestBuildActorWithDependencyInjectionAndSendMany() | ||
{ | ||
IServiceCollection services = new ServiceCollection(); | ||
|
||
services.AddSingleton<ISomeService, SomeService>(); | ||
|
||
ServiceProvider serviceProvider = services.BuildServiceProvider(); | ||
|
||
using ActorSystem asx = new(serviceProvider); | ||
|
||
IActorRef<DiAwareActor, string> actor = asx.Spawn<DiAwareActor, string>(); | ||
|
||
Assert.IsAssignableFrom<DiAwareActor>(actor.Runner.Actor); | ||
|
||
for (int i = 0; i < 10; i++) | ||
actor.Send("message"); | ||
|
||
await asx.Wait(); | ||
|
||
Assert.Equal(50, ((DiAwareActor)actor.Runner.Actor!).GetMessages()); | ||
} | ||
|
||
[Fact] | ||
public async Task TestBuildActorWithDependencyInjectionAndArgs() | ||
{ | ||
IServiceCollection services = new ServiceCollection(); | ||
|
||
services.AddSingleton<ISomeService, SomeService>(); | ||
|
||
ServiceProvider serviceProvider = services.BuildServiceProvider(); | ||
|
||
using ActorSystem asx = new(serviceProvider); | ||
|
||
IActorRef<DiAwareArgsActor, string> actor = asx.Spawn<DiAwareArgsActor, string>(null, 100); | ||
|
||
Assert.IsAssignableFrom<DiAwareArgsActor>(actor.Runner.Actor); | ||
|
||
actor.Send("message"); | ||
|
||
await asx.Wait(); | ||
|
||
Assert.Equal(105, ((DiAwareArgsActor)actor.Runner.Actor!).GetMessages()); | ||
} | ||
|
||
[Fact] | ||
public async Task TestBuildActorWithDependencyInjectionAndArgsAndSendMany() | ||
{ | ||
IServiceCollection services = new ServiceCollection(); | ||
|
||
services.AddSingleton<ISomeService, SomeService>(); | ||
|
||
ServiceProvider serviceProvider = services.BuildServiceProvider(); | ||
|
||
using ActorSystem asx = new(serviceProvider); | ||
|
||
IActorRef<DiAwareArgsActor, string> actor = asx.Spawn<DiAwareArgsActor, string>(null, 100); | ||
|
||
Assert.IsAssignableFrom<DiAwareArgsActor>(actor.Runner.Actor); | ||
|
||
for (int i = 0; i < 10; i++) | ||
actor.Send("message"); | ||
|
||
await asx.Wait(); | ||
|
||
Assert.Equal(1050, ((DiAwareArgsActor)actor.Runner.Actor!).GetMessages()); | ||
} | ||
} | ||
|
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,41 @@ | ||
| ||
using Nixie.Tests.Actors; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Nixie.Tests; | ||
|
||
public class TestLogging | ||
{ | ||
private readonly ILoggerFactory loggerFactory; | ||
|
||
private readonly ILogger logger; | ||
|
||
public TestLogging() | ||
{ | ||
loggerFactory = LoggerFactory.Create(builder => | ||
{ | ||
builder | ||
.AddFilter("Nixie", LogLevel.Debug) | ||
.AddConsole(); | ||
}); | ||
|
||
logger = loggerFactory.CreateLogger<TestLogging>(); | ||
} | ||
|
||
[Fact] | ||
public async Task TestAddLogging() | ||
{ | ||
using ActorSystem asx = new(logger: logger); | ||
|
||
IActorRef<LoggingActor, string> actor = asx.Spawn<LoggingActor, string>(); | ||
|
||
Assert.IsAssignableFrom<LoggingActor>(actor.Runner.Actor); | ||
|
||
actor.Send("message"); | ||
|
||
await asx.Wait(); | ||
|
||
//Assert.Equal(5, ((LoggingActor)actor.Runner.Actor!).GetMessages()); | ||
} | ||
} | ||
|
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
Oops, something went wrong.