2021-04-09 02:36:08 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Threading.Tasks;
|
2021-04-22 21:03:09 +00:00
|
|
|
using GameServiceWarden.ModuleFramework;
|
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
|
|
|
{
|
|
|
|
public class FakeService : IService
|
|
|
|
{
|
|
|
|
public IReadOnlyCollection<IServiceConfigurable> Configurables { get; set; }
|
|
|
|
|
2021-04-19 06:34:45 +00:00
|
|
|
public event EventHandler<ServiceState> StateChangeEvent;
|
|
|
|
public event EventHandler<string> UpdateLogEvent;
|
2021-04-09 02:36:08 +00:00
|
|
|
|
2021-04-19 06:34:45 +00:00
|
|
|
public ServiceState CurrentState { get; private set; } = ServiceState.Stopped;
|
|
|
|
private MemoryStream memoryStream;
|
2021-04-09 02:36:08 +00:00
|
|
|
private StreamWriter consoleWriter;
|
|
|
|
private Stack<Task> taskStack = new Stack<Task>();
|
|
|
|
|
|
|
|
public FakeService(params IServiceConfigurable[] configurables)
|
|
|
|
{
|
|
|
|
HashSet<IServiceConfigurable> modifiable = new HashSet<IServiceConfigurable>();
|
|
|
|
foreach (IServiceConfigurable configurable in configurables)
|
|
|
|
{
|
|
|
|
modifiable.Add(configurable);
|
|
|
|
}
|
|
|
|
this.Configurables = modifiable;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ElegantShutdown()
|
|
|
|
{
|
|
|
|
CurrentState = ServiceState.Stopped;
|
2021-04-19 06:34:45 +00:00
|
|
|
StateChangeEvent?.Invoke(this, ServiceState.Stopped);
|
2021-04-09 02:36:08 +00:00
|
|
|
Task task;
|
|
|
|
while(taskStack.TryPop(out task)) {
|
|
|
|
if (task.IsCompleted) {
|
|
|
|
task.Wait();
|
|
|
|
} else {
|
|
|
|
throw new InvalidOperationException("A task was not completed.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ExecuteCommand(string command)
|
|
|
|
{
|
|
|
|
taskStack.Push(consoleWriter.WriteLineAsync(command));
|
|
|
|
taskStack.Push(consoleWriter.FlushAsync());
|
2021-04-19 06:34:45 +00:00
|
|
|
UpdateLogEvent?.Invoke(this, command);
|
2021-04-09 02:36:08 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 06:34:45 +00:00
|
|
|
public void InitializeService()
|
2021-04-09 02:36:08 +00:00
|
|
|
{
|
|
|
|
CurrentState = ServiceState.Running;
|
2021-04-19 06:34:45 +00:00
|
|
|
memoryStream = new MemoryStream();
|
|
|
|
this.consoleWriter = new StreamWriter(memoryStream);
|
|
|
|
StateChangeEvent?.Invoke(this, ServiceState.Running);
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] GetLogBuffer()
|
|
|
|
{
|
|
|
|
return memoryStream.ToArray();
|
2021-04-09 02:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|