THRIFT-1087 Nonblocking asynchronous JS services
Patch: Henrique Mendonca
git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1089637 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/js/test/build.xml b/lib/js/test/build.xml
index c1a17e8..843ec6b 100644
--- a/lib/js/test/build.xml
+++ b/lib/js/test/build.xml
@@ -66,7 +66,7 @@
</not>
</condition>
You need libthrift*.jar and libthrift*test.jar located at
- ${thrift.java.dir}
+ ${thrift.java.dir}/build
Did you compile Thrift Java library and its test suite by "ant compile-test"?
</fail>
<fail>
@@ -101,7 +101,7 @@
<javac srcdir="${src}" destdir="${build}" classpathref="libs.classpath" />
</target>
- <target name="jstest" description="" depends="compile">
+ <target name="jstest" description="" depends="compile, lint">
<jar jarfile="${jar.file}" basedir="${build}"/>
</target>
@@ -117,14 +117,14 @@
<arg line="--gen java ${thrift.dir}/test/ThriftTest.thrift" />
</exec>
<exec executable="${thrift.compiler}" failonerror="true">
- <arg line="--gen js ${thrift.dir}/test/ThriftTest.thrift" />
+ <arg line="--gen js:jquery ${thrift.dir}/test/ThriftTest.thrift" />
</exec>
</target>
<!-- @TODO QUnit tests as part of the testsuite-->
<target name="test" description="run test suite" depends="init, generate, resolve, lint"/>
- <target name="lint" description="code quality checks" depends="gjslint, jslint, generate"/>
+ <target name="lint" description="code quality checks" depends="generate, gjslint, jslint"/>
<target name="jslint">
<taskdef uri="antlib:com.googlecode.jslint4java" resource="com/googlecode/jslint4java/antlib.xml" classpathref="libs.classpath" />
diff --git a/lib/js/test/test.html b/lib/js/test/test.html
index 4615f88..f99da01 100644
--- a/lib/js/test/test.html
+++ b/lib/js/test/test.html
@@ -27,7 +27,7 @@
<script src="gen-js/ThriftTest.js" type="text/javascript" charset="utf-8"></script>
<!-- jQuery -->
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" charset="utf-8"></script>
+ <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.2.js" charset="utf-8"></script>
<!-- json2 -->
<script type="text/javascript" src="json2.js" charset="utf-8"></script>
@@ -39,9 +39,9 @@
<script type="text/javascript" charset="utf-8">
//<![CDATA[
$(document).ready(function(){
- var transport = new Thrift.Transport("/service")
- var protocol = new Thrift.Protocol(transport)
- var client = new ThriftTest.ThriftTestClient(protocol)
+ var transport = new Thrift.Transport("/service");
+ var protocol = new Thrift.Protocol(transport);
+ var client = new ThriftTest.ThriftTestClient(protocol);
module("Base Types");
@@ -69,11 +69,6 @@
});
test("I32", function() {
equals(client.testI32(Math.pow(2,30)), Math.pow(2,30));
-
- /*
- how to test things like that?
- equals(client.testI32(Math.pow(2,60)), Math.pow(2,60));
- */
});
test("I64", function() {
equals(client.testI64(Math.pow(2,60)), Math.pow(2,60));
@@ -174,9 +169,6 @@
equals(JSON.stringify(mapMapTestOutput), JSON.stringify(mapMapTestExpectedResult))
});
- test("testMulti", function() {
- });
-
module("Exception");
@@ -204,7 +196,7 @@
client.testException("ApplicationException");
} catch(e) {
ok(true); //@HACK: ignore faulty java server response for exceptions
- //equals(e.message, "ApplicationException");
+ //equals(e.message, "ApplicationException");
}
});
@@ -217,63 +209,157 @@
ok(res);
});
+
+ //////////////////////////////////
+ //Run same tests asynchronously
+ jQuery.ajaxSetup({ timeout: 0 });
+ $(document).ajaxError( function() { QUnit.start(); } );
+
+ module("Async Manual");
+
+ test("testI32", function() {
+ expect( 2 );
+ QUnit.stop();
+
+ var transport = new Thrift.Transport();
+ var protocol = new Thrift.Protocol(transport);
+ var client = new ThriftTest.ThriftTestClient(protocol);
+
+ var jqxhr = jQuery.ajax({
+ url: "/service",
+ data: client.send_testI32(Math.pow(-2,31)),
+ type: "POST",
+ cache: false,
+ dataType: "text",
+ success: function(res){
+ transport.setRecvBuffer( res );
+ equals(client.recv_testI32(), Math.pow(-2,31));
+ },
+ error: function() { ok(false); },
+ complete: function() {
+ ok(true);
+ QUnit.start();
+ }
+ });
+ });
+
+
+ test("testI64", function() {
+ expect( 2 );
+ QUnit.stop();
+
+ var transport = new Thrift.Transport();
+ var protocol = new Thrift.Protocol(transport);
+ var client = new ThriftTest.ThriftTestClient(protocol);
+
+ jQuery.ajax({
+ url: "/service",
+ data: client.send_testI64(Math.pow(-2,61)),
+ type: "POST",
+ cache: false,
+ dataType: "text",
+ success: function(res){
+ transport.setRecvBuffer( res );
+ equals(client.recv_testI64(), Math.pow(-2,61));
+ },
+ error: function() { ok(false); },
+ complete: function() {
+ ok(true);
+ QUnit.start();
+ }
+ });
+ });
+
+
+
+ module("Async");
+
+ test("Double", function() {
+ expect( 1 );
+
+ QUnit.stop();
+ client.testDouble(3.14159265, function(result) {
+ equals(result, 3.14159265);
+ QUnit.start();
+ });
+ });
+
+ test("Byte", function() {
+ expect( 1 );
+
+ QUnit.stop();
+ client.testByte(0x01, function(result) {
+ equals(result, 0x01);
+ QUnit.start();
+ });
+ });
+
+ test("I32", function() {
+ expect( 3 );
+
+ QUnit.stop();
+ client.testI32(Math.pow(2,30), function(result) {
+ equals(result, Math.pow(2,30));
+ QUnit.start();
+ });
+
+ QUnit.stop();
+ var jqxhr = client.testI32(Math.pow(-2,31), function(result) {
+ equals(result, Math.pow(-2,31));
+ });
+
+ jqxhr.success(function(result) {
+ equals(result, Math.pow(-2,31));
+ QUnit.start();
+ });
+ });
+
+ test("I64", function() {
+ expect( 4 );
+
+ QUnit.stop();
+ client.testI64(Math.pow(2,60), function(result) {
+ equals(result, Math.pow(2,60));
+ QUnit.start();
+ });
+
+ QUnit.stop();
+ client.testI64(Math.pow(-2,61), function(result) {
+ equals(result, Math.pow(-2,61));
+ })
+ .error( function(e) { ok(false); } )
+ .success(function(result) {
+ equals(result, Math.pow(-2,61));
+ })
+ .complete(function() {
+ ok(true);
+ QUnit.start();
+ });
+ });
+
+ test("Xception", function() {
+ expect( 2 );
+
+ QUnit.stop();
+
+ var dfd = client.testException("Xception", function(result) {
+ ok(false);
+ QUnit.start();
+ })
+ .error(function(e){
+ equals(e.errorCode, 1001);
+ equals(e.message, "Xception");
+ QUnit.start();
+ });
+ });
+
+
});
//]]>
</script>
</head>
<body>
-
- <script type="text/javascript" charset="utf-8">
- //<![CDATA[
-
- //////////////////////////////////
- //Run same tests asynchronously
-/*
- var transport = new Thrift.Transport()
- var protocol = new Thrift.Protocol(transport)
- var client = new ThriftTest.ThriftTestClient(protocol)
-
- document.write("<h2>Asynchronous Example<\/h2>")
- jQuery.ajax({
- url: "/service",
- data: client.send_testI32(Math.pow(2,30)),
- type: "POST",
- cache: false,
- success: function(res){
- var _transport = new Thrift.Transport()
- var _protocol = new Thrift.Protocol(_transport)
- var _client = new ThriftTest.ThriftTestClient(_protocol)
-
- _transport.setRecvBuffer( res )
-
- var v = _client.recv_testI32()
- $("#body").append("client.testI32() => "+(v == Math.pow(2,30))+"<br/>")
-
- }
- })
-
- jQuery.ajax({
- url: "/service",
- data: client.send_testI64(Math.pow(2,60)),
- type: "POST",
- cache: false,
- success: function(res){
- var _transport = new Thrift.Transport()
- var _protocol = new Thrift.Protocol(_transport)
- var _client = new ThriftTest.ThriftTestClient(_protocol)
-
- _transport.setRecvBuffer( res )
-
- var v = _client.recv_testI64()
- $("#body").append("client.testI64() => "+(v == Math.pow(2,60))+"<br/>")
-
- }
- })
-*/
-
- //]]>
- </script>
<h1 id="qunit-header">Thrift Javascript Bindings: Unit Test (<a href="https://svn.apache.org/repos/asf/thrift/trunk/test/ThriftTest.thrift">ThriftTest.thrift</a>)</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
diff --git a/lib/js/thrift.js b/lib/js/thrift.js
index b8ca2c0..c7a46fd 100644
--- a/lib/js/thrift.js
+++ b/lib/js/thrift.js
@@ -56,7 +56,7 @@
var length = 0;
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
- length++;
+ length++;
}
}
@@ -75,8 +75,8 @@
Thrift.TException = {};
Thrift.TException.prototype = {
initialize: function(message, code) {
- this.message = message;
- this.code = (code === null) ? 0 : code;
+ this.message = message;
+ this.code = (code === null) ? 0 : code;
}
};
@@ -186,19 +186,16 @@
//Gets the browser specific XmlHttpRequest Object
getXmlHttpRequestObject: function() {
-
try { return new XMLHttpRequest(); } catch (e1) { }
try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch (e2) { }
try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (e3) { }
throw "Your browser doesn't support the XmlHttpRequest object.";
-
},
- flush: function() {
-
+ flush: function(async) {
//async mode
- if (this.url === undefined || this.url === '') {
+ if (async || this.url === undefined || this.url === '') {
return this.send_buf;
}
@@ -225,6 +222,54 @@
this.rpos = 0;
},
+ jqRequest: function(client, postData, args, recv_method) {
+ if (typeof jQuery === 'undefined' ||
+ typeof jQuery.Deferred === 'undefined') {
+ throw 'Thrift.js requires jQuery 1.5+ to use asynchronous requests';
+ }
+
+ // Deferreds
+ var deferred = jQuery.Deferred();
+ var completeDfd = jQuery._Deferred();
+ var dfd = deferred.promise();
+ dfd.success = dfd.done;
+ dfd.error = dfd.fail;
+ dfd.complete = completeDfd.done;
+
+ var jqXHR = jQuery.ajax({
+ url: this.url,
+ data: postData,
+ type: 'POST',
+ cache: false,
+ dataType: 'text',
+ context: this,
+ success: this.jqResponse,
+ error: function(xhr, status, e) {
+ deferred.rejectWith(client, jQuery.merge([e], xhr.tArgs));
+ },
+ complete: function(xhr, status) {
+ completeDfd.resolveWith(client, [xhr, status]);
+ }
+ });
+
+ deferred.done(jQuery.makeArray(args).pop()); //pop callback from args
+ jqXHR.tArgs = args;
+ jqXHR.tClient = client;
+ jqXHR.tRecvFn = recv_method;
+ jqXHR.tDfd = deferred;
+ return dfd;
+ },
+
+ jqResponse: function(responseData, textStatus, jqXHR) {
+ this.setRecvBuffer(responseData);
+ try {
+ var value = jqXHR.tRecvFn.call(jqXHR.tClient);
+ jqXHR.tDfd.resolveWith(jqXHR, jQuery.merge([value], jqXHR.tArgs));
+ } catch (ex) {
+ jqXHR.tDfd.rejectWith(jqXHR, jQuery.merge([ex], jqXHR.tArgs));
+ }
+ },
+
setRecvBuffer: function(buf) {
this.recv_buf = buf;
this.recv_buf_sz = this.recv_buf.length;
@@ -531,7 +576,11 @@
this.rstack = [];
this.rpos = [];
- this.robj = eval(this.transport.readAll());
+ if (typeof jQuery !== 'undefined') {
+ this.robj = jQuery.parseJSON(this.transport.readAll());
+ } else {
+ this.robj = eval(this.transport.readAll());
+ }
var r = {};
var version = this.robj.shift();
@@ -551,7 +600,6 @@
return r;
},
-
readMessageEnd: function() {
},
@@ -617,7 +665,6 @@
r.ftype = ftype;
r.fid = fid;
-
return r;
},
@@ -632,7 +679,6 @@
},
readMapBegin: function(keyType, valType, size) {
-
var map = this.rstack.pop();
var r = {};
@@ -652,14 +698,12 @@
},
readListBegin: function(elemType, size) {
-
var list = this.rstack[this.rstack.length - 1];
var r = {};
r.etype = Thrift.Protocol.RType[list.shift()];
r.size = list.shift();
-
this.rpos.push(this.rstack.length);
this.rstack.push(list);
@@ -698,7 +742,6 @@
return this.readI32();
},
-
readI32: function(f) {
if (f === undefined) {
f = this.rstack[this.rstack.length - 1];
@@ -753,5 +796,4 @@
skip: function(type) {
throw 'skip not supported yet';
}
-
};