Refactoring test server/client to use async/await more consistently
diff --git a/test/netstd/Client/Performance/PerformanceTests.cs b/test/netstd/Client/Performance/PerformanceTests.cs
index 2c79aa6..f9eb9e4 100644
--- a/test/netstd/Client/Performance/PerformanceTests.cs
+++ b/test/netstd/Client/Performance/PerformanceTests.cs
@@ -28,6 +28,8 @@
using System.Diagnostics;
using Thrift.Transport;
+#pragma warning disable CS1998 // no await in async method
+
namespace Client.Tests
{
public class PerformanceTests
@@ -37,9 +39,9 @@
private TMemoryBufferTransport MemBuffer;
private TTransport Transport;
private LayeredChoice Layered;
- private readonly TConfiguration Configuration = new TConfiguration();
+ private readonly TConfiguration Configuration = new();
- internal static int Execute()
+ internal static async Task<int> Execute()
{
var instance = new PerformanceTests();
instance.ProtocolPeformanceTestAsync().Wait();
diff --git a/test/netstd/Client/Program.cs b/test/netstd/Client/Program.cs
index bcc02a2..09a9cf2 100644
--- a/test/netstd/Client/Program.cs
+++ b/test/netstd/Client/Program.cs
@@ -19,13 +19,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
+using System.Threading.Tasks;
using ThriftTest;
namespace Client
{
public class Program
{
- public static int Main(string[] args)
+ static async Task<int> Main(string[] args)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
@@ -46,15 +47,15 @@
case "client": // crosstest wants to pass this, so just emit a hint and ignore
Console.WriteLine("Hint: The 'client' argument is no longer required.");
argslist.RemoveAt(0);
- return TestClient.Execute(argslist);
+ return await TestClient.Execute(argslist);
case "--performance":
case "--performance-test":
- return Tests.PerformanceTests.Execute();
+ return await Tests.PerformanceTests.Execute();
case "--help":
PrintHelp();
return 0;
default:
- return TestClient.Execute(argslist);
+ return await TestClient.Execute(argslist);
}
}
diff --git a/test/netstd/Client/TestClient.cs b/test/netstd/Client/TestClient.cs
index 2d18cf1..38bccf1 100644
--- a/test/netstd/Client/TestClient.cs
+++ b/test/netstd/Client/TestClient.cs
@@ -17,6 +17,7 @@
#pragma warning disable IDE0066 // switch expression
#pragma warning disable IDE0057 // substring
+#pragma warning disable CS1998 // no await in async method
using System;
using System.Collections.Generic;
@@ -322,7 +323,7 @@
numIterations = param.numIterations;
}
- public void Execute()
+ public async Task Execute()
{
if (done)
{
@@ -335,7 +336,7 @@
try
{
if (!transport.IsOpen)
- transport.OpenAsync(MakeTimeoutToken()).GetAwaiter().GetResult();
+ await transport.OpenAsync(MakeTimeoutToken());
}
catch (TTransportException ex)
{
@@ -356,7 +357,7 @@
try
{
- ReturnCode |= ExecuteClientTest(client).GetAwaiter().GetResult(); ;
+ ReturnCode |= await ExecuteClientTest(client);
}
catch (Exception ex)
{
@@ -393,7 +394,7 @@
Console.WriteLine();
}
- public static int Execute(List<string> args)
+ public static async Task<int> Execute(List<string> args)
{
try
{
@@ -414,15 +415,9 @@
var tests = Enumerable.Range(0, param.numThreads).Select(_ => new ClientTest(param)).ToArray();
//issue tests on separate threads simultaneously
- var threads = tests.Select(test => new Task(test.Execute)).ToArray();
var start = DateTime.Now;
- foreach (var t in threads)
- {
- t.Start();
- }
-
- Task.WaitAll(threads);
-
+ var tasks = tests.Select(test => test.Execute()).ToArray();
+ Task.WaitAll(tasks);
Console.WriteLine("Total time: " + (DateTime.Now - start));
Console.WriteLine();
return tests.Select(t => t.ReturnCode).Aggregate((r1, r2) => r1 | r2);
@@ -660,37 +655,13 @@
mapout[j] = j - 10;
}
Console.Write("testMap({");
- var first = true;
- foreach (var key in mapout.Keys)
- {
- if (first)
- {
- first = false;
- }
- else
- {
- Console.Write(", ");
- }
- Console.Write(key + " => " + mapout[key]);
- }
+ Console.Write(string.Join(", ", mapout.Select((pair) => { return pair.Key + " => " + pair.Value; })));
Console.Write("})");
var mapin = await client.testMap(mapout, MakeTimeoutToken());
Console.Write(" = {");
- first = true;
- foreach (var key in mapin.Keys)
- {
- if (first)
- {
- first = false;
- }
- else
- {
- Console.Write(", ");
- }
- Console.Write(key + " => " + mapin[key]);
- }
+ Console.Write(string.Join(", ", mapin.Select((pair) => { return pair.Key + " => " + pair.Value; })));
Console.WriteLine("}");
// TODO: Validate received message
@@ -700,37 +671,13 @@
listout.Add(j);
}
Console.Write("testList({");
- first = true;
- foreach (var j in listout)
- {
- if (first)
- {
- first = false;
- }
- else
- {
- Console.Write(", ");
- }
- Console.Write(j);
- }
+ Console.Write(string.Join(", ", listout));
Console.Write("})");
var listin = await client.testList(listout, MakeTimeoutToken());
Console.Write(" = {");
- first = true;
- foreach (var j in listin)
- {
- if (first)
- {
- first = false;
- }
- else
- {
- Console.Write(", ");
- }
- Console.Write(j);
- }
+ Console.Write(string.Join(", ", listin));
Console.WriteLine("}");
//set
@@ -741,37 +688,13 @@
setout.Add(j);
}
Console.Write("testSet({");
- first = true;
- foreach (int j in setout)
- {
- if (first)
- {
- first = false;
- }
- else
- {
- Console.Write(", ");
- }
- Console.Write(j);
- }
+ Console.Write(string.Join(", ", setout));
Console.Write("})");
var setin = await client.testSet(setout, MakeTimeoutToken());
Console.Write(" = {");
- first = true;
- foreach (int j in setin)
- {
- if (first)
- {
- first = false;
- }
- else
- {
- Console.Write(", ");
- }
- Console.Write(j);
- }
+ Console.Write(string.Join(", ", setin));
Console.WriteLine("}");
diff --git a/test/netstd/Server/Program.cs b/test/netstd/Server/Program.cs
index 8f1f36d..a319b22 100644
--- a/test/netstd/Server/Program.cs
+++ b/test/netstd/Server/Program.cs
@@ -20,12 +20,13 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
using ThriftTest;
+using System.Threading.Tasks;
namespace Server
{
public class Program
{
- public static int Main(string[] args)
+ static async Task<int> Main(string[] args)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
@@ -46,12 +47,12 @@
case "server": // crosstest wants to pass this, so just emit a hint and ignore
Console.WriteLine("Hint: The 'server' argument is no longer required.");
argslist.RemoveAt(0);
- return TestServer.Execute(argslist);
+ return await TestServer.Execute(argslist);
case "--help":
PrintHelp();
return 0;
default:
- return TestServer.Execute(argslist);
+ return await TestServer.Execute(argslist);
}
}
diff --git a/test/netstd/Server/TestServer.cs b/test/netstd/Server/TestServer.cs
index 471d6c8..65458d6 100644
--- a/test/netstd/Server/TestServer.cs
+++ b/test/netstd/Server/TestServer.cs
@@ -500,13 +500,11 @@
return Task.FromResult(result);
}
- public Task testOneway(int secondsToSleep, CancellationToken cancellationToken)
+ public async Task testOneway(int secondsToSleep, CancellationToken cancellationToken)
{
logger.Invoke("testOneway({0}), sleeping...", secondsToSleep);
- Task.Delay(secondsToSleep * 1000, cancellationToken).GetAwaiter().GetResult();
+ await Task.Delay(secondsToSleep * 1000, cancellationToken);
logger.Invoke("testOneway finished");
-
- return Task.CompletedTask;
}
}
@@ -543,7 +541,7 @@
return cert;
}
- public static int Execute(List<string> args)
+ public static async Task<int> Execute(List<string> args)
{
using (var loggerFactory = new LoggerFactory()) //.AddConsole().AddDebug();
{
@@ -648,7 +646,7 @@
(param.protocol == ProtocolChoice.Compact ? " with compact protocol" : "") +
(param.protocol == ProtocolChoice.Json ? " with json protocol" : "") +
"...");
- serverEngine.ServeAsync(CancellationToken.None).GetAwaiter().GetResult();
+ await serverEngine.ServeAsync(CancellationToken.None);
Console.ReadLine();
}
catch (Exception x)