Skip to content

Commit

Permalink
changing loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Gardham-Pallister committed Apr 9, 2017
1 parent fd9509e commit d751654
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 26 deletions.
9 changes: 5 additions & 4 deletions src/Rafty/Infrastructure/RaftyConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public static IApplicationBuilder UseRafty(this IApplicationBuilder builder,
IMessageBus messageBus,
IStateMachine stateMachine,
IServiceRegistry serviceRegistry,
ILogger logger,
ILoggerFactory loggerFactory,
IServersInCluster serversInCluster,
string raftyBasePath = null)
{
builder.UseRaftyForTesting(baseUri, messageSender, messageBus, stateMachine, serviceRegistry,
logger, serversInCluster, raftyBasePath);
loggerFactory, serversInCluster, raftyBasePath);

return builder;
}
Expand All @@ -40,13 +40,14 @@ public static (IApplicationBuilder builder, Server server, ServerInCluster serve
IMessageBus messageBus,
IStateMachine stateMachine,
IServiceRegistry serviceRegistry,
ILogger logger,
ILoggerFactory loggerFactory,
IServersInCluster serversInCluster,
string raftyBasePath = null)
{
var urlConfig = RaftyUrlConfig.Get(raftyBasePath);

var server = new Server(messageBus, serversInCluster, stateMachine, logger);
var server = new Server(messageBus, serversInCluster, stateMachine, loggerFactory);
var logger = loggerFactory.CreateLogger<IApplicationBuilder>();

serviceRegistry.Register(new RegisterService(RaftyServiceDiscoveryName.Get(), server.Id, baseUri));

Expand Down
4 changes: 2 additions & 2 deletions src/Rafty/Messaging/HttpClientMessageSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public class HttpClientMessageSender : IMessageSender, IDisposable
private readonly string _commandUrl;
private readonly ILogger _logger;

public HttpClientMessageSender(IServiceRegistry serviceRegistry, ILogger logger, string raftyBasePath = null)
public HttpClientMessageSender(IServiceRegistry serviceRegistry, ILoggerFactory loggerFactory, string raftyBasePath = null)
{
var urlConfig = RaftyUrlConfig.Get(raftyBasePath);

_appendEntriesUrl = urlConfig.appendEntriesUrl;
_requestVoteUrl = urlConfig.requestVoteUrl;
_commandUrl = urlConfig.commandUrl;
_serviceRegistry = serviceRegistry;
_logger = logger;
_logger = loggerFactory.CreateLogger<HttpClientMessageSender>();
_sendToSelfHandlers = new Dictionary<Type, Action<IMessage>>
{
{typeof(BecomeCandidate), x => _server.Receive((BecomeCandidate) x)},
Expand Down
4 changes: 2 additions & 2 deletions src/Rafty/Raft/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public class Server
public Server(IMessageBus messageBus,
IServersInCluster serversInCluster,
IStateMachine stateMachine,
ILogger logger)
ILoggerFactory loggerFactory)
{
_stateMachine = stateMachine;
_logger = logger;
_logger = loggerFactory.CreateLogger<Server>();
_messageBus = messageBus;
_serversInClusterInCluster = serversInCluster;
Id = Guid.NewGuid();
Expand Down
24 changes: 14 additions & 10 deletions test/Rafty.AcceptanceTests/AcceptanceTestsSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,27 +290,31 @@ private async Task GivenAServerIsRunning(string baseUrl)
ServerInCluster serverInCluster = null;
IMessageBus messageBus = null;
IStateMachine stateMachine = null;
ILogger logger = null;

var webHost = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureServices(s =>
{
logger = new ConsoleLogger("ConsoleLogger", (x, y) => true, true);
s.AddSingleton<ILogger>(logger);
messageSender = new HttpClientMessageSender(_serviceRegistry, logger);
s.AddSingleton<IMessageSender>(messageSender);
messageBus = new InMemoryBus(messageSender);
s.AddSingleton<IMessageBus>(messageBus);
stateMachine = new FakeStateMachine();
s.AddSingleton<IStateMachine>(stateMachine);
s.AddSingleton<IMessageSender, HttpClientMessageSender>();
s.AddSingleton<IMessageBus, InMemoryBus>();
s.AddSingleton<IStateMachine, FakeStateMachine>();
s.AddSingleton<IServersInCluster, InMemoryServersInCluster>();
//s.AddSingleton<IServiceRegistry, ServiceRegistry>();
})
.Configure(app =>
{
var loggerFactory = app.ApplicationServices.GetRequiredService<ILoggerFactory>();

messageSender = app.ApplicationServices.GetRequiredService<IMessageSender>();
messageBus = app.ApplicationServices.GetRequiredService<IMessageBus>();
stateMachine = app.ApplicationServices.GetRequiredService<IStateMachine>();
//var _serviceRegistry = app.ApplicationServices.GetRequiredService<ILoggerFactory>();
//_serversInCluster = app.ApplicationServices.GetRequiredService<IServersInCluster>();

var result = app.UseRaftyForTesting(new Uri(baseUrl), messageSender, messageBus, stateMachine,
_serviceRegistry, logger, _serversInCluster);
_serviceRegistry, loggerFactory, _serversInCluster);

server = result.server;
serverInCluster = result.serverInCluster;
Expand Down
3 changes: 2 additions & 1 deletion test/Rafty.UnitTests/AllServersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Moq;
using Rafty.Messages;
Expand Down Expand Up @@ -186,7 +187,7 @@ private void ServerReceives(RequestVote requestVote)
private void GivenANewServer()
{
_fakeStateMachine = new FakeStateMachine();
_server = new Server(_messageBus.Object, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true));
_server = new Server(_messageBus.Object, _serversInCluster, _fakeStateMachine, new LoggerFactory());
}
}
}
3 changes: 2 additions & 1 deletion test/Rafty.UnitTests/AppendEntriesTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Rafty.AcceptanceTests;
using Rafty.Messages;
Expand Down Expand Up @@ -210,7 +211,7 @@ private void GivenANewServer()
{
_fakeStateMachine = new FakeStateMachine();
_messageBus = new FakeMessageBus();
_server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true));
_server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new LoggerFactory());
}

private void ThenTheLogContainsEntriesCount(int expectedCount)
Expand Down
3 changes: 2 additions & 1 deletion test/Rafty.UnitTests/CandidateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Moq;
using Rafty.Messages;
Expand Down Expand Up @@ -243,7 +244,7 @@ private void TheServerIsACandidate()
private void GivenANewServer()
{
_fakeStateMachine = new FakeStateMachine();
_server = new Server(_messageBus.Object, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true));
_server = new Server(_messageBus.Object, _serversInCluster, _fakeStateMachine, new LoggerFactory());
}
}
}
5 changes: 3 additions & 2 deletions test/Rafty.UnitTests/FollowerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Moq;
using Rafty.AcceptanceTests;
Expand Down Expand Up @@ -75,13 +76,13 @@ private void GivenANewServer()
{
_fakeStateMachine = new FakeStateMachine();
_messageBus = new FakeMessageBus();
_server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true));
_server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new LoggerFactory());
}

private void GivenANewServer(Mock<IMessageBus> mock)
{
_fakeStateMachine = new FakeStateMachine();
_server = new Server(mock.Object, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true));
_server = new Server(mock.Object, _serversInCluster, _fakeStateMachine, new LoggerFactory());
}

private void TheServerIsAFollower()
Expand Down
3 changes: 2 additions & 1 deletion test/Rafty.UnitTests/InitialServerStateTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Rafty.Messaging;
using Rafty.Raft;
Expand Down Expand Up @@ -127,7 +128,7 @@ private void GivenANewServer()
{
_fakeStateMachine = new FakeStateMachine();
_messageBus = new FakeMessageBus();
_server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true));
_server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new LoggerFactory());
}

private void ThenTheServerHasAnId()
Expand Down
3 changes: 2 additions & 1 deletion test/Rafty.UnitTests/LeaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Moq;
using Moq.Language.Flow;
Expand Down Expand Up @@ -421,7 +422,7 @@ private void TheServerIsALeader()
private void GivenANewServer()
{
_fakeStateMachine = new FakeStateMachine();
_server = new Server(_messageBus.Object, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true));
_server = new Server(_messageBus.Object, _serversInCluster, _fakeStateMachine, new LoggerFactory());
_serversInCluster.Add(new ServerInCluster(_server.Id));
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/Rafty.UnitTests/RequestVoteTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Rafty.AcceptanceTests;
using Rafty.Messages;
Expand Down Expand Up @@ -123,7 +124,7 @@ private void GivenANewServer()
{
_fakeStateMachine = new FakeStateMachine();
_messageBus = new FakeMessageBus();
_server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new ConsoleLogger("ConsoleLogger", (x, y) => true, true));
_server = new Server(_messageBus, _serversInCluster, _fakeStateMachine, new LoggerFactory());
}

private void GivenTheCurrentTermIs(int term)
Expand Down

0 comments on commit d751654

Please sign in to comment.