2021-04-09 02:36:08 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2021-04-19 06:34:45 +00:00
|
|
|
using GameServiceWarden.Core.Module;
|
2021-04-22 21:03:09 +00:00
|
|
|
using GameServiceWarden.ModuleFramework;
|
2021-04-09 02:36:08 +00:00
|
|
|
using Xunit;
|
|
|
|
using Xunit.Abstractions;
|
2021-04-23 01:39:22 +00:00
|
|
|
using SimpleLogger;
|
2021-04-09 02:36:08 +00:00
|
|
|
|
2021-04-19 19:40:11 +00:00
|
|
|
namespace GameServiceWarden.Core.Tests.Modules
|
2021-04-09 02:36:08 +00:00
|
|
|
{
|
|
|
|
// Testing convention from: https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices
|
|
|
|
// fakes are generic test objects,
|
|
|
|
// mocks are the objects being asserted upon,
|
|
|
|
// stubs are objects used as part of the test.
|
|
|
|
[CollectionDefinition("Service", DisableParallelization = true)]
|
|
|
|
public class ServiceDescriptorTest
|
|
|
|
{
|
|
|
|
private readonly ITestOutputHelper output;
|
|
|
|
private readonly XUnitLogger logger;
|
|
|
|
|
|
|
|
public ServiceDescriptorTest(ITestOutputHelper output)
|
|
|
|
{
|
|
|
|
this.output = output;
|
|
|
|
logger = new XUnitLogger(output);
|
|
|
|
Logger.AddLogListener(logger);
|
|
|
|
}
|
|
|
|
|
|
|
|
//MethodTested_ScenarioTested_ExpectedBehavior
|
|
|
|
[Fact]
|
|
|
|
public void Start_FromStopped_StateIsRunning()
|
|
|
|
{
|
|
|
|
//Arrange, Act, Assert
|
|
|
|
const string SERVICE_NAME = "Start_FromStopped_StateIsRunning";
|
|
|
|
IService stubService = new FakeService();
|
|
|
|
ServiceDescriptor serviceInfo = new ServiceDescriptor(stubService, SERVICE_NAME, "FakeModule", "FakeAssembly");
|
|
|
|
serviceInfo.Start();
|
2021-04-19 06:34:45 +00:00
|
|
|
Assert.Equal(ServiceState.Running, serviceInfo.GetServiceState());
|
2021-04-09 02:36:08 +00:00
|
|
|
serviceInfo.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void Stop_FromStart_Stopped()
|
|
|
|
{
|
|
|
|
const string SERVICE_NAME = "Stop_FromStart_Stopped";
|
|
|
|
IService stubService = new FakeService();
|
|
|
|
ServiceDescriptor serviceInfo = new ServiceDescriptor(stubService, SERVICE_NAME, "FakeModule", "FakeAssembly");
|
|
|
|
serviceInfo.Start();
|
|
|
|
serviceInfo.Stop();
|
2021-04-19 06:34:45 +00:00
|
|
|
Assert.Equal(ServiceState.Stopped, serviceInfo.GetServiceState());
|
2021-04-09 02:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void GetConfigurableOptions_ServiceStopped_ReturnsConfigurables()
|
|
|
|
{
|
|
|
|
//Given
|
|
|
|
const string SERVICE_NAME = "GetConfigurableOptions_ServiceStopped_ReturnsConfigurables";
|
|
|
|
FakeService stubService = new FakeService();
|
|
|
|
FakeServiceConfigurable stubConfigurable = new FakeServiceConfigurable("Option");
|
|
|
|
HashSet<IServiceConfigurable> configurables = new HashSet<IServiceConfigurable>();
|
|
|
|
configurables.Add(stubConfigurable);
|
|
|
|
stubService.Configurables = configurables;
|
|
|
|
ServiceDescriptor serviceInfo = new ServiceDescriptor(stubService, SERVICE_NAME, "FakeModule", "FakeAssembly");
|
|
|
|
//Then
|
|
|
|
Assert.Contains<string>(stubConfigurable.OptionName, serviceInfo.GetConfigurableOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void SetAndGetConfigurationValue_ServiceStopped_AppropriateValueReturned()
|
|
|
|
{
|
|
|
|
//Given
|
|
|
|
const string SERVICE_NAME = "SetAndGetConfigurationValue_ServiceStopped_AppropriateValueReturned";
|
|
|
|
FakeService stubService = new FakeService();
|
|
|
|
FakeServiceConfigurable stubConfigurable = new FakeServiceConfigurable("Option");
|
|
|
|
HashSet<IServiceConfigurable> configurables = new HashSet<IServiceConfigurable>();
|
|
|
|
configurables.Add(stubConfigurable);
|
|
|
|
stubService.Configurables = configurables;
|
|
|
|
ServiceDescriptor serviceInfo = new ServiceDescriptor(stubService, SERVICE_NAME, "FakeModule", "FakeAssembly");
|
|
|
|
//When
|
|
|
|
serviceInfo.SetConfigurableValue(stubConfigurable.OptionName, "success");
|
|
|
|
//Then
|
|
|
|
Assert.True("success".Equals(serviceInfo.GetConfigurableValue(stubConfigurable.OptionName)));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void GetServiceState_ServiceNotStarted_ReturnsStoppedState()
|
|
|
|
{
|
|
|
|
//Given
|
|
|
|
const string SERVICE_NAME = "GetServiceState_ServiceNotStarted_ReturnsStoppedState";
|
|
|
|
IService stubService = new FakeService();
|
|
|
|
ServiceDescriptor serviceInfo = new ServiceDescriptor(stubService, SERVICE_NAME, "FakeModule", "FakeAssembly");
|
|
|
|
//Then
|
2021-04-19 06:34:45 +00:00
|
|
|
Assert.Equal(ServiceState.Stopped, serviceInfo.GetServiceState());
|
2021-04-09 02:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void GetServiceState_ServiceStarted_ReturnsRunningState()
|
|
|
|
{
|
|
|
|
//Given
|
|
|
|
const string SERVICE_NAME = "GetServiceState_ServiceStarted_ReturnsRunningState";
|
|
|
|
IService stubService = new FakeService();
|
|
|
|
ServiceDescriptor serviceInfo = new ServiceDescriptor(stubService, SERVICE_NAME, "FakeModule", "FakeAssembly");
|
|
|
|
//When
|
|
|
|
serviceInfo.Start();
|
|
|
|
//Then
|
2021-04-19 06:34:45 +00:00
|
|
|
Assert.Equal(ServiceState.Running, serviceInfo.GetServiceState());
|
2021-04-09 02:36:08 +00:00
|
|
|
serviceInfo.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void GetModuleName_ServiceNotStarted_ReturnsSetName()
|
|
|
|
{
|
|
|
|
//Given
|
|
|
|
const string SERVICE_NAME = "GetModuleName_ServiceNotStarted_ReturnsSetName";
|
|
|
|
const string MODULE_NAME = "FakeModule";
|
|
|
|
IService stubService = new FakeService();
|
|
|
|
ServiceDescriptor serviceInfo = new ServiceDescriptor(stubService, SERVICE_NAME, MODULE_NAME, "FakeAssembly");
|
|
|
|
//Then
|
|
|
|
Assert.Equal(MODULE_NAME, serviceInfo.GetModuleName());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void GetAssemblyName_ServiceNotStarted_ReturnsSetAssemblyName()
|
|
|
|
{
|
|
|
|
//Given
|
|
|
|
const string ASSEMBLY_NAME = "FakeAssembly";
|
|
|
|
const string SERVICE_NAME = "GetAssemblyName_ServiceNotStarted_ReturnsSetAssemblyName";
|
|
|
|
IService stubService = new FakeService();
|
|
|
|
ServiceDescriptor serviceInfo = new ServiceDescriptor(stubService, SERVICE_NAME, "FakeModule", ASSEMBLY_NAME);
|
|
|
|
//Then
|
|
|
|
Assert.Equal(ASSEMBLY_NAME, serviceInfo.GetAssemblyName());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void GetServiceName_ServiceNotStartedSingleThread_ServiceNameReturned()
|
|
|
|
{
|
|
|
|
//Given
|
|
|
|
const string SERVICE_NAME = "GetServiceName_ServiceNotStartedSingleThread_ServiceNameReturned";
|
|
|
|
IService stubService = new FakeService();
|
|
|
|
//When
|
|
|
|
ServiceDescriptor serviceInfo = new ServiceDescriptor(stubService, SERVICE_NAME, "FakeModule", "FakeAssemblyName");
|
|
|
|
//Then
|
|
|
|
Assert.True(SERVICE_NAME.Equals(serviceInfo.ServiceName));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
2021-04-19 06:34:45 +00:00
|
|
|
public void GetLogBuffer_CommandWritten_CommandLogged()
|
2021-04-09 02:36:08 +00:00
|
|
|
{
|
|
|
|
//Given
|
2021-04-19 06:34:45 +00:00
|
|
|
const string CMD = "hello";
|
|
|
|
const string SERVICE_NAME = "GetLogBuffer_CommandWritten_CommandLogged";
|
2021-04-09 02:36:08 +00:00
|
|
|
IService stubService = new FakeService();
|
|
|
|
ServiceDescriptor serviceInfo = new ServiceDescriptor(stubService, SERVICE_NAME, "FakeModule", "FakeAssembly");
|
|
|
|
serviceInfo.Start();
|
2021-04-19 06:34:45 +00:00
|
|
|
//When
|
|
|
|
serviceInfo.ExecuteCommand(CMD);
|
2021-04-09 02:36:08 +00:00
|
|
|
//Then
|
2021-04-19 06:34:45 +00:00
|
|
|
using (MemoryStream mem = new MemoryStream(serviceInfo.GetLogBuffer()))
|
|
|
|
{
|
|
|
|
using (StreamReader reader = new StreamReader(mem))
|
|
|
|
{
|
|
|
|
Assert.Equal(CMD, reader.ReadLine());
|
|
|
|
}
|
|
|
|
}
|
2021-04-09 02:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|