THRIFT-4818 Test client should use cancellation token
Client: netstd
Patch: Jens Geyer
This closes #1756
diff --git a/test/netstd/Client/TestClient.cs b/test/netstd/Client/TestClient.cs
index a85312b..c6b086f 100644
--- a/test/netstd/Client/TestClient.cs
+++ b/test/netstd/Client/TestClient.cs
@@ -314,8 +314,6 @@
public void Execute()
{
- var token = CancellationToken.None;
-
if (done)
{
Console.WriteLine("Execute called more than once");
@@ -327,9 +325,7 @@
try
{
if (!transport.IsOpen)
- {
- transport.OpenAsync(token).GetAwaiter().GetResult();
- }
+ transport.OpenAsync(MakeTimeoutToken()).GetAwaiter().GetResult();
}
catch (TTransportException ex)
{
@@ -471,17 +467,22 @@
return retval;
}
+ private static CancellationToken MakeTimeoutToken(int msec = 5000)
+ {
+ var token = new CancellationTokenSource(msec);
+ return token.Token;
+ }
+
public static async Task<int> ExecuteClientTestAsync(ThriftTest.Client client)
{
- var token = CancellationToken.None;
var returnCode = 0;
Console.Write("testVoid()");
- await client.testVoidAsync(token);
+ await client.testVoidAsync(MakeTimeoutToken());
Console.WriteLine(" = void");
Console.Write("testString(\"Test\")");
- var s = await client.testStringAsync("Test", token);
+ var s = await client.testStringAsync("Test", MakeTimeoutToken());
Console.WriteLine(" = \"" + s + "\"");
if ("Test" != s)
{
@@ -490,7 +491,7 @@
}
Console.Write("testBool(true)");
- var t = await client.testBoolAsync((bool)true, token);
+ var t = await client.testBoolAsync((bool)true, MakeTimeoutToken());
Console.WriteLine(" = " + t);
if (!t)
{
@@ -498,7 +499,7 @@
returnCode |= ErrorBaseTypes;
}
Console.Write("testBool(false)");
- var f = await client.testBoolAsync((bool)false, token);
+ var f = await client.testBoolAsync((bool)false, MakeTimeoutToken());
Console.WriteLine(" = " + f);
if (f)
{
@@ -507,7 +508,7 @@
}
Console.Write("testByte(1)");
- var i8 = await client.testByteAsync((sbyte)1, token);
+ var i8 = await client.testByteAsync((sbyte)1, MakeTimeoutToken());
Console.WriteLine(" = " + i8);
if (1 != i8)
{
@@ -516,7 +517,7 @@
}
Console.Write("testI32(-1)");
- var i32 = await client.testI32Async(-1, token);
+ var i32 = await client.testI32Async(-1, MakeTimeoutToken());
Console.WriteLine(" = " + i32);
if (-1 != i32)
{
@@ -525,7 +526,7 @@
}
Console.Write("testI64(-34359738368)");
- var i64 = await client.testI64Async(-34359738368, token);
+ var i64 = await client.testI64Async(-34359738368, MakeTimeoutToken());
Console.WriteLine(" = " + i64);
if (-34359738368 != i64)
{
@@ -535,7 +536,7 @@
// TODO: Validate received message
Console.Write("testDouble(5.325098235)");
- var dub = await client.testDoubleAsync(5.325098235, token);
+ var dub = await client.testDoubleAsync(5.325098235, MakeTimeoutToken());
Console.WriteLine(" = " + dub);
if (5.325098235 != dub)
{
@@ -543,7 +544,7 @@
returnCode |= ErrorBaseTypes;
}
Console.Write("testDouble(-0.000341012439638598279)");
- dub = await client.testDoubleAsync(-0.000341012439638598279, token);
+ dub = await client.testDoubleAsync(-0.000341012439638598279, MakeTimeoutToken());
Console.WriteLine(" = " + dub);
if (-0.000341012439638598279 != dub)
{
@@ -555,7 +556,7 @@
Console.Write("testBinary(" + BytesToHex(binOut) + ")");
try
{
- var binIn = await client.testBinaryAsync(binOut, token);
+ var binIn = await client.testBinaryAsync(binOut, MakeTimeoutToken());
Console.WriteLine(" = " + BytesToHex(binIn));
if (binIn.Length != binOut.Length)
{
@@ -601,7 +602,7 @@
o.Byte_thing = (sbyte)1;
o.I32_thing = -3;
o.I64_thing = -5;
- var i = await client.testStructAsync(o, token);
+ var i = await client.testStructAsync(o, MakeTimeoutToken());
Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
// TODO: Validate received message
@@ -610,7 +611,7 @@
o2.Byte_thing = (sbyte)1;
o2.Struct_thing = o;
o2.I32_thing = 5;
- var i2 = await client.testNestAsync(o2, token);
+ var i2 = await client.testNestAsync(o2, MakeTimeoutToken());
i = i2.Struct_thing;
Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}, " + i2.I32_thing + "}");
@@ -635,7 +636,7 @@
}
Console.Write("})");
- var mapin = await client.testMapAsync(mapout, token);
+ var mapin = await client.testMapAsync(mapout, MakeTimeoutToken());
Console.Write(" = {");
first = true;
@@ -675,7 +676,7 @@
}
Console.Write("})");
- var listin = await client.testListAsync(listout, token);
+ var listin = await client.testListAsync(listout, MakeTimeoutToken());
Console.Write(" = {");
first = true;
@@ -716,7 +717,7 @@
}
Console.Write("})");
- var setin = await client.testSetAsync(setout, token);
+ var setin = await client.testSetAsync(setout, MakeTimeoutToken());
Console.Write(" = {");
first = true;
@@ -736,7 +737,7 @@
Console.Write("testEnum(ONE)");
- var ret = await client.testEnumAsync(Numberz.ONE, token);
+ var ret = await client.testEnumAsync(Numberz.ONE, MakeTimeoutToken());
Console.WriteLine(" = " + ret);
if (Numberz.ONE != ret)
{
@@ -745,7 +746,7 @@
}
Console.Write("testEnum(TWO)");
- ret = await client.testEnumAsync(Numberz.TWO, token);
+ ret = await client.testEnumAsync(Numberz.TWO, MakeTimeoutToken());
Console.WriteLine(" = " + ret);
if (Numberz.TWO != ret)
{
@@ -754,7 +755,7 @@
}
Console.Write("testEnum(THREE)");
- ret = await client.testEnumAsync(Numberz.THREE, token);
+ ret = await client.testEnumAsync(Numberz.THREE, MakeTimeoutToken());
Console.WriteLine(" = " + ret);
if (Numberz.THREE != ret)
{
@@ -763,7 +764,7 @@
}
Console.Write("testEnum(FIVE)");
- ret = await client.testEnumAsync(Numberz.FIVE, token);
+ ret = await client.testEnumAsync(Numberz.FIVE, MakeTimeoutToken());
Console.WriteLine(" = " + ret);
if (Numberz.FIVE != ret)
{
@@ -772,7 +773,7 @@
}
Console.Write("testEnum(EIGHT)");
- ret = await client.testEnumAsync(Numberz.EIGHT, token);
+ ret = await client.testEnumAsync(Numberz.EIGHT, MakeTimeoutToken());
Console.WriteLine(" = " + ret);
if (Numberz.EIGHT != ret)
{
@@ -781,7 +782,7 @@
}
Console.Write("testTypedef(309858235082523)");
- var uid = await client.testTypedefAsync(309858235082523L, token);
+ var uid = await client.testTypedefAsync(309858235082523L, MakeTimeoutToken());
Console.WriteLine(" = " + uid);
if (309858235082523L != uid)
{
@@ -791,7 +792,7 @@
// TODO: Validate received message
Console.Write("testMapMap(1)");
- var mm = await client.testMapMapAsync(1, token);
+ var mm = await client.testMapMapAsync(1, MakeTimeoutToken());
Console.Write(" = {");
foreach (var key in mm.Keys)
{
@@ -817,7 +818,7 @@
insane.Xtructs = new List<Xtruct>();
insane.Xtructs.Add(truck);
Console.Write("testInsanity()");
- var whoa = await client.testInsanityAsync(insane, token);
+ var whoa = await client.testInsanityAsync(insane, MakeTimeoutToken());
Console.Write(" = {");
foreach (var key in whoa.Keys)
{
@@ -880,14 +881,14 @@
var arg4 = Numberz.FIVE;
long arg5 = 5000000;
Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + ",{" + string.Join(",", tmpMultiDict) + "}," + arg4 + "," + arg5 + ")");
- var multiResponse = await client.testMultiAsync(arg0, arg1, arg2, multiDict, arg4, arg5, token);
+ var multiResponse = await client.testMultiAsync(arg0, arg1, arg2, multiDict, arg4, arg5, MakeTimeoutToken());
Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
+ ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
try
{
Console.WriteLine("testException(\"Xception\")");
- await client.testExceptionAsync("Xception", token);
+ await client.testExceptionAsync("Xception", MakeTimeoutToken());
Console.WriteLine("*** FAILED ***");
returnCode |= ErrorExceptions;
}
@@ -908,7 +909,7 @@
try
{
Console.WriteLine("testException(\"TException\")");
- await client.testExceptionAsync("TException", token);
+ await client.testExceptionAsync("TException", MakeTimeoutToken());
Console.WriteLine("*** FAILED ***");
returnCode |= ErrorExceptions;
}
@@ -925,7 +926,7 @@
try
{
Console.WriteLine("testException(\"ok\")");
- await client.testExceptionAsync("ok", token);
+ await client.testExceptionAsync("ok", MakeTimeoutToken());
// OK
}
catch (Exception ex)
@@ -938,7 +939,7 @@
try
{
Console.WriteLine("testMultiException(\"Xception\", ...)");
- await client.testMultiExceptionAsync("Xception", "ignore", token);
+ await client.testMultiExceptionAsync("Xception", "ignore", MakeTimeoutToken());
Console.WriteLine("*** FAILED ***");
returnCode |= ErrorExceptions;
}
@@ -959,7 +960,7 @@
try
{
Console.WriteLine("testMultiException(\"Xception2\", ...)");
- await client.testMultiExceptionAsync("Xception2", "ignore", token);
+ await client.testMultiExceptionAsync("Xception2", "ignore", MakeTimeoutToken());
Console.WriteLine("*** FAILED ***");
returnCode |= ErrorExceptions;
}
@@ -980,7 +981,7 @@
try
{
Console.WriteLine("testMultiException(\"success\", \"OK\")");
- if ("OK" != (await client.testMultiExceptionAsync("success", "OK", token)).String_thing)
+ if ("OK" != (await client.testMultiExceptionAsync("success", "OK", MakeTimeoutToken())).String_thing)
{
Console.WriteLine("*** FAILED ***");
returnCode |= ErrorExceptions;
@@ -996,7 +997,7 @@
var sw = new Stopwatch();
sw.Start();
Console.WriteLine("Test Oneway(1)");
- await client.testOnewayAsync(1, token);
+ await client.testOnewayAsync(1, MakeTimeoutToken());
sw.Stop();
if (sw.ElapsedMilliseconds > 1000)
{
@@ -1008,6 +1009,7 @@
var times = 50;
sw.Reset();
sw.Start();
+ var token = MakeTimeoutToken(20000);
for (var k = 0; k < times; ++k)
await client.testVoidAsync(token);
sw.Stop();