From: David Reiss Date: Mon, 18 Feb 2008 02:11:44 +0000 (+0000) Subject: Add testAsync to ThriftTest.thrift which verifies async void works properly. X-Git-Tag: 0.2.0~988 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=2ab6fe88e26b5570bcdb357fdf6c1ee84b37bd44;p=common%2Fthrift.git Add testAsync to ThriftTest.thrift which verifies async void works properly. Summary: - testAsync takes a number of seconds to sleep. The test client makes sure that the RPC returns in less than 0.2 seconds even though it asks the server to sleep for 3 seconds. - Implemented this test for C++ and Java. Test Plan: - ran cpp TestServer and TestClient and verified functionality - tested cpp and java test server/client against each other - tests passed git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665484 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/test/ThriftTest.thrift b/test/ThriftTest.thrift index 90564657..a549fa71 100644 --- a/test/ThriftTest.thrift +++ b/test/ThriftTest.thrift @@ -90,6 +90,9 @@ service ThriftTest /* Multiple exceptions specifier */ Xtruct testMultiException(string arg0, string arg1) throws(Xception err1, Xception2 err2) + + /* Test async void */ + async void testAsync(1:i32 secondsToSleep) } service SecondService diff --git a/test/cpp/src/TestClient.cpp b/test/cpp/src/TestClient.cpp index 9b350b0b..2b8ccedd 100644 --- a/test/cpp/src/TestClient.cpp +++ b/test/cpp/src/TestClient.cpp @@ -430,6 +430,19 @@ int main(int argc, char** argv) { printf(" exception\nFAILURE\n"); } + /* test async void */ + { + printf("testClient.testAsync(3) =>"); + uint64_t startAsync = now(); + testClient.testAsync(3); + uint64_t elapsed = now() - startAsync; + if (elapsed > 200 * 1000) { // 0.2 seconds + printf(" FAILURE - took %.2f ms\n", (double)elapsed/1000.0); + } else { + printf(" success - took %.2f ms\n", (double)elapsed/1000.0); + } + } + uint64_t stop = now(); uint64_t tot = stop-start; diff --git a/test/cpp/src/TestServer.cpp b/test/cpp/src/TestServer.cpp index 773225e1..14a8a8ef 100644 --- a/test/cpp/src/TestServer.cpp +++ b/test/cpp/src/TestServer.cpp @@ -255,6 +255,12 @@ class TestHandler : public ThriftTestIf { return; } } + + void testAsync(int sleepFor) { + printf("testAsync(%d): Sleeping...\n", sleepFor); + sleep(sleepFor); + printf("testAsync(%d): done sleeping!\n", sleepFor); + } }; int main(int argc, char **argv) { diff --git a/test/java/src/TestClient.java b/test/java/src/TestClient.java index 85aac9ec..4e0482a6 100644 --- a/test/java/src/TestClient.java +++ b/test/java/src/TestClient.java @@ -357,6 +357,22 @@ public class TestClient { } System.out.print("}\n"); + // Test async + System.out.print("testAsync(3)..."); + long startAsync = System.nanoTime(); + testClient.testAsync(3); + long asyncElapsedMillis = (System.nanoTime() - startAsync) / 1000000; + if (asyncElapsedMillis > 200) { + throw new Exception("Async test failed: took " + + Long.toString(asyncElapsedMillis) + + "ms"); + } else { + System.out.println("Success - took " + + Long.toString(asyncElapsedMillis) + + "ms"); + } + + long stop = System.nanoTime(); long tot = stop-start; diff --git a/test/java/src/TestServer.java b/test/java/src/TestServer.java index 21003b10..92fd4d27 100644 --- a/test/java/src/TestServer.java +++ b/test/java/src/TestServer.java @@ -234,6 +234,17 @@ public class TestServer { return result; } + public void testAsync(int sleepFor) { + System.out.println("testAsync(" + Integer.toString(sleepFor) + + ") => sleeping..."); + try { + Thread.sleep(sleepFor * 1000); + System.out.println("Done sleeping!"); + } catch (InterruptedException ie) { + throw new RuntimeException(ie); + } + } + } // class TestHandler public static void main(String [] args) {