diff options
author | Jens Geyer <jensg@apache.org> | 2021-11-13 23:51:16 +0100 |
---|---|---|
committer | Jens Geyer <Jens-G@users.noreply.github.com> | 2021-11-14 12:35:30 +0100 |
commit | 7156940c1da6f7e0c4e8b830cea1e37f770db173 (patch) | |
tree | 11617e4a8b10ffde3fd1459991a4cf1825444d9c | |
parent | da1e19b72bceb1c5a094613c286c1331ff1a6c1b (diff) | |
download | thrift-7156940c1da6f7e0c4e8b830cea1e37f770db173.tar.gz |
Refactoring test server/client to use async/await more consistently
-rw-r--r-- | test/netstd/Client/Performance/PerformanceTests.cs | 6 | ||||
-rw-r--r-- | test/netstd/Client/Program.cs | 9 | ||||
-rw-r--r-- | test/netstd/Client/TestClient.cs | 103 | ||||
-rw-r--r-- | test/netstd/Server/Program.cs | 7 | ||||
-rw-r--r-- | test/netstd/Server/TestServer.cs | 10 |
5 files changed, 30 insertions, 105 deletions
diff --git a/test/netstd/Client/Performance/PerformanceTests.cs b/test/netstd/Client/Performance/PerformanceTests.cs index 2c79aa6ef..f9eb9e4e4 100644 --- a/test/netstd/Client/Performance/PerformanceTests.cs +++ b/test/netstd/Client/Performance/PerformanceTests.cs @@ -28,6 +28,8 @@ using System.Threading.Tasks; 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 @@ namespace Client.Tests 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 bcc02a242..09a9cf2bb 100644 --- a/test/netstd/Client/Program.cs +++ b/test/netstd/Client/Program.cs @@ -19,13 +19,14 @@ using System; 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 @@ namespace Client 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 2d18cf1f9..38bccf1f5 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 @@ namespace ThriftTest numIterations = param.numIterations; } - public void Execute() + public async Task Execute() { if (done) { @@ -335,7 +336,7 @@ namespace ThriftTest try { if (!transport.IsOpen) - transport.OpenAsync(MakeTimeoutToken()).GetAwaiter().GetResult(); + await transport.OpenAsync(MakeTimeoutToken()); } catch (TTransportException ex) { @@ -356,7 +357,7 @@ namespace ThriftTest try { - ReturnCode |= ExecuteClientTest(client).GetAwaiter().GetResult(); ; + ReturnCode |= await ExecuteClientTest(client); } catch (Exception ex) { @@ -393,7 +394,7 @@ namespace ThriftTest Console.WriteLine(); } - public static int Execute(List<string> args) + public static async Task<int> Execute(List<string> args) { try { @@ -414,15 +415,9 @@ namespace ThriftTest 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 @@ namespace ThriftTest 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 @@ namespace ThriftTest 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 @@ namespace ThriftTest 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 8f1f36d77..a319b2206 100644 --- a/test/netstd/Server/Program.cs +++ b/test/netstd/Server/Program.cs @@ -20,12 +20,13 @@ using System.Linq; 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 @@ namespace Server 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 471d6c824..65458d60a 100644 --- a/test/netstd/Server/TestServer.cs +++ b/test/netstd/Server/TestServer.cs @@ -500,13 +500,11 @@ namespace ThriftTest 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 @@ namespace ThriftTest 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 @@ namespace ThriftTest (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) |