ServiceManager completed and ServiceInfo modified to increase encapsulation.
ServiceManager tested and ServiceInfo tests updated to reflect changes.
This commit is contained in:
parent
3a49502970
commit
4996982c89
@ -27,7 +27,7 @@ namespace GameServiceWarden.Host.Modules
|
||||
private readonly IGameService service;
|
||||
private readonly string assemblyName;
|
||||
private readonly string moduleName;
|
||||
private readonly Dictionary<string, IConfigurable> configurables = new Dictionary<string, IConfigurable>();
|
||||
private readonly IReadOnlyDictionary<string, IConfigurable> configurables;
|
||||
private bool disposed;
|
||||
|
||||
public ServiceInfo(IGameService service, string moduleName, string assemblyName)
|
||||
@ -37,10 +37,12 @@ namespace GameServiceWarden.Host.Modules
|
||||
this.assemblyName = assemblyName ?? throw new ArgumentNullException("assemblyName");
|
||||
this.service.StateChangeEvent += OnServiceStateChange;
|
||||
|
||||
Dictionary<string, IConfigurable> configurables = new Dictionary<string, IConfigurable>();
|
||||
foreach (IConfigurable configurable in service.Configurables)
|
||||
{
|
||||
configurables.Add(configurable.OptionName, configurable);
|
||||
}
|
||||
this.configurables = new ReadOnlyDictionary<string, IConfigurable>(configurables);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -91,12 +93,24 @@ namespace GameServiceWarden.Host.Modules
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the possible <see cref="IConfigurable"/>'s for this service.
|
||||
/// Gets the possible <see cref="IConfigurable"/>'s names for this service.
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="IReadOnlyDictionary{string, IConfigurable}"/> is returned where the string is the option name and the configurable is what handles actually changing the values.</returns>
|
||||
public IReadOnlyDictionary<string, IConfigurable> GetConfigurables()
|
||||
/// <returns>A <see cref="ISet{string}"/> returned where the string is the option's name.</returns>
|
||||
public ISet<string> GetConfigurableOptions()
|
||||
{
|
||||
return new ReadOnlyDictionary<string, IConfigurable>(this.configurables);
|
||||
return new HashSet<string>(this.configurables.Keys);
|
||||
}
|
||||
|
||||
public bool SetConfigurableValue(string configurationName, string value)
|
||||
{
|
||||
if (!this.configurables.ContainsKey(configurationName)) throw new KeyNotFoundException($"Unable to find option with name \"{configurationName}\".");
|
||||
return this.configurables[configurationName].SetValue(value);
|
||||
}
|
||||
|
||||
public string GetConfigurableValue(string configurationName)
|
||||
{
|
||||
if (!this.configurables.ContainsKey(configurationName)) throw new KeyNotFoundException($"Unable to find option with name \"{configurationName}\".");
|
||||
return this.configurables[configurationName].GetValue();
|
||||
}
|
||||
|
||||
/// <returns>The <see cref="ServiceState"/> that this service is currently in.</returns>
|
||||
|
@ -7,8 +7,8 @@ namespace GameServiceWarden.Host.Modules
|
||||
{
|
||||
public class ServiceManager
|
||||
{
|
||||
private Dictionary<string, ServiceInfo> services = new Dictionary<string, ServiceInfo>();
|
||||
private Dictionary<string, Dictionary<string, IGameServiceModule>> modules = new Dictionary<string, Dictionary<string, IGameServiceModule>>();
|
||||
private readonly Dictionary<string, ServiceInfo> services = new Dictionary<string, ServiceInfo>();
|
||||
private readonly Dictionary<string, Dictionary<string, IGameServiceModule>> modules = new Dictionary<string, Dictionary<string, IGameServiceModule>>();
|
||||
|
||||
public void AddModule(string assemblyName, IGameServiceModule module)
|
||||
{
|
||||
@ -23,9 +23,25 @@ namespace GameServiceWarden.Host.Modules
|
||||
if (modules[assemblyName].Count == 0) modules.Remove(assemblyName);
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<string> GetAssemblyNames()
|
||||
{
|
||||
string[] names = new string[modules.Count];
|
||||
modules.Keys.CopyTo(names, 0);
|
||||
return names;
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<string> GetModuleNames(string assembly)
|
||||
{
|
||||
if (!modules.ContainsKey(assembly)) throw new KeyNotFoundException($"No loaded assembly named \"{assembly}\".");
|
||||
string[] names = new string[modules.Count];
|
||||
modules[assembly].Keys.CopyTo(names, 0);
|
||||
return names;
|
||||
|
||||
}
|
||||
|
||||
public void CreateService(string serviceName, string assemblyName, string moduleName)
|
||||
{
|
||||
if (!modules.ContainsKey(assemblyName) || modules[assemblyName].ContainsKey(moduleName)) throw new KeyNotFoundException($"No module registered from \"{assemblyName}\" named \"{moduleName}\".");
|
||||
if (!modules.ContainsKey(assemblyName) || !modules[assemblyName].ContainsKey(moduleName)) throw new KeyNotFoundException($"No module registered from \"{assemblyName}\" named \"{moduleName}\".");
|
||||
if (services.ContainsKey(serviceName)) throw new ArgumentException($"Service of Name \"{serviceName}\" already exists.");
|
||||
|
||||
services.Add(serviceName, new ServiceInfo(modules[assemblyName][moduleName].CreateGameService(), moduleName, assemblyName));
|
||||
@ -42,15 +58,27 @@ namespace GameServiceWarden.Host.Modules
|
||||
{
|
||||
if (!services.ContainsKey(serviceName)) throw new KeyNotFoundException($"Service under name \"{serviceName}\" not found.");
|
||||
ServiceInfo serviceInfo = services[serviceName];
|
||||
return serviceInfo.GetConfigurables().Keys;
|
||||
return serviceInfo.GetConfigurableOptions();
|
||||
}
|
||||
|
||||
public bool SetServiceOptionValue(string serviceName, string optionName, string value)
|
||||
{
|
||||
if (!services.ContainsKey(serviceName)) throw new KeyNotFoundException($"Service under name \"{serviceName}\" not found.");
|
||||
if (!services[serviceName].GetConfigurables().ContainsKey(optionName)) throw new KeyNotFoundException($"Option \"{optionName}\" for service \"{serviceName}\" not found.");
|
||||
IConfigurable configurable = services[serviceName].GetConfigurables()[optionName];
|
||||
return configurable.SetValue(value);
|
||||
if (!services[serviceName].GetConfigurableOptions().Contains(optionName)) throw new KeyNotFoundException($"Option \"{optionName}\" for service \"{serviceName}\" not found.");
|
||||
return services[serviceName].SetConfigurableValue(optionName, value);
|
||||
}
|
||||
|
||||
public string GetServiceOptionValue(string serviceName, string optionName)
|
||||
{
|
||||
if (!services.ContainsKey(serviceName)) throw new KeyNotFoundException($"Service under name \"{serviceName}\" not found.");
|
||||
if (!services[serviceName].GetConfigurableOptions().Contains(optionName)) throw new KeyNotFoundException($"Option \"{optionName}\" for service \"{serviceName}\" not found.");
|
||||
return services[serviceName].GetConfigurableValue(optionName);
|
||||
}
|
||||
|
||||
public ServiceState GetServiceState(string serviceName)
|
||||
{
|
||||
if (!services.ContainsKey(serviceName)) throw new KeyNotFoundException($"Service under name \"{serviceName}\" not found.");
|
||||
return services[serviceName].GetServiceState();
|
||||
}
|
||||
|
||||
public void StartService(string serviceName)
|
||||
|
@ -1,26 +1,26 @@
|
||||
<mxfile host="65bd71144e" modified="2020-12-24T22:37:11.750Z" agent="5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Code/1.52.1 Chrome/83.0.4103.122 Electron/9.3.5 Safari/537.36" etag="I0usdzu5wRPewA4HBn-M" version="13.10.0" type="embed">
|
||||
<mxfile host="65bd71144e" modified="2020-12-26T07:51:18.537Z" agent="5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Code/1.52.1 Chrome/83.0.4103.122 Electron/9.3.5 Safari/537.36" etag="Iqq-QtiSg1pcXFCyEV9I" version="13.10.0" type="embed">
|
||||
<diagram id="LHR7ubqCPd17_LyHkaH9" name="Structure">
|
||||
<mxGraphModel dx="895" dy="510" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
|
||||
<mxGraphModel dx="850" dy="740" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
|
||||
<root>
|
||||
<mxCell id="0"/>
|
||||
<mxCell id="1" parent="0"/>
|
||||
<mxCell id="v9q6W0nyI9kZyF3peKlB-5" style="edgeStyle=orthogonalEdgeStyle;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=1;entryDx=0;entryDy=0;jumpStyle=none;dashed=1;endArrow=block;endFill=0;exitX=1;exitY=0;exitDx=0;exitDy=0;sketch=1;curved=1;" parent="1" source="v9q6W0nyI9kZyF3peKlB-1" target="v9q6W0nyI9kZyF3peKlB-4" edge="1">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="v9q6W0nyI9kZyF3peKlB-1" value="<table border="1" width="100%" cellpadding="4" style="width: 100% ; height: 100% ; border-collapse: collapse"><tbody><tr><th align="center">ServiceInfo (entity)</th></tr><tr><td align="center">- serviceName: string<br>- controlLock: object<br>- state: ServiceState<br>- service: IGameService<br>- serviceConsoleStream: Stream<br>- moduleName: string<br>- assemblyName: string<br>- Dictionary&lt;string, IConfigurable&gt;<br>- disposed: bool</td></tr><tr><td align="center">+ Start(): void<br>+ Stop(): void<br>+ GetConfigurables(): IReadOnlyDictionary&lt;string, IConfigurable&gt;<br>+ GetServiceState(): ServiceState<br>+ getModuleName(): string<br>+ GetassemblyName(): string<br>+ SetServiceName(name: string): void // Implemented as property<br>+ GetServiceName(): string // Implemented as property<br>+ GetServiceConsoleStream(): Stream // Implemented as property<br>- OnServiceStateChange(curr: ServiceState,&nbsp;prev: ServiceState): void<br># Dispose(disposing: bool): void<br>+ Dispose(): void<br></td></tr></tbody></table>" style="text;html=1;fillColor=none;overflow=fill;strokeColor=#f0f0f0;sketch=1;" parent="1" vertex="1">
|
||||
<mxCell id="v9q6W0nyI9kZyF3peKlB-1" value="<table border="1" width="100%" cellpadding="4" style="width: 100% ; height: 100% ; border-collapse: collapse"><tbody><tr><th align="center">ServiceInfo (entity)</th></tr><tr><td align="center">- serviceName: string<br>- controlLock: object<br>- state: ServiceState<br>- service: IGameService<br>- serviceConsoleStream: Stream<br>- moduleName: string<br>- assemblyName: string<br>- Dictionary&lt;string, IConfigurable&gt;<br>- disposed: bool</td></tr><tr><td align="center">+ Start(): void<br>+ Stop(): void<br>+ GetConfigurableOptions(): ISet&lt;string&gt;<br>+ SetConfigurableValue(configurationName: string, value: string): bool<br>+ GetConfigurableValue(configurationName: string): string<br>+ GetServiceState(): ServiceState<br>+ getModuleName(): string<br>+ GetassemblyName(): string<br>+ SetServiceName(name: string): void // Implemented as property<br>+ GetServiceName(): string // Implemented as property<br>+ GetServiceConsoleStream(): Stream // Implemented as property<br>- OnServiceStateChange(curr: ServiceState,&nbsp;prev: ServiceState): void<br># Dispose(disposing: bool): void<br>+ Dispose(): void<br></td></tr></tbody></table>" style="text;html=1;fillColor=none;overflow=fill;strokeColor=#f0f0f0;sketch=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="950" y="950" width="400" height="410" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="6" style="edgeStyle=orthogonalEdgeStyle;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;fontStyle=1;sketch=1;curved=1;" parent="1" source="v9q6W0nyI9kZyF3peKlB-2" target="v9q6W0nyI9kZyF3peKlB-1" edge="1">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="v9q6W0nyI9kZyF3peKlB-2" value="<table border="1" width="100%" cellpadding="4" style="width: 100% ; height: 100% ; border-collapse: collapse"><tbody><tr><th align="center">ServiceManager (Use-case)</th></tr><tr><td align="center">- services: Dictionary&lt;string, Service&gt;<br>- modules: Dictionary&lt;string, Dictionary&lt;string, IGameServiceModule&gt;&gt;</td></tr><tr><td align="center">+ AddModule(assemblyName: string, module: IGameServiceModule): void<br>+ RemoveModule(assemblyName: string, moduleName string): void<br>+ CreateService(serviceName: string, assemblyName: string, moduleName: string): void<br>+ GetServiceNames(): IReadOnlyCollection&lt;string&gt;<br>+ GetServiceOptions(serviceName: string): IEnumerable&lt;string&gt;<br>+ SetServiceOptionValue(serviceName: string, optionName: string, string: value): bool<br>+ StartService(serviceName: string): void<br>+ StopService(serviceName: string): void<br>+ ExecuteCommand(serviceName: string, command: string): void<br>+ GetServiceConsoleStream(): Stream</td></tr></tbody></table>" style="text;html=1;fillColor=none;overflow=fill;strokeColor=#f0f0f0;sketch=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="640" y="610" width="490" height="280" as="geometry"/>
|
||||
<mxCell id="v9q6W0nyI9kZyF3peKlB-2" value="<table border="1" width="100%" cellpadding="4" style="width: 100% ; height: 100% ; border-collapse: collapse"><tbody><tr><th align="center">ServiceManager (Use-case)</th></tr><tr><td align="center">- services: Dictionary&lt;string, Service&gt;<br>- modules: Dictionary&lt;string, Dictionary&lt;string, IGameServiceModule&gt;&gt;</td></tr><tr><td align="center">+ AddModule(assemblyName: string, module: IGameServiceModule): void<br>+ RemoveModule(assemblyName: string, moduleName string): void<br>+ CreateService(serviceName: string, assemblyName: string, moduleName: string): void<br>+ GetServiceNames(): IReadOnlyCollection&lt;string&gt;<br>+ GetServiceOptionsValue(serviceName: string): IEnumerable&lt;string&gt;<br>+ SetServiceOptionValue(serviceName: string, optionName: string, string: value): bool<br>+ GetServiceState(serviceName: string): ServiceState<br>+ StartService(serviceName: string): void<br>+ StopService(serviceName: string): void<br>+ ExecuteCommand(serviceName: string, command: string): void<br>+ GetServiceConsoleStream(): Stream</td></tr></tbody></table>" style="text;html=1;fillColor=none;overflow=fill;strokeColor=#f0f0f0;sketch=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="640" y="570" width="490" height="290" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="v9q6W0nyI9kZyF3peKlB-4" value="IDisposable" style="shape=ext;double=1;whiteSpace=wrap;html=1;sketch=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="1410" y="820" width="120" height="80" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="v9q6W0nyI9kZyF3peKlB-13" value="<table border="1" width="100%" cellpadding="4" style="width: 100% ; height: 100% ; border-collapse: collapse"><tbody><tr><th align="center">ModuleLoader (Gateway)</th></tr><tr><td align="center"></td></tr><tr><td align="center">- InstantiateServiceables(assembly: Assembly): void<br>- LoadAssembly(path: string): void<br>+ LoadModules(path: string): IEnumerable&lt;IGameServiceModule&gt;<br>+ LoadAllModules(path: string[]): IEnumerable&lt;IGameServiceModule&gt;<br>+ LoadAllModules(path: IEnumerable&lt;string&gt;): IEnumerable&lt;IGameServiceModule&gt;<br></td></tr></tbody></table>" style="text;html=1;fillColor=none;overflow=fill;strokeColor=#f0f0f0;sketch=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="110" y="280" width="490" height="250" as="geometry"/>
|
||||
<mxGeometry x="90" y="280" width="490" height="250" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="titdvn9p0HDrujjw1N2D-4" style="edgeStyle=orthogonalEdgeStyle;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.75;exitY=0;exitDx=0;exitDy=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;dashed=1;endArrow=block;endFill=0;sketch=1;curved=1;" parent="1" source="v9q6W0nyI9kZyF3peKlB-16" target="v9q6W0nyI9kZyF3peKlB-27" edge="1">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
@ -38,7 +38,7 @@
|
||||
<mxGeometry x="640" y="280" width="432.5" height="250" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="v9q6W0nyI9kZyF3peKlB-23" value="<table border="1" width="100%" cellpadding="4" style="width: 100% ; height: 100% ; border-collapse: collapse"><tbody><tr><th align="center">ServiceGateway (Gateway)</th></tr><tr><td align="center">- dataDirectory: string</td></tr><tr><td align="center">+ SaveService(name: string, assemblyName: string, moduleName: string): void<br>+ GetServiceName(path: string): string<br>+ GetServiceModuleName(path: string): string<br>+ GetAllServiceInfoPaths() IEnumerable&lt;string&gt;</td></tr></tbody></table>" style="text;html=1;fillColor=none;overflow=fill;strokeColor=#f0f0f0;sketch=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="110" y="545" width="490" height="230" as="geometry"/>
|
||||
<mxGeometry x="90" y="545" width="490" height="230" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="v9q6W0nyI9kZyF3peKlB-27" value="<table border="1" width="100%" cellpadding="4" style="width: 100% ; height: 100% ; border-collapse: collapse"><tbody><tr><th align="center">ICommandable &lt;I&gt;</th></tr><tr><td align="center"></td></tr><tr><td align="center">+ GetPrefix(): string<br>+ Validate(string input): bool<br>+ Execute(string input): void<br>+ Help(): string</td></tr></tbody></table>" style="text;html=1;fillColor=none;overflow=fill;strokeColor=#f0f0f0;sketch=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="900" y="50" width="250" height="170" as="geometry"/>
|
||||
|
@ -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