Changed logging system to be event based.

Implemented LRUCache for instantiating services without starting.

Service state changed back to enumerator.

Namespace refactoring.
This commit is contained in:
2021-04-19 01:34:45 -05:00
parent cac5ca054c
commit 35a2765559
26 changed files with 404 additions and 498 deletions

View File

@@ -2,7 +2,7 @@ using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using GameServiceWarden.Core.Games;
using GameServiceWarden.Core.Module;
using GameServiceWarden.Core.Persistence;
namespace GameServiceWarden.Core.Tests.Modules

View File

@@ -10,9 +10,11 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
{
public IReadOnlyCollection<IServiceConfigurable> Configurables { get; set; }
public event EventHandler<bool> StateChangeEvent;
public ServiceState CurrentState { get; private set; } = ServiceState.Stopped;
public event EventHandler<ServiceState> StateChangeEvent;
public event EventHandler<string> UpdateLogEvent;
public ServiceState CurrentState { get; private set; } = ServiceState.Stopped;
private MemoryStream memoryStream;
private StreamWriter consoleWriter;
private Stack<Task> taskStack = new Stack<Task>();
@@ -29,7 +31,7 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
public void ElegantShutdown()
{
CurrentState = ServiceState.Stopped;
StateChangeEvent?.Invoke(this, false);
StateChangeEvent?.Invoke(this, ServiceState.Stopped);
Task task;
while(taskStack.TryPop(out task)) {
if (task.IsCompleted) {
@@ -44,13 +46,20 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
{
taskStack.Push(consoleWriter.WriteLineAsync(command));
taskStack.Push(consoleWriter.FlushAsync());
UpdateLogEvent?.Invoke(this, command);
}
public void InitializeService(Stream stream)
public void InitializeService()
{
CurrentState = ServiceState.Running;
this.consoleWriter = new StreamWriter(stream);
StateChangeEvent?.Invoke(this, true);
memoryStream = new MemoryStream();
this.consoleWriter = new StreamWriter(memoryStream);
StateChangeEvent?.Invoke(this, ServiceState.Running);
}
public byte[] GetLogBuffer()
{
return memoryStream.ToArray();
}
}
}

View File

@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using GameServiceWarden.API.Games;
using GameServiceWarden.Core.Games;
using GameServiceWarden.Core.Module;
namespace GameServiceWarden.Core.Tests.Modules.Games
{

View File

@@ -17,7 +17,7 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
public IEnumerable<string> Authors { get; private set; } = new string[] { "FakeAuthor", "FakeAuthor2" };
public IService InstantiateService(string workspace, bool clean)
public IService InstantiateService(string workspace)
{
return new FakeService(configurables);
}

View File

@@ -1,10 +1,11 @@
using System.Collections.Generic;
using System.IO;
using GameServiceWarden.Core.Games;
using GameServiceWarden.Core.Module;
using GameServiceWarden.Core.Logging;
using GameServiceWarden.API.Module;
using Xunit;
using Xunit.Abstractions;
using System.Text;
namespace GameServiceWarden.Core.Tests.Modules.Games
{
@@ -34,7 +35,7 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
IService stubService = new FakeService();
ServiceDescriptor serviceInfo = new ServiceDescriptor(stubService, SERVICE_NAME, "FakeModule", "FakeAssembly");
serviceInfo.Start();
Assert.True(serviceInfo.GetServiceState());
Assert.Equal(ServiceState.Running, serviceInfo.GetServiceState());
serviceInfo.Stop();
}
@@ -46,7 +47,7 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
ServiceDescriptor serviceInfo = new ServiceDescriptor(stubService, SERVICE_NAME, "FakeModule", "FakeAssembly");
serviceInfo.Start();
serviceInfo.Stop();
Assert.False(serviceInfo.GetServiceState());
Assert.Equal(ServiceState.Stopped, serviceInfo.GetServiceState());
}
[Fact]
@@ -89,7 +90,7 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
IService stubService = new FakeService();
ServiceDescriptor serviceInfo = new ServiceDescriptor(stubService, SERVICE_NAME, "FakeModule", "FakeAssembly");
//Then
Assert.False(serviceInfo.GetServiceState());
Assert.Equal(ServiceState.Stopped, serviceInfo.GetServiceState());
}
[Fact]
@@ -102,7 +103,7 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
//When
serviceInfo.Start();
//Then
Assert.True(serviceInfo.GetServiceState());
Assert.Equal(ServiceState.Running, serviceInfo.GetServiceState());
serviceInfo.Stop();
}
@@ -143,28 +144,24 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
}
[Fact]
public void ServiceLogPipeName_ServiceNotStarted_PipeNameReturned()
public void GetLogBuffer_CommandWritten_CommandLogged()
{
//Given
const string SERVICE_NAME = "ServiceLogPipeName_ServiceNotStarted_PipeNameReturned";
const string CMD = "hello";
const string SERVICE_NAME = "GetLogBuffer_CommandWritten_CommandLogged";
IService stubService = new FakeService();
ServiceDescriptor serviceInfo = new ServiceDescriptor(stubService, SERVICE_NAME, "FakeModule", "FakeAssembly");
//Then
Assert.NotNull(serviceInfo.ServiceLogPipeName);
}
[Fact]
public void ServiceLogPipeName_ServiceStarted_StreamReturned()
{
//Given
const string SERVICE_NAME = "ServiceLogPipeName_ServiceStarted_StreamReturned";
IService stubService = new FakeService();
ServiceDescriptor serviceInfo = new ServiceDescriptor(stubService, SERVICE_NAME, "FakeModule", "FakeAssembly");
//When
serviceInfo.Start();
//When
serviceInfo.ExecuteCommand(CMD);
//Then
Assert.NotNull(serviceInfo.ServiceLogPipeName);
serviceInfo.Stop();
using (MemoryStream mem = new MemoryStream(serviceInfo.GetLogBuffer()))
{
using (StreamReader reader = new StreamReader(mem))
{
Assert.Equal(CMD, reader.ReadLine());
}
}
}
}
}

View File

@@ -4,11 +4,12 @@ using System.IO;
using System.IO.Pipes;
using System.Threading;
using System.Threading.Tasks;
using GameServiceWarden.Core.Games;
using GameServiceWarden.Core.Module;
using GameServiceWarden.Core.Logging;
using GameServiceWarden.API.Module;
using Xunit;
using Xunit.Abstractions;
using System.Text;
[assembly: CollectionBehavior(DisableTestParallelization = true)]
namespace GameServiceWarden.Core.Tests.Modules.Games
@@ -28,7 +29,7 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
const string ASSEMBLY_NAME = "FakeAssembly";
const string FAKE_SERVICE_NAME = "CreateService_NewManager_NewServiceCreated";
FakePersistence<IReadOnlyDictionary<string, IServiceModule>> stubPersistentModuleDictionary = new FakePersistence<IReadOnlyDictionary<string, IServiceModule>>();
FakePersistence<ServiceDescriptor> stubPersistentServiceDictionary = new FakePersistence<ServiceDescriptor>();
FakePersistence<IReadOnlyDictionary<string, string>> stubPersistentServiceDictionary = new FakePersistence<IReadOnlyDictionary<string, string>>();
FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor();
ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary);
Dictionary<string, IServiceModule> stubAssemblyModulesDictionary = new Dictionary<string, IServiceModule>();
@@ -48,7 +49,7 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
const string ASSEMBLY_NAME = "FakeAssembly";
const string FAKE_SERVICE_NAME = "CreateService_OneService_ServiceDeleted";
FakePersistence<IReadOnlyDictionary<string, IServiceModule>> stubPersistentModuleDictionary = new FakePersistence<IReadOnlyDictionary<string, IServiceModule>>();
FakePersistence<ServiceDescriptor> stubPersistentServiceDictionary = new FakePersistence<ServiceDescriptor>();
FakePersistence<IReadOnlyDictionary<string, string>> stubPersistentServiceDictionary = new FakePersistence<IReadOnlyDictionary<string, string>>();
FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor();
ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary);
Dictionary<string, IServiceModule> stubAssemblyModulesDictionary = new Dictionary<string, IServiceModule>();
@@ -71,7 +72,7 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
const string ASSEMBLY_NAME = "FakeAssembly";
const string FAKE_SERVICE_PREFIX = "GetServiceNames_MultipleServices_AllCorrectNames_";
FakePersistence<IReadOnlyDictionary<string, IServiceModule>> stubPersistentModuleDictionary = new FakePersistence<IReadOnlyDictionary<string, IServiceModule>>();
FakePersistence<ServiceDescriptor> stubPersistentServiceDictionary = new FakePersistence<ServiceDescriptor>();
FakePersistence<IReadOnlyDictionary<string, string>> stubPersistentServiceDictionary = new FakePersistence<IReadOnlyDictionary<string, string>>();
FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor();
ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary);
Dictionary<string, IServiceModule> stubAssemblyModulesDictionary = new Dictionary<string, IServiceModule>();
@@ -97,7 +98,7 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
const string ASSEMBLY_NAME = "FakeAssembly";
const string FAKE_SERVICE_NAME = "GetServiceOptions_ThreeOptionService_CorrectOptions";
FakePersistence<IReadOnlyDictionary<string, IServiceModule>> stubPersistentModuleDictionary = new FakePersistence<IReadOnlyDictionary<string, IServiceModule>>();
FakePersistence<ServiceDescriptor> stubPersistentServiceDictionary = new FakePersistence<ServiceDescriptor>();
FakePersistence<IReadOnlyDictionary<string, string>> stubPersistentServiceDictionary = new FakePersistence<IReadOnlyDictionary<string, string>>();
FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor();
ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary);
Dictionary<string, IServiceModule> stubAssemblyModulesDictionary = new Dictionary<string, IServiceModule>();
@@ -122,7 +123,7 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
const string ASSEMBLY_NAME = "FakeAssembly";
const string FAKE_SERVICE_NAME = "SetandGetServiceOptionValue_OneOption_OptionChanged";
FakePersistence<IReadOnlyDictionary<string, IServiceModule>> stubPersistentModuleDictionary = new FakePersistence<IReadOnlyDictionary<string, IServiceModule>>();
FakePersistence<ServiceDescriptor> stubPersistentServiceDictionary = new FakePersistence<ServiceDescriptor>();
FakePersistence<IReadOnlyDictionary<string, string>> stubPersistentServiceDictionary = new FakePersistence<IReadOnlyDictionary<string, string>>();
FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor();
ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary);
Dictionary<string, IServiceModule> stubAssemblyModulesDictionary = new Dictionary<string, IServiceModule>();
@@ -145,7 +146,7 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
const string ASSEMBLY_NAME = "FakeAssembly";
const string FAKE_SERVICE_NAME = "GetServiceState_NotRunning_ReturnsNotRunningState";
FakePersistence<IReadOnlyDictionary<string, IServiceModule>> stubPersistentModuleDictionary = new FakePersistence<IReadOnlyDictionary<string, IServiceModule>>();
FakePersistence<ServiceDescriptor> stubPersistentServiceDictionary = new FakePersistence<ServiceDescriptor>();
FakePersistence<IReadOnlyDictionary<string, string>> stubPersistentServiceDictionary = new FakePersistence<IReadOnlyDictionary<string, string>>();
FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor();
ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary);
Dictionary<string, IServiceModule> stubAssemblyModulesDictionary = new Dictionary<string, IServiceModule>();
@@ -165,7 +166,7 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
const string ASSEMBLY_NAME = "FakeAssembly";
const string FAKE_SERVICE_NAME = "StartService_NotStarted_SuccessfulStart";
FakePersistence<IReadOnlyDictionary<string, IServiceModule>> stubPersistentModuleDictionary = new FakePersistence<IReadOnlyDictionary<string, IServiceModule>>();
FakePersistence<ServiceDescriptor> stubPersistentServiceDictionary = new FakePersistence<ServiceDescriptor>();
FakePersistence<IReadOnlyDictionary<string, string>> stubPersistentServiceDictionary = new FakePersistence<IReadOnlyDictionary<string, string>>();
FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor();
ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary);
Dictionary<string, IServiceModule> stubAssemblyModulesDictionary = new Dictionary<string, IServiceModule>();
@@ -187,7 +188,7 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
const string ASSEMBLY_NAME = "FakeAssembly";
const string FAKE_SERVICE_NAME = "StopService_ServiceStartedThenStopped_StateUpdated";
FakePersistence<IReadOnlyDictionary<string, IServiceModule>> stubPersistentModuleDictionary = new FakePersistence<IReadOnlyDictionary<string, IServiceModule>>();
FakePersistence<ServiceDescriptor> stubPersistentServiceDictionary = new FakePersistence<ServiceDescriptor>();
FakePersistence<IReadOnlyDictionary<string, string>> stubPersistentServiceDictionary = new FakePersistence<IReadOnlyDictionary<string, string>>();
FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor();
ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary);
Dictionary<string, IServiceModule> stubAssemblyModulesDictionary = new Dictionary<string, IServiceModule>();
@@ -203,14 +204,14 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
}
[Fact]
public void ExecuteCommand_CommandExecutedBeforeConnected_CommandLogged()
public void ExecuteCommand_ServiceStarted_CommandLogged()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
const string FAKE_SERVICE_NAME = "ExecuteCommand_CommandExecutedBeforeConnected_CommandLogged";
const string FAKE_SERVICE_NAME = "ExecuteCommand_ServiceStarted_CommandLogged";
const string COMMAND = "TEST";
FakePersistence<IReadOnlyDictionary<string, IServiceModule>> stubPersistentModuleDictionary = new FakePersistence<IReadOnlyDictionary<string, IServiceModule>>();
FakePersistence<ServiceDescriptor> stubPersistentServiceDictionary = new FakePersistence<ServiceDescriptor>();
FakePersistence<IReadOnlyDictionary<string, string>> stubPersistentServiceDictionary = new FakePersistence<IReadOnlyDictionary<string, string>>();
FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor();
ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary);
Dictionary<string, IServiceModule> stubAssemblyModulesDictionary = new Dictionary<string, IServiceModule>();
@@ -220,116 +221,16 @@ namespace GameServiceWarden.Core.Tests.Modules.Games
//When
serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubServiceModule.Name);
serviceManager.StartService(FAKE_SERVICE_NAME);
string pipeName = serviceManager.GetLogPipeNames()[FAKE_SERVICE_NAME];
NamedPipeClientStream clientStream = new NamedPipeClientStream(".", pipeName, PipeDirection.In);
serviceManager.ExecuteCommand(FAKE_SERVICE_NAME, COMMAND);
clientStream.Connect(1000);
Thread.Sleep(1000);
//Then
byte[] buffer = new byte[1024 * 8];
CancellationTokenSource cancelToken = new CancellationTokenSource(2000);
ValueTask<int> task = clientStream.ReadAsync(buffer, cancelToken.Token);
Assert.False(task.AsTask().Wait(1000));
serviceManager.StopService(FAKE_SERVICE_NAME);
cancelToken.Dispose();
}
[Fact]
public void ExecuteCommand_CommandExecutedAfterConnected_CommandLogged()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
const string FAKE_SERVICE_NAME = "ExecuteCommand_CommandExecutedAfterConnected_CommandLogged";
const string COMMAND = "TEST";
FakePersistence<IReadOnlyDictionary<string, IServiceModule>> stubPersistentModuleDictionary = new FakePersistence<IReadOnlyDictionary<string, IServiceModule>>();
FakePersistence<ServiceDescriptor> stubPersistentServiceDictionary = new FakePersistence<ServiceDescriptor>();
FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor();
ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary);
Dictionary<string, IServiceModule> stubAssemblyModulesDictionary = new Dictionary<string, IServiceModule>();
IServiceModule stubServiceModule = new FakeServiceModule();
stubAssemblyModulesDictionary.Add(stubServiceModule.Name, stubServiceModule);
stubPersistentModuleDictionary.AddToPersistence(ASSEMBLY_NAME, stubAssemblyModulesDictionary);
//When
serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubServiceModule.Name);
serviceManager.StartService(FAKE_SERVICE_NAME);
string pipeName = serviceManager.GetLogPipeNames()[FAKE_SERVICE_NAME];
NamedPipeClientStream clientStream = new NamedPipeClientStream(".", pipeName, PipeDirection.In);
clientStream.Connect(1000);
Thread.Sleep(1000);
serviceManager.ExecuteCommand(FAKE_SERVICE_NAME, COMMAND);
//Then
using (StreamReader reader = new StreamReader(clientStream))
using (MemoryStream mem = new MemoryStream(serviceManager.GetLogBuffer()[FAKE_SERVICE_NAME]))
{
CancellationTokenSource cancelToken = new CancellationTokenSource(2000);
string message = null;
Task task = Task.Run(() => message = reader.ReadLine(), cancelToken.Token);
Assert.True(task.Wait(1000));
Assert.True(COMMAND.Equals(message), $"Received message \"{message}\" when expecting \"{COMMAND}\"");
cancelToken.Dispose();
}
serviceManager.StopService(FAKE_SERVICE_NAME);
}
[Fact]
public void ExecuteCommand_CommandExecutedAfterMultipleLogListenersConnected_CommandLogged()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
const string FAKE_SERVICE_NAME = "ExecuteCommand_CommandExecutedAfterMultipleLogListenersConnected_CommandLogged";
const string COMMAND = "TEST";
FakePersistence<IReadOnlyDictionary<string, IServiceModule>> stubPersistentModuleDictionary = new FakePersistence<IReadOnlyDictionary<string, IServiceModule>>();
FakePersistence<ServiceDescriptor> stubPersistentServiceDictionary = new FakePersistence<ServiceDescriptor>();
FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor();
ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary);
Dictionary<string, IServiceModule> stubAssemblyModulesDictionary = new Dictionary<string, IServiceModule>();
IServiceModule stubServiceModule = new FakeServiceModule();
stubAssemblyModulesDictionary.Add(stubServiceModule.Name, stubServiceModule);
stubPersistentModuleDictionary.AddToPersistence(ASSEMBLY_NAME, stubAssemblyModulesDictionary);
//When
serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubServiceModule.Name);
serviceManager.StartService(FAKE_SERVICE_NAME);
string pipeName = serviceManager.GetLogPipeNames()[FAKE_SERVICE_NAME];
NamedPipeClientStream[] clientStreams = new NamedPipeClientStream[5];
for (int i = 0; i < clientStreams.Length; i++)
{
clientStreams[i] = new NamedPipeClientStream(".", pipeName, PipeDirection.In);
clientStreams[i].Connect(1000);
}
Thread.Sleep(1000);
serviceManager.ExecuteCommand(FAKE_SERVICE_NAME, COMMAND);
//Then
for (int i = 0; i < clientStreams.Length; i++)
{
using (StreamReader reader = new StreamReader(clientStreams[i]))
using (StreamReader reader = new StreamReader(mem))
{
string message = null;
Task clientTask = Task.Run(() => message = reader.ReadLine());
Assert.True(clientTask.Wait(1000));
Assert.True(COMMAND.Equals(message), $"Received message \"{message}\" when expecting \"{COMMAND}\"");
Assert.Equal(COMMAND, reader.ReadLine());
}
}
Task task = Task.Run(() => serviceManager.StopService(FAKE_SERVICE_NAME));
Assert.True(task.Wait(5000)); //TODO FIX WHY THIS IS HAPPENING!!!!!
}
[Fact]
public void GetServiceConsoleStream_ServiceStopped_ExceptionThrown()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
const string FAKE_SERVICE_NAME = "GetServiceConsoleStream_ServiceStopped_ExceptionThrown";
FakePersistence<IReadOnlyDictionary<string, IServiceModule>> stubPersistentModuleDictionary = new FakePersistence<IReadOnlyDictionary<string, IServiceModule>>();
FakePersistence<ServiceDescriptor> stubPersistentServiceDictionary = new FakePersistence<ServiceDescriptor>();
FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor();
ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary);
Dictionary<string, IServiceModule> stubAssemblyModulesDictionary = new Dictionary<string, IServiceModule>();
IServiceModule stubServiceModule = new FakeServiceModule();
stubAssemblyModulesDictionary.Add(stubServiceModule.Name, stubServiceModule);
stubPersistentModuleDictionary.AddToPersistence(ASSEMBLY_NAME, stubAssemblyModulesDictionary);
//When
serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubServiceModule.Name);
//Then
Assert.Throws<KeyNotFoundException>(() => serviceManager.GetLogPipeNames()[FAKE_SERVICE_NAME]);
serviceManager.StopService(FAKE_SERVICE_NAME);
}
}
}