using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using GameServiceWarden.API.Module; namespace GameServiceWarden.Core.Tests.Modules { public class FakeService : IService { public IReadOnlyCollection Configurables { get; set; } public event EventHandler StateChangeEvent; public event EventHandler UpdateLogEvent; public ServiceState CurrentState { get; private set; } = ServiceState.Stopped; private MemoryStream memoryStream; private StreamWriter consoleWriter; private Stack taskStack = new Stack(); public FakeService(params IServiceConfigurable[] configurables) { HashSet modifiable = new HashSet(); foreach (IServiceConfigurable configurable in configurables) { modifiable.Add(configurable); } this.Configurables = modifiable; } public void ElegantShutdown() { CurrentState = ServiceState.Stopped; StateChangeEvent?.Invoke(this, ServiceState.Stopped); 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()); UpdateLogEvent?.Invoke(this, command); } public void InitializeService() { CurrentState = ServiceState.Running; memoryStream = new MemoryStream(); this.consoleWriter = new StreamWriter(memoryStream); StateChangeEvent?.Invoke(this, ServiceState.Running); } public byte[] GetLogBuffer() { return memoryStream.ToArray(); } } }