тесты

This commit is contained in:
2025-01-12 01:06:09 +03:00
parent a78494a7b1
commit c9cd6ec0c0
6 changed files with 53 additions and 28 deletions
+6
View File
@@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mROA", "mROA\mROA.csproj", "{AE4E93F6-7B91-41FC-9BEA-2C8C2CBE1CB6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mROA.Test", "mROA.Test\mROA.Test.csproj", "{D0E5760B-BB6E-453A-B396-A972CD94F133}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -12,5 +14,9 @@ Global
{AE4E93F6-7B91-41FC-9BEA-2C8C2CBE1CB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AE4E93F6-7B91-41FC-9BEA-2C8C2CBE1CB6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AE4E93F6-7B91-41FC-9BEA-2C8C2CBE1CB6}.Release|Any CPU.Build.0 = Release|Any CPU
{D0E5760B-BB6E-453A-B396-A972CD94F133}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D0E5760B-BB6E-453A-B396-A972CD94F133}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0E5760B-BB6E-453A-B396-A972CD94F133}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0E5760B-BB6E-453A-B396-A972CD94F133}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
+1
View File
@@ -2,5 +2,6 @@ namespace mROA;
public interface IInteractionModule
{
void SetMessageHandler(Action<int, string> handler);
void SendTo(int clientId, byte[] message);
}
+3 -3
View File
@@ -2,7 +2,7 @@
public class FinalCommandExecution : ICommandExecution
{
public Guid ExecutionId { get; init; } = new Guid();
public Guid ExecutionId { get; init; } = Guid.NewGuid();
public int ClientId { get; set; }
public int CommandId { get; set; }
public object? Result { get; set; }
@@ -10,7 +10,7 @@ public class FinalCommandExecution : ICommandExecution
public class ExeptionCommandExecution : ICommandExecution
{
public Guid ExecutionId { get; init; } = new Guid();
public Guid ExecutionId { get; init; } = Guid.NewGuid();
public int ClientId { get; set; }
public int CommandId { get; set; }
public string Reason { get; set; }
@@ -18,7 +18,7 @@ public class ExeptionCommandExecution : ICommandExecution
public class AsyncCommandExecution : ICommandExecution
{
public Guid ExecutionId { get; init; } = new Guid();
public Guid ExecutionId { get; init; } = Guid.NewGuid();
public int ClientId { get; set; }
public int CommandId { get; set; }
+23 -7
View File
@@ -4,9 +4,18 @@ using System.Text.Json.Serialization;
namespace mROA.Implementation;
public class InputModule(IInteractionModule dataSource, IExecuteModule executeModule)
: IInputModule
public class InputModule : IInputModule
{
private readonly IInteractionModule _dataSource;
private readonly IExecuteModule _executeModule;
public InputModule(IInteractionModule dataSource, IExecuteModule executeModule)
{
_dataSource = dataSource;
_executeModule = executeModule;
_dataSource.SetMessageHandler(HandleIncomingRequest);
}
public void HandleIncomingRequest(int clientId, string command)
{
var type = JsonDocument.Parse(command).RootElement.GetProperty("RequestTypeId").GetInt32();
@@ -29,17 +38,24 @@ public class InputModule(IInteractionModule dataSource, IExecuteModule executeMo
break;
}
var response = executeModule.Execute(request);
var texted = JsonSerializer.Serialize(response);
request.ClientId = clientId;
var response = _executeModule.Execute(request);
var texted = string.Empty;
if (response is FinalCommandExecution finalCommand)
{
texted = JsonSerializer.Serialize(finalCommand);
}else if (response is AsyncCommandExecution asyncCommand)
{
texted = JsonSerializer.Serialize(asyncCommand);
}
var binary = Encoding.UTF8.GetBytes(texted);
dataSource.SendTo(clientId, binary);
_dataSource.SendTo(clientId, binary);
}
public void PostResponse(ICommandExecution call)
{
var texted = JsonSerializer.Serialize(call);
var binary = Encoding.UTF8.GetBytes(texted);
dataSource.SendTo(call.ClientId, binary);
_dataSource.SendTo(call.ClientId, binary);
}
}
@@ -1,18 +0,0 @@
namespace mROA.Implementation;
public class ConsoleInteractionChanel(IInputModule inputModule) : IInteractionModule
{
public void StartListeningKeyboard()
{
while (true)
{
var input = Console.ReadLine();
inputModule.HandleIncomingRequest(123, input);
}
}
public void SendTo(int clientId, byte[] message)
{
Console.WriteLine("{0}: {1}", clientId, System.Text.Encoding.UTF8.GetString(message));
}
}
@@ -0,0 +1,20 @@
namespace mROA.Implementation;
public class ProgramlyInteractionChanel : IInteractionModule
{
public void PassCommand(int clientId, string message) => _handler(clientId, message);
private Action<int, string> _handler;
public List<string> OutputBuffer = new List<string>();
public void SetMessageHandler(Action<int, string> handler)
{
_handler = handler;
}
public void SendTo(int clientId, byte[] message)
{
Console.WriteLine("{0}: {1}", clientId, System.Text.Encoding.UTF8.GetString(message));
OutputBuffer.Add(System.Text.Encoding.UTF8.GetString(message));
}
}