ServiceManager completed and ServiceInfo modified to increase encapsulation.
ServiceManager tested and ServiceInfo tests updated to reflect changes.
This commit is contained in:
@@ -5,7 +5,12 @@ namespace GameServiceWarden.Host.Tests.Modules
|
||||
public class FakeConfigurable : IConfigurable
|
||||
{
|
||||
private string value;
|
||||
public string OptionName => "FakeOption";
|
||||
public string OptionName { get; private set; }
|
||||
|
||||
public FakeConfigurable(string optionName)
|
||||
{
|
||||
this.OptionName = optionName;
|
||||
}
|
||||
|
||||
public string GetValue()
|
||||
{
|
||||
|
@@ -0,0 +1,25 @@
|
||||
using System.Collections.Generic;
|
||||
using GameServiceWarden.ModuleAPI;
|
||||
|
||||
namespace GameServiceWarden.Host.Tests.Modules
|
||||
{
|
||||
public class FakeGameServiceModule : IGameServiceModule
|
||||
{
|
||||
public string Name => "FakeModule";
|
||||
|
||||
public string Description => "A fake module for testing.";
|
||||
|
||||
private IConfigurable[] configurables;
|
||||
public FakeGameServiceModule(params IConfigurable[] configurables)
|
||||
{
|
||||
this.configurables = configurables;
|
||||
}
|
||||
|
||||
public IEnumerable<string> Authors { get; private set; } = new string[] { "FakeAuthor", "FakeAuthor2" };
|
||||
|
||||
public IGameService CreateGameService()
|
||||
{
|
||||
return new FakeService(configurables);
|
||||
}
|
||||
}
|
||||
}
|
@@ -7,12 +7,24 @@ namespace GameServiceWarden.Host.Tests.Modules
|
||||
{
|
||||
public class FakeService : IGameService
|
||||
{
|
||||
public IReadOnlyCollection<IConfigurable> Configurables { get; set; } = new HashSet<IConfigurable>();
|
||||
public IReadOnlyCollection<IConfigurable> Configurables { get; set; }
|
||||
|
||||
public event EventHandler<ServiceState> StateChangeEvent;
|
||||
|
||||
public ServiceState CurrentState { get; private set; } = ServiceState.Stopped;
|
||||
|
||||
private TextWriter consoleStream;
|
||||
|
||||
public FakeService(params IConfigurable[] configurables)
|
||||
{
|
||||
HashSet<IConfigurable> modifiable = new HashSet<IConfigurable>();
|
||||
foreach (IConfigurable configurable in configurables)
|
||||
{
|
||||
modifiable.Add(configurable);
|
||||
}
|
||||
this.Configurables = modifiable;
|
||||
}
|
||||
|
||||
public void ElegantShutdown()
|
||||
{
|
||||
CurrentState = ServiceState.Stopped;
|
||||
@@ -21,12 +33,15 @@ namespace GameServiceWarden.Host.Tests.Modules
|
||||
|
||||
public void ExecuteCommand(string command)
|
||||
{
|
||||
consoleStream.WriteLine(command);
|
||||
consoleStream.Flush();
|
||||
}
|
||||
|
||||
public void InitializeService(TextWriter stream)
|
||||
{
|
||||
CurrentState = ServiceState.Running;
|
||||
StateChangeEvent?.Invoke(this, CurrentState);
|
||||
this.consoleStream = stream;
|
||||
}
|
||||
}
|
||||
}
|
@@ -36,20 +36,34 @@ namespace GameServiceWarden.Host.Tests.Modules
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetConfigurables_ServiceStopped_ReturnsConfigurables()
|
||||
public void GetConfigurableOptions_ServiceStopped_ReturnsConfigurables()
|
||||
{
|
||||
//Given
|
||||
FakeService stubService = new FakeService();
|
||||
FakeConfigurable stubConfigurable = new FakeConfigurable();
|
||||
FakeConfigurable stubConfigurable = new FakeConfigurable("Option");
|
||||
HashSet<IConfigurable> configurables = new HashSet<IConfigurable>();
|
||||
configurables.Add(stubConfigurable);
|
||||
stubService.Configurables = configurables;
|
||||
ServiceInfo serviceInfo = new ServiceInfo(stubService, "FakeModule", "FakeAssembly");
|
||||
//Then
|
||||
Assert.Contains<string>(stubConfigurable.OptionName, serviceInfo.GetConfigurableOptions());
|
||||
serviceInfo.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetAndGetConfigurationValue_ServiceStopped_AppropriateValueReturned()
|
||||
{
|
||||
//Given
|
||||
FakeService stubService = new FakeService();
|
||||
FakeConfigurable stubConfigurable = new FakeConfigurable("Option");
|
||||
HashSet<IConfigurable> configurables = new HashSet<IConfigurable>();
|
||||
configurables.Add(stubConfigurable);
|
||||
stubService.Configurables = configurables;
|
||||
ServiceInfo serviceInfo = new ServiceInfo(stubService, "FakeModule", "FakeAssembly");
|
||||
//When
|
||||
serviceInfo.Start();
|
||||
serviceInfo.SetConfigurableValue(stubConfigurable.OptionName, "success");
|
||||
//Then
|
||||
Assert.Contains(stubConfigurable, serviceInfo.GetConfigurables().Values);
|
||||
serviceInfo.Dispose();
|
||||
Assert.Equal<string>("success", serviceInfo.GetConfigurableValue(stubConfigurable.OptionName));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
214
tests/GameServiceWarden.Host.Tests/Modules/ServiceManagerTest.cs
Normal file
214
tests/GameServiceWarden.Host.Tests/Modules/ServiceManagerTest.cs
Normal file
@@ -0,0 +1,214 @@
|
||||
using System.IO;
|
||||
using GameServiceWarden.Host.Modules;
|
||||
using GameServiceWarden.ModuleAPI;
|
||||
using Xunit;
|
||||
|
||||
namespace GameServiceWarden.Host.Tests.Modules
|
||||
{
|
||||
public class ServiceManagerTest
|
||||
{
|
||||
[Fact]
|
||||
public void AddModule_NewManager_SuccessfulAddition()
|
||||
{
|
||||
//Given
|
||||
const string ASSEMBLY_NAME = "FakeAssembly";
|
||||
ServiceManager serviceManager = new ServiceManager();
|
||||
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
||||
//When
|
||||
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
|
||||
//Then
|
||||
Assert.Contains<string>(ASSEMBLY_NAME, serviceManager.GetAssemblyNames());
|
||||
Assert.Contains<string>(stubGameServiceModule.Name, serviceManager.GetModuleNames(ASSEMBLY_NAME));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveModule_NewManager_SuccessfulRemoval()
|
||||
{
|
||||
//Given
|
||||
const string ASSEMBLY_NAME = "FakeAssembly";
|
||||
ServiceManager serviceManager = new ServiceManager();
|
||||
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
||||
//When
|
||||
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
|
||||
serviceManager.RemoveModule(ASSEMBLY_NAME, stubGameServiceModule.Name);
|
||||
//Then
|
||||
Assert.DoesNotContain<string>(ASSEMBLY_NAME, serviceManager.GetAssemblyNames());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateService_NewManager_NewServiceCreated()
|
||||
{
|
||||
//Given
|
||||
const string ASSEMBLY_NAME = "FakeAssembly";
|
||||
const string FAKE_SERVICE_NAME = "FakeService";
|
||||
ServiceManager serviceManager = new ServiceManager();
|
||||
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
||||
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
|
||||
//When
|
||||
serviceManager.CreateService("FakeService", ASSEMBLY_NAME, stubGameServiceModule.Name);
|
||||
//Then
|
||||
Assert.Contains<string>(FAKE_SERVICE_NAME, serviceManager.GetServiceNames());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetServiceNames_MultipleServices_AllCorrectNames()
|
||||
{
|
||||
//Given
|
||||
const string ASSEMBLY_NAME = "FakeAssembly";
|
||||
const string FAKE_SERVICE_PREFIX = "FakeService_";
|
||||
ServiceManager serviceManager = new ServiceManager();
|
||||
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
||||
//When
|
||||
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
serviceManager.CreateService(FAKE_SERVICE_PREFIX + i, ASSEMBLY_NAME, stubGameServiceModule.Name);
|
||||
}
|
||||
//Then
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Assert.Contains<string>(FAKE_SERVICE_PREFIX + i, serviceManager.GetServiceNames());
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetServiceOptions_ThreeOptionService_CorrectOptions()
|
||||
{
|
||||
//Given
|
||||
const string ASSEMBLY_NAME = "FakeAssembly";
|
||||
const string SERVICE_NAME = "FakeService";
|
||||
ServiceManager serviceManager = new ServiceManager();
|
||||
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule(
|
||||
new FakeConfigurable("A"),
|
||||
new FakeConfigurable("B"),
|
||||
new FakeConfigurable("C")
|
||||
);
|
||||
//When
|
||||
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
|
||||
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
|
||||
//Then
|
||||
Assert.Contains<string>("A", serviceManager.GetServiceOptions(SERVICE_NAME));
|
||||
Assert.Contains<string>("B", serviceManager.GetServiceOptions(SERVICE_NAME));
|
||||
Assert.Contains<string>("C", serviceManager.GetServiceOptions(SERVICE_NAME));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetandGetServiceOptionValue_OneOption_OptionChanged()
|
||||
{
|
||||
//Given
|
||||
const string ASSEMBLY_NAME = "FakeAssembly";
|
||||
const string SERVICE_NAME = "FakeService";
|
||||
ServiceManager serviceManager = new ServiceManager();
|
||||
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule(
|
||||
new FakeConfigurable("A")
|
||||
);
|
||||
//When
|
||||
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
|
||||
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
|
||||
serviceManager.SetServiceOptionValue(SERVICE_NAME, "A", "Test");
|
||||
//Then
|
||||
Assert.Equal<string>("Test", serviceManager.GetServiceOptionValue(SERVICE_NAME, "A"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetServiceState_NotRunning_ReturnsNotRunningState()
|
||||
{
|
||||
//Given
|
||||
const string ASSEMBLY_NAME = "FakeAssembly";
|
||||
const string SERVICE_NAME = "FakeService";
|
||||
ServiceManager serviceManager = new ServiceManager();
|
||||
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
||||
//When
|
||||
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
|
||||
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
|
||||
//Then
|
||||
Assert.Equal<ServiceState>(ServiceState.Stopped, serviceManager.GetServiceState(SERVICE_NAME));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetServiceState_Running_ReturnsNotRunningState()
|
||||
{
|
||||
//Given
|
||||
const string ASSEMBLY_NAME = "FakeAssembly";
|
||||
const string SERVICE_NAME = "FakeService";
|
||||
ServiceManager serviceManager = new ServiceManager();
|
||||
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
||||
//When
|
||||
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
|
||||
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
|
||||
serviceManager.StartService(SERVICE_NAME);
|
||||
//Then
|
||||
Assert.Equal<ServiceState>(ServiceState.Running, serviceManager.GetServiceState(SERVICE_NAME));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StartService_NotStarted_SuccessfulStart()
|
||||
{
|
||||
//Given
|
||||
const string ASSEMBLY_NAME = "FakeAssembly";
|
||||
const string SERVICE_NAME = "FakeService";
|
||||
ServiceManager serviceManager = new ServiceManager();
|
||||
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
||||
//When
|
||||
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
|
||||
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
|
||||
serviceManager.StartService(SERVICE_NAME);
|
||||
//Then
|
||||
Assert.Equal<ServiceState>(ServiceState.Running, serviceManager.GetServiceState(SERVICE_NAME));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StopService_Stopped_StateUpdated()
|
||||
{
|
||||
//Given
|
||||
const string ASSEMBLY_NAME = "FakeAssembly";
|
||||
const string SERVICE_NAME = "FakeService";
|
||||
ServiceManager serviceManager = new ServiceManager();
|
||||
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
||||
//When
|
||||
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
|
||||
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
|
||||
serviceManager.StartService(SERVICE_NAME);
|
||||
serviceManager.StopService(SERVICE_NAME);
|
||||
//Then
|
||||
Assert.Equal<ServiceState>(ServiceState.Stopped, serviceManager.GetServiceState(SERVICE_NAME));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExecuteCommand_ServiceStarted_CommandLogged()
|
||||
{
|
||||
//Given
|
||||
const string ASSEMBLY_NAME = "FakeAssembly";
|
||||
const string SERVICE_NAME = "FakeService";
|
||||
ServiceManager serviceManager = new ServiceManager();
|
||||
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
||||
//When
|
||||
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
|
||||
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
|
||||
serviceManager.StartService(SERVICE_NAME);
|
||||
serviceManager.ExecuteCommand(SERVICE_NAME, "Test");
|
||||
//Then
|
||||
Stream stream = serviceManager.GetServiceConsoleStream(SERVICE_NAME);
|
||||
stream.Position = 0;
|
||||
using (StreamReader reader = new StreamReader(stream))
|
||||
{
|
||||
Assert.Equal<string>("Test", reader.ReadLine());
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetServiceConsoleStream_ServiceStopped_ExceptionThrown()
|
||||
{
|
||||
//Given
|
||||
const string ASSEMBLY_NAME = "FakeAssembly";
|
||||
const string SERVICE_NAME = "FakeService";
|
||||
ServiceManager serviceManager = new ServiceManager();
|
||||
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
||||
//When
|
||||
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
|
||||
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
|
||||
//Then
|
||||
Assert.Null(serviceManager.GetServiceConsoleStream(SERVICE_NAME));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user