THRIFT-1594 Java test clients should have a return codes that reflect whether it succeeds or not.
Patch: Kamil Salas and Roger Meier
diff --git a/lib/java/test/org/apache/thrift/server/ServerTestBase.java b/lib/java/test/org/apache/thrift/server/ServerTestBase.java
old mode 100644
new mode 100755
index 08b57e2..209038b
--- a/lib/java/test/org/apache/thrift/server/ServerTestBase.java
+++ b/lib/java/test/org/apache/thrift/server/ServerTestBase.java
@@ -234,7 +234,7 @@
x.errorCode = 1001;
x.message = arg;
throw x;
- } else if (arg.equals("ApplicationException")) {
+ } else if (arg.equals("TException")) {
throw new TException(arg);
} else {
Xtruct result = new Xtruct();
diff --git a/lib/java/test/org/apache/thrift/test/TestClient.java b/lib/java/test/org/apache/thrift/test/TestClient.java
index 45369ba..a7d5544 100644
--- a/lib/java/test/org/apache/thrift/test/TestClient.java
+++ b/lib/java/test/org/apache/thrift/test/TestClient.java
@@ -48,37 +48,38 @@
*/
public class TestClient {
public static void main(String [] args) {
+ String host = "localhost";
+ int port = 9090;
+ String url = null;
+ int numTests = 1;
+ boolean framed = false;
+
+ int socketTimeout = 1000;
+
try {
- String host = "localhost";
- int port = 9090;
- String url = null;
- int numTests = 1;
- boolean framed = false;
-
- int socketTimeout = 1000;
-
- try {
- for (int i = 0; i < args.length; ++i) {
- if (args[i].equals("-h")) {
- String[] hostport = (args[++i]).split(":");
- host = hostport[0];
- port = Integer.valueOf(hostport[1]);
- } else if (args[i].equals("-f") || args[i].equals("-framed")) {
- framed = true;
- } else if (args[i].equals("-u")) {
- url = args[++i];
- } else if (args[i].equals("-n")) {
- numTests = Integer.valueOf(args[++i]);
- } else if (args[i].equals("-timeout")) {
- socketTimeout = Integer.valueOf(args[++i]);
- }
+ for (int i = 0; i < args.length; ++i) {
+ if (args[i].equals("-h")) {
+ String[] hostport = (args[++i]).split(":");
+ host = hostport[0];
+ port = Integer.valueOf(hostport[1]);
+ } else if (args[i].equals("-f") || args[i].equals("-framed")) {
+ framed = true;
+ } else if (args[i].equals("-u")) {
+ url = args[++i];
+ } else if (args[i].equals("-n")) {
+ numTests = Integer.valueOf(args[++i]);
+ } else if (args[i].equals("-timeout")) {
+ socketTimeout = Integer.valueOf(args[++i]);
}
- } catch (Exception x) {
- x.printStackTrace();
}
+ } catch (Exception x) {
+ x.printStackTrace();
+ System.exit(1);
+ }
- TTransport transport;
+ TTransport transport = null;
+ try {
if (url != null) {
transport = new THttpClient(url);
} else {
@@ -89,28 +90,35 @@
transport = new TFramedTransport(transport);
}
}
+ } catch (Exception x) {
+ x.printStackTrace();
+ System.exit(1);
+ }
- TBinaryProtocol binaryProtocol =
- new TBinaryProtocol(transport);
- ThriftTest.Client testClient =
- new ThriftTest.Client(binaryProtocol);
- Insanity insane = new Insanity();
+ TBinaryProtocol binaryProtocol =
+ new TBinaryProtocol(transport);
+ ThriftTest.Client testClient =
+ new ThriftTest.Client(binaryProtocol);
+ Insanity insane = new Insanity();
- long timeMin = 0;
- long timeMax = 0;
- long timeTot = 0;
+ long timeMin = 0;
+ long timeMax = 0;
+ long timeTot = 0;
- for (int test = 0; test < numTests; ++test) {
-
+ int failCount = 0;
+ for (int test = 0; test < numTests; ++test) {
+ try {
/**
* CONNECT TEST
*/
System.out.println("Test #" + (test+1) + ", " + "connect " + host + ":" + port);
+
try {
transport.open();
} catch (TTransportException ttx) {
+ ttx.printStackTrace();
System.out.println("Connect failed: " + ttx.getMessage());
- continue;
+ System.exit(1);
}
long start = System.nanoTime();
@@ -124,6 +132,7 @@
System.out.print(" = void\n");
} catch (TApplicationException tax) {
tax.printStackTrace();
+ failCount++;
}
/**
@@ -132,6 +141,10 @@
System.out.print("testString(\"Test\")");
String s = testClient.testString("Test");
System.out.print(" = \"" + s + "\"\n");
+ if (!s.equals("Test")) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
/**
* BYTE TEST
@@ -139,6 +152,10 @@
System.out.print("testByte(1)");
byte i8 = testClient.testByte((byte)1);
System.out.print(" = " + i8 + "\n");
+ if (i8 != 1) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
/**
* I32 TEST
@@ -146,6 +163,10 @@
System.out.print("testI32(-1)");
int i32 = testClient.testI32(-1);
System.out.print(" = " + i32 + "\n");
+ if (i32 != -1) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
/**
* I64 TEST
@@ -153,13 +174,21 @@
System.out.print("testI64(-34359738368)");
long i64 = testClient.testI64(-34359738368L);
System.out.print(" = " + i64 + "\n");
+ if (i64 != -34359738368L) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
/**
* DOUBLE TEST
*/
- System.out.print("testDouble(5.325098235)");
- double dub = testClient.testDouble(5.325098235);
+ System.out.print("testDouble(-5.325098235)");
+ double dub = testClient.testDouble(-5.325098235);
System.out.print(" = " + dub + "\n");
+ if (Math.abs(dub - (-5.325098235)) > 0.001) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
/**
* STRUCT TEST
@@ -171,7 +200,15 @@
out.i32_thing = -3;
out.i64_thing = -5;
Xtruct in = testClient.testStruct(out);
- System.out.print(" = {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}\n");
+ System.out.print(" = {" + "\"" +
+ in.string_thing + "\"," +
+ in.byte_thing + ", " +
+ in.i32_thing + ", " +
+ in.i64_thing + "}\n");
+ if (!in.equals(out)) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
/**
* NESTED STRUCT TEST
@@ -183,7 +220,16 @@
out2.i32_thing = 5;
Xtruct2 in2 = testClient.testNest(out2);
in = in2.struct_thing;
- System.out.print(" = {" + in2.byte_thing + ", {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}, " + in2.i32_thing + "}\n");
+ System.out.print(" = {" + in2.byte_thing + ", {" + "\"" +
+ in.string_thing + "\", " +
+ in.byte_thing + ", " +
+ in.i32_thing + ", " +
+ in.i64_thing + "}, " +
+ in2.i32_thing + "}\n");
+ if (!in2.equals(out2)) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
/**
* MAP TEST
@@ -215,6 +261,15 @@
System.out.print(key + " => " + mapout.get(key));
}
System.out.print("}\n");
+ if (!mapout.equals(mapin)) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
+
+ /**
+ * STRING MAP TEST
+ * missing
+ */
/**
* SET TEST
@@ -246,6 +301,10 @@
System.out.print(elem);
}
System.out.print("}\n");
+ if (!setout.equals(setin)) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
/**
* LIST TEST
@@ -277,6 +336,10 @@
System.out.print(elem);
}
System.out.print("}\n");
+ if (!listout.equals(listin)) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
/**
* ENUM TEST
@@ -284,22 +347,42 @@
System.out.print("testEnum(ONE)");
Numberz ret = testClient.testEnum(Numberz.ONE);
System.out.print(" = " + ret + "\n");
+ if (ret != Numberz.ONE) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
System.out.print("testEnum(TWO)");
ret = testClient.testEnum(Numberz.TWO);
System.out.print(" = " + ret + "\n");
+ if (ret != Numberz.TWO) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
System.out.print("testEnum(THREE)");
ret = testClient.testEnum(Numberz.THREE);
System.out.print(" = " + ret + "\n");
+ if (ret != Numberz.THREE) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
System.out.print("testEnum(FIVE)");
ret = testClient.testEnum(Numberz.FIVE);
System.out.print(" = " + ret + "\n");
+ if (ret != Numberz.FIVE) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
System.out.print("testEnum(EIGHT)");
ret = testClient.testEnum(Numberz.EIGHT);
System.out.print(" = " + ret + "\n");
+ if (ret != Numberz.EIGHT) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
/**
* TYPEDEF TEST
@@ -307,6 +390,10 @@
System.out.print("testTypedef(309858235082523)");
long uid = testClient.testTypedef(309858235082523L);
System.out.print(" = " + uid + "\n");
+ if (uid != 309858235082523L) {
+ failCount++;
+ System.out.println("FAILURE\n");
+ }
/**
* NESTED MAP TEST
@@ -381,14 +468,16 @@
System.out.print("testClient.testException(\"Xception\") =>");
testClient.testException("Xception");
System.out.print(" void\nFAILURE\n");
+ failCount++;
} catch(Xception e) {
- System.out.printf(" {%u, \"%s\"}\n", e.errorCode, e.message);
+ System.out.printf(" {%d, \"%s\"}\n", e.errorCode, e.message);
}
try {
System.out.print("testClient.testException(\"TException\") =>");
testClient.testException("TException");
System.out.print(" void\nFAILURE\n");
+ failCount++;
} catch(TException e) {
System.out.printf(" {\"%s\"}\n", e.getMessage());
}
@@ -399,6 +488,7 @@
System.out.print(" void\n");
}catch(Exception e) {
System.out.printf(" exception\nFAILURE\n");
+ failCount++;
}
@@ -410,16 +500,18 @@
System.out.printf("testClient.testMultiException(\"Xception\", \"test 1\") =>");
testClient.testMultiException("Xception", "test 1");
System.out.print(" result\nFAILURE\n");
+ failCount++;
} catch(Xception e) {
- System.out.printf(" {%u, \"%s\"}\n", e.errorCode, e.message);
+ System.out.printf(" {%d, \"%s\"}\n", e.errorCode, e.message);
}
try {
System.out.printf("testClient.testMultiException(\"Xception2\", \"test 2\") =>");
testClient.testMultiException("Xception2", "test 2");
System.out.print(" result\nFAILURE\n");
+ failCount++;
} catch(Xception2 e) {
- System.out.printf(" {%u, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing);
+ System.out.printf(" {%d, {\"%s\"}}\n", e.errorCode, e.struct_thing.string_thing);
}
try {
@@ -429,6 +521,7 @@
System.out.printf(" {{\"%s\"}}\n", result.string_thing);
} catch(Exception e) {
System.out.printf(" exception\nFAILURE\n");
+ failCount++;
}
@@ -441,9 +534,10 @@
testClient.testOneway(3);
long onewayElapsedMillis = (System.nanoTime() - startOneway) / 1000000;
if (onewayElapsedMillis > 200) {
- throw new Exception("Oneway test failed: took " +
+ System.out.println("Oneway test failed: took " +
Long.toString(onewayElapsedMillis) +
"ms");
+ failCount++;
} else {
System.out.println("Success - took " +
Long.toString(onewayElapsedMillis) +
@@ -465,22 +559,27 @@
timeTot += tot;
transport.close();
+ } catch (Exception x) {
+ x.printStackTrace();
+ failCount++;
}
-
- long timeAvg = timeTot / numTests;
-
- System.out.println("Min time: " + timeMin/1000 + "us");
- System.out.println("Max time: " + timeMax/1000 + "us");
- System.out.println("Avg time: " + timeAvg/1000 + "us");
-
- String json = (new TSerializer(new TSimpleJSONProtocol.Factory())).toString(insane);
-
- System.out.println("\nFor good meausre here is some JSON:\n" + json);
-
- } catch (Exception x) {
- x.printStackTrace();
}
- }
+ long timeAvg = timeTot / numTests;
+ System.out.println("Min time: " + timeMin/1000 + "us");
+ System.out.println("Max time: " + timeMax/1000 + "us");
+ System.out.println("Avg time: " + timeAvg/1000 + "us");
+
+ try {
+ String json = (new TSerializer(new TSimpleJSONProtocol.Factory())).toString(insane);
+ System.out.println("\nFor good meausre here is some JSON:\n" + json);
+ } catch (TException x) {
+ x.printStackTrace();
+ System.exit(1);
+ }
+
+
+ System.exit(failCount);
+ }
}
diff --git a/lib/js/test/test.js b/lib/js/test/test.js
index c30287f..99fcc41 100755
--- a/lib/js/test/test.js
+++ b/lib/js/test/test.js
@@ -1,4 +1,4 @@
-/*
+/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@@ -205,13 +205,12 @@
}
});
- test("unexpected Application Exception", function() {
+ test("TException", function() {
expect(1);
try{
- client.testException("ApplicationException");
+ client.testException("TException");
} catch(e) {
- ok(true); //@HACK: ignore faulty java server response for exceptions
- //equal(e.message, "ApplicationException");
+ ok(true);
}
});