Skip to content

Commit

Permalink
Added logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutierrez committed Oct 16, 2023
1 parent 81e4e1e commit 9baf2fe
Show file tree
Hide file tree
Showing 18 changed files with 355 additions and 21 deletions.
32 changes: 32 additions & 0 deletions Nixie.Tests/Actors/DiAwareActor.cs
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());
}
}

35 changes: 35 additions & 0 deletions Nixie.Tests/Actors/DiAwareArgsActor.cs
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);
}
}

22 changes: 22 additions & 0 deletions Nixie.Tests/Actors/LoggingActor.cs
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);
}
}

1 change: 1 addition & 0 deletions Nixie.Tests/Nixie.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
19 changes: 17 additions & 2 deletions Nixie.Tests/TestAskReplies.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using Microsoft.Extensions.Logging;
using Nixie.Tests.Actors;

namespace Nixie.Tests;
Expand Down Expand Up @@ -117,7 +118,14 @@ private async Task AskPingPong(IActorRef<PingActor, string, string> pingRef, int
[Fact]
public async Task TestCreateMultiplePingPingAndAskRace()
{
using ActorSystem asx = new();
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("Nixie", LogLevel.Debug)
.AddConsole();
});

using ActorSystem asx = new(logger: loggerFactory.CreateLogger<TestSendMessages>());

IActorRef<PingActor, string, string>[] actorRefs = new IActorRef<PingActor, string, string>[100];

Expand Down Expand Up @@ -152,7 +160,14 @@ private async Task AskPingPongMultiple(IActorRef<PingActor, string, string> ping
[Fact]
public async Task TestCreateMultiplePingPingAndAskMultipleRace()
{
using ActorSystem asx = new();
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("Nixie", LogLevel.Debug)
.AddConsole();
});

using ActorSystem asx = new(logger: loggerFactory.CreateLogger<TestSendMessages>());

IActorRef<PingActor, string, string>[] actorRefs = new IActorRef<PingActor, string, string>[100];

Expand Down
112 changes: 112 additions & 0 deletions Nixie.Tests/TestDI.cs
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());
}
}

41 changes: 41 additions & 0 deletions Nixie.Tests/TestLogging.cs
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());
}
}

12 changes: 11 additions & 1 deletion Nixie.Tests/TestSendMessages.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using Microsoft.Extensions.Logging;
using Nixie.Tests.Actors;

namespace Nixie.Tests;
Expand Down Expand Up @@ -152,7 +153,16 @@ public async Task TestCreateMultipleActorsAndSendOneMessage2()
[Fact]
public async Task TestSendMessageToFaultyActor()
{
using ActorSystem asx = new();
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("Nixie", LogLevel.Debug)
.AddConsole();
});

ILogger logger = loggerFactory.CreateLogger<TestSendMessages>();

using ActorSystem asx = new(logger: logger);

IActorRef<FaultyActor, FaultyMessage> actor = asx.Spawn<FaultyActor, FaultyMessage>();

Expand Down
14 changes: 13 additions & 1 deletion Nixie/ActorContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

using Microsoft.Extensions.Logging;

namespace Nixie;

/// <summary>
Expand All @@ -9,13 +11,20 @@ public sealed class ActorContext<TActor, TRequest> : IActorContext<TActor, TRequ
{
private readonly ActorSystem actorSystem;

private readonly ILogger? logger;

private readonly ActorRef<TActor, TRequest> self;

/// <summary>
/// Returns the actor system
/// </summary>
public ActorSystem ActorSystem => actorSystem;

/// <summary>
/// Returns the actor system logger
/// </summary>
public ILogger? Logger => logger;

/// <summary>
/// Returns a reference to the current actor
/// </summary>
Expand All @@ -30,9 +39,12 @@ public sealed class ActorContext<TActor, TRequest> : IActorContext<TActor, TRequ
/// Creates a new actor context.
/// </summary>
/// <param name="actorSystem"></param>
public ActorContext(ActorSystem actorSystem, ActorRef<TActor, TRequest> self)
/// <param name="logger"></param>
/// <param name="self"></param>
public ActorContext(ActorSystem actorSystem, ILogger? logger, ActorRef<TActor, TRequest> self)
{
this.actorSystem = actorSystem;
this.logger = logger;
this.self = self;
}
}
Loading

0 comments on commit 9baf2fe

Please sign in to comment.