2020-12-26 19:36:09 +00:00
|
|
|
using System.IO;
|
2020-12-28 06:43:02 +00:00
|
|
|
using GameServiceWarden.Core.Games;
|
2020-12-26 19:36:09 +00:00
|
|
|
using GameServiceWarden.ModuleAPI;
|
|
|
|
using Xunit;
|
|
|
|
|
2020-12-28 06:43:02 +00:00
|
|
|
namespace GameServiceWarden.Core.Tests.Modules.Games
|
2020-12-26 19:36:09 +00:00
|
|
|
{
|
2020-12-28 06:43:02 +00:00
|
|
|
public class GameServiceManagerTest
|
2020-12-26 19:36:09 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void CreateService_NewManager_NewServiceCreated()
|
|
|
|
{
|
|
|
|
//Given
|
|
|
|
const string ASSEMBLY_NAME = "FakeAssembly";
|
|
|
|
const string FAKE_SERVICE_NAME = "FakeService";
|
2020-12-30 05:14:42 +00:00
|
|
|
FakePersistentDictionary<IGameServiceModule> stubPersistentModuleDictionary = new FakePersistentDictionary<IGameServiceModule>();
|
|
|
|
FakePersistentDictionary<GameServiceInfo> stubPersistentServiceDictionary = new FakePersistentDictionary<GameServiceInfo>();
|
|
|
|
GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary);
|
2020-12-26 19:36:09 +00:00
|
|
|
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
|
|
|
//When
|
2020-12-30 05:14:42 +00:00
|
|
|
stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule);
|
|
|
|
serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
|
2020-12-26 19:36:09 +00:00
|
|
|
//Then
|
|
|
|
Assert.Contains<string>(FAKE_SERVICE_NAME, serviceManager.GetServiceNames());
|
|
|
|
}
|
|
|
|
|
2020-12-28 01:14:29 +00:00
|
|
|
[Fact]
|
|
|
|
public void CreateService_OneService_ServiceDeleted()
|
|
|
|
{
|
|
|
|
//Given
|
|
|
|
const string ASSEMBLY_NAME = "FakeAssembly";
|
|
|
|
const string FAKE_SERVICE_NAME = "FakeService";
|
2020-12-30 05:14:42 +00:00
|
|
|
FakePersistentDictionary<IGameServiceModule> stubPersistentModuleDictionary = new FakePersistentDictionary<IGameServiceModule>();
|
|
|
|
FakePersistentDictionary<GameServiceInfo> stubPersistentServiceDictionary = new FakePersistentDictionary<GameServiceInfo>();
|
|
|
|
GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary);
|
2020-12-28 01:14:29 +00:00
|
|
|
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
|
|
|
//When
|
2020-12-30 05:14:42 +00:00
|
|
|
stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule);
|
|
|
|
serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
|
2020-12-28 01:14:29 +00:00
|
|
|
serviceManager.DeleteService(FAKE_SERVICE_NAME);
|
|
|
|
//Then
|
|
|
|
Assert.DoesNotContain<string>(FAKE_SERVICE_NAME, serviceManager.GetServiceNames());
|
|
|
|
}
|
|
|
|
|
2020-12-26 19:36:09 +00:00
|
|
|
[Fact]
|
|
|
|
public void GetServiceNames_MultipleServices_AllCorrectNames()
|
|
|
|
{
|
|
|
|
//Given
|
|
|
|
const string ASSEMBLY_NAME = "FakeAssembly";
|
|
|
|
const string FAKE_SERVICE_PREFIX = "FakeService_";
|
2020-12-30 05:14:42 +00:00
|
|
|
FakePersistentDictionary<IGameServiceModule> stubPersistentModuleDictionary = new FakePersistentDictionary<IGameServiceModule>();
|
|
|
|
FakePersistentDictionary<GameServiceInfo> stubPersistentServiceDictionary = new FakePersistentDictionary<GameServiceInfo>();
|
|
|
|
GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary);
|
2020-12-26 19:36:09 +00:00
|
|
|
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
|
|
|
//When
|
2020-12-30 05:14:42 +00:00
|
|
|
stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule);
|
2020-12-26 19:36:09 +00:00
|
|
|
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";
|
2020-12-30 05:14:42 +00:00
|
|
|
FakePersistentDictionary<IGameServiceModule> stubPersistentModuleDictionary = new FakePersistentDictionary<IGameServiceModule>();
|
|
|
|
FakePersistentDictionary<GameServiceInfo> stubPersistentServiceDictionary = new FakePersistentDictionary<GameServiceInfo>();
|
|
|
|
GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary);
|
2020-12-26 19:36:09 +00:00
|
|
|
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule(
|
2020-12-28 06:43:02 +00:00
|
|
|
new FakeGameConfigurable("A"),
|
|
|
|
new FakeGameConfigurable("B"),
|
|
|
|
new FakeGameConfigurable("C")
|
2020-12-26 19:36:09 +00:00
|
|
|
);
|
|
|
|
//When
|
2020-12-30 05:14:42 +00:00
|
|
|
stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule);
|
2020-12-26 19:36:09 +00:00
|
|
|
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";
|
2020-12-30 05:14:42 +00:00
|
|
|
FakePersistentDictionary<IGameServiceModule> stubPersistentModuleDictionary = new FakePersistentDictionary<IGameServiceModule>();
|
|
|
|
FakePersistentDictionary<GameServiceInfo> stubPersistentServiceDictionary = new FakePersistentDictionary<GameServiceInfo>();
|
|
|
|
GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary);
|
2020-12-26 19:36:09 +00:00
|
|
|
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule(
|
2020-12-28 06:43:02 +00:00
|
|
|
new FakeGameConfigurable("A")
|
2020-12-26 19:36:09 +00:00
|
|
|
);
|
|
|
|
//When
|
2020-12-30 05:14:42 +00:00
|
|
|
stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule);
|
2020-12-26 19:36:09 +00:00
|
|
|
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";
|
2020-12-30 05:14:42 +00:00
|
|
|
FakePersistentDictionary<IGameServiceModule> stubPersistentModuleDictionary = new FakePersistentDictionary<IGameServiceModule>();
|
|
|
|
FakePersistentDictionary<GameServiceInfo> stubPersistentServiceDictionary = new FakePersistentDictionary<GameServiceInfo>();
|
|
|
|
GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary);
|
2020-12-26 19:36:09 +00:00
|
|
|
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
|
|
|
//When
|
2020-12-30 05:14:42 +00:00
|
|
|
stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule);
|
2020-12-26 19:36:09 +00:00
|
|
|
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";
|
2020-12-30 05:14:42 +00:00
|
|
|
FakePersistentDictionary<IGameServiceModule> stubPersistentModuleDictionary = new FakePersistentDictionary<IGameServiceModule>();
|
|
|
|
FakePersistentDictionary<GameServiceInfo> stubPersistentServiceDictionary = new FakePersistentDictionary<GameServiceInfo>();
|
|
|
|
GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary);
|
2020-12-26 19:36:09 +00:00
|
|
|
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
|
|
|
//When
|
2020-12-30 05:14:42 +00:00
|
|
|
stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule);
|
2020-12-26 19:36:09 +00:00
|
|
|
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";
|
2020-12-30 05:14:42 +00:00
|
|
|
FakePersistentDictionary<IGameServiceModule> stubPersistentModuleDictionary = new FakePersistentDictionary<IGameServiceModule>();
|
|
|
|
FakePersistentDictionary<GameServiceInfo> stubPersistentServiceDictionary = new FakePersistentDictionary<GameServiceInfo>();
|
|
|
|
GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary);
|
2020-12-26 19:36:09 +00:00
|
|
|
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
|
|
|
//When
|
2020-12-30 05:14:42 +00:00
|
|
|
stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule);
|
2020-12-26 19:36:09 +00:00
|
|
|
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";
|
2020-12-30 05:14:42 +00:00
|
|
|
FakePersistentDictionary<IGameServiceModule> stubPersistentModuleDictionary = new FakePersistentDictionary<IGameServiceModule>();
|
|
|
|
FakePersistentDictionary<GameServiceInfo> stubPersistentServiceDictionary = new FakePersistentDictionary<GameServiceInfo>();
|
|
|
|
GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary);
|
2020-12-26 19:36:09 +00:00
|
|
|
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
|
|
|
//When
|
2020-12-30 05:14:42 +00:00
|
|
|
stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule);
|
2020-12-26 19:36:09 +00:00
|
|
|
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";
|
2020-12-30 05:14:42 +00:00
|
|
|
FakePersistentDictionary<IGameServiceModule> stubPersistentModuleDictionary = new FakePersistentDictionary<IGameServiceModule>();
|
|
|
|
FakePersistentDictionary<GameServiceInfo> stubPersistentServiceDictionary = new FakePersistentDictionary<GameServiceInfo>();
|
|
|
|
GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary);
|
2020-12-26 19:36:09 +00:00
|
|
|
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
|
|
|
//When
|
2020-12-30 05:14:42 +00:00
|
|
|
stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule);
|
2020-12-26 19:36:09 +00:00
|
|
|
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";
|
2020-12-30 05:14:42 +00:00
|
|
|
FakePersistentDictionary<IGameServiceModule> stubPersistentModuleDictionary = new FakePersistentDictionary<IGameServiceModule>();
|
|
|
|
FakePersistentDictionary<GameServiceInfo> stubPersistentServiceDictionary = new FakePersistentDictionary<GameServiceInfo>();
|
|
|
|
GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary);
|
2020-12-26 19:36:09 +00:00
|
|
|
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
|
|
|
|
//When
|
2020-12-30 05:14:42 +00:00
|
|
|
stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule);
|
2020-12-26 19:36:09 +00:00
|
|
|
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
|
|
|
|
//Then
|
|
|
|
Assert.Null(serviceManager.GetServiceConsoleStream(SERVICE_NAME));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|