Make C# tests not crash the whole test command!

This commit is contained in:
Fabian Stamm 2022-07-16 21:34:58 +00:00
parent df9576bbaf
commit e110259684

View File

@ -4,47 +4,57 @@ using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
class TestSrvimpl : Example.TestServiceServer<int> { class TestSrvimpl : Example.TestServiceServer<int>
public TestSrvimpl(): base() {} {
public override async Task<Example.AddValueResponse> AddValuesSingleParam(AddValueRequest request, int ctx) { public TestSrvimpl() : base() { }
public override async Task<Example.AddValueResponse> AddValuesSingleParam(AddValueRequest request, int ctx)
{
var res = new Example.AddValueResponse(); var res = new Example.AddValueResponse();
res.value = 1; res.value = 1;
return res; return res;
} }
public override async Task<double> AddValuesMultipleParams(double value1,double value2,int ctx) { public override async Task<double> AddValuesMultipleParams(double value1, double value2, int ctx)
{
return value1 + value2; return value1 + value2;
} }
public override async Task ReturningVoid(double param1,int ctx) { public override async Task ReturningVoid(double param1, int ctx)
{
} }
public override void OnEvent(string param1,int ctx) { public override void OnEvent(string param1, int ctx)
{
Console.WriteLine($"OnEvent {param1}"); Console.WriteLine($"OnEvent {param1}");
} }
public override async Task<IList<double>> FunctionWithArrayAsParamAndReturn(List<double> values1,List<double> values2, int ctx) { public override async Task<IList<double>> FunctionWithArrayAsParamAndReturn(List<double> values1, List<double> values2, int ctx)
{
var l = new List<double>(); var l = new List<double>();
l.Append(1); l.Append(1);
return l; return l;
} }
public override async Task ThrowingError(int ctx) { public override async Task ThrowingError(int ctx)
{
throw new Exception("This is a remote error :)"); throw new Exception("This is a remote error :)");
} }
} }
class CopyTransportS2 : Example.JRpcTransport { class CopyTransportS2 : Example.JRpcTransport
{
CopyTransportS1 tr1; CopyTransportS1 tr1;
public Queue<string> backlog = new Queue<string>(); public Queue<string> backlog = new Queue<string>();
public CopyTransportS2(CopyTransportS1 tr1) { public CopyTransportS2(CopyTransportS1 tr1)
{
this.tr1 = tr1; this.tr1 = tr1;
} }
public override Task Write(string data) { public override Task Write(string data)
{
Console.WriteLine("--> " + data); Console.WriteLine("--> " + data);
this.tr1.SendPacketEvent(data); this.tr1.SendPacketEvent(data);
@ -52,28 +62,34 @@ class CopyTransportS2 : Example.JRpcTransport {
} }
} }
class CopyTransportS1 : Example.JRpcTransport { class CopyTransportS1 : Example.JRpcTransport
{
public Queue<string> backlog = new Queue<string>(); public Queue<string> backlog = new Queue<string>();
public CopyTransportS2 tr2; public CopyTransportS2 tr2;
public CopyTransportS1() { public CopyTransportS1()
{
this.tr2 = new CopyTransportS2(this); this.tr2 = new CopyTransportS2(this);
} }
public override Task Write(string data) { public override Task Write(string data)
{
Console.WriteLine("<-- " + data); Console.WriteLine("<-- " + data);
this.tr2.SendPacketEvent(data); this.tr2.SendPacketEvent(data);
return Task.CompletedTask; return Task.CompletedTask;
} }
} }
class Program { class Program
public static void Main() { {
public static void Main()
{
Program.Start().Wait(); Program.Start().Wait();
} }
public static async Task Start() { public static async Task Start()
{
var server = new Example.JRpcServer<int>(); var server = new Example.JRpcServer<int>();
server.AddService(new TestSrvimpl()); server.AddService(new TestSrvimpl());
var transport = new CopyTransportS1(); var transport = new CopyTransportS1();
@ -82,11 +98,19 @@ class Program {
var client = new Example.JRpcClient(transport.tr2); var client = new Example.JRpcClient(transport.tr2);
var testService = new Example.TestServiceClient(client); var testService = new Example.TestServiceClient(client);
var result = await testService.AddValuesMultipleParams(1,2); var result = await testService.AddValuesMultipleParams(1, 2);
Console.WriteLine($"Add 1 + 2 = {result}"); Console.WriteLine($"Add 1 + 2 = {result}");
await testService.ThrowingError(); try
{
await testService.ThrowingError();
throw new Exception("No error was issued, even though it was expected!");
}
catch (JRpcException err)
{
Console.WriteLine("Error successfully catched: ", err.Message);
}
} }
} }