GWT RPC synchronic calls

283 views
Skip to first unread message

Sebastián Gurin

unread,
Oct 3, 2012, 1:49:22 PM10/3/12
to google-we...@googlegroups.com
Hi all. I really want to use RPC synchronic calls for creating simple to read automated tests against my model classes services. *** I don't want to use RPC calls synchronically on production code, only on my automated tests. ****

In my particular case, I think it is a common case, I have some RPC services that perform CRUD operations on some model class. (behind there is appengine datastore and objectify, but that isn't part of the problem).

I want to write test units that,  for example, add some entities through RPC, delete, search and update the entities and make some asserts for making sure it works  (like a DAO automated test in common j2ee applications).

The problem is that because of the asynchronic nature of GWT RPC it is very hard to write such tests cases. For example, writing a simple use case test like "1) save a new entity, 2) get the entity and check it was saved/loaded ok 3) update the entity and assert it was updated ok, 4) delete the entity and assert it was deleted ok" take five or more anidated rpc callbacks.  and more complicated uses cases code are almost impossible to write/ read.

I have created a simple "Synchronic queue" so I can execute each stage of the test more asynchronically and thus more readable. But still kind of complicated, see http://code.google.com/p/yuigwt/source/browse/madrenecesidad/src/org/sgx/madrenecesidad/client/test/tests/ChannelServiceTest1.java#41

In this case, synchronic RPC would be ideal:

service.addMyEntity(new MyEntity(...));
assertTrue(service.listMyEntities().size()==1);
MyEntity e = service.getEntityByName("name");
assertTrue(e.getName.equals("...));
service.deleteMyEntity(e);
assertTrue(service.listMyEntities().size()==0);

etc.

There is a project called http://code.google.com/p/gwt-syncproxy/ that claims to support synch rpc calls just like I want, but it do not seems to be supported anymore and the .jar do not contain any GWT module

My question to you all is, is there a way of facilitating the writing of automated testcases for testing RPC services ?  How do you do that ? I bet GWT brings something for this case after all automated testing on RPC services should be a common tasks for GWTers.... but I can't figure out.

Thanks in advance, any suggestion is most welcome.

Jens

unread,
Oct 3, 2012, 2:26:57 PM10/3/12
to google-we...@googlegroups.com
I think you dont need a GWT module for gwt-syncproxy. It should work in a pure JUnit test as it does not use GWT.create() for creating the GWT service. gwt-syncproxy uses Java's dynamic proxy to implement the service interface and uses Java's HttpUrlConnection to call your server.

-- J.

Sebastián Gurin

unread,
Oct 3, 2012, 3:01:41 PM10/3/12
to google-we...@googlegroups.com
Jens, thank you but as I suspected I have troubles. What do you mean with "a pure JUnit test" ? you mean a non gwt program ? A pure junit test launched from a "normal java runtime", not in GWT ? mmmm then this is not for me. In my case I want to launch the tests from my GWT/GAE application itself , not from a "pure junit test"

I thought syncproxy was a library for the gwt client side, so it is strange to me that dodn't come with a gwt module definition. The docs says (http://code.google.com/p/gwt-syncproxy/) I must instantiate the service class on the client side like this:

private static GreetingService rpcService =
 
SyncProxy.newProxyInstance(GreetingService.class,
       
"http://example.com/helloApp", "greet");

But that introduces the GWT compilation error:

No source code is available for type com.gdevelop.gwt.syncrpc.SyncProxy; did you forget to inherit a required module?

Espected error.. I'm trying now to put the library sources in my own code. Hope that let me use it. Any suggestions are appreciated, in the meanwhile will try to contact the authors. Thanks

Jens

unread,
Oct 3, 2012, 5:26:43 PM10/3/12
to google-we...@googlegroups.com

Jens, thank you but as I suspected I have troubles. What do you mean with "a pure JUnit test" ? you mean a non gwt program ? A pure junit test launched from a "normal java runtime", not in GWT ?

Yes. See the provided tests of gwt-syncproxy (e.g.: http://code.google.com/p/gwt-syncproxy/source/browse/trunk/test/com/gdevelop/gwt/syncrpc/test/EnumsTest.java). These are normal JUnit Tests. 

 
I thought syncproxy was a library for the gwt client side, so it is strange to me that dodn't come with a gwt module definition. The docs says (http://code.google.com/p/gwt-syncproxy/) I must instantiate the service class on the client side like this: 

private static GreetingService rpcService =
 
SyncProxy.newProxyInstance(GreetingService.class,
       
"http://example.com/helloApp", "greet");

When the docs say "Java Client code" it means the client side code of the server service. It does not mean GWT client code. SyncProxy.newProxyInstance() will never work in GWT client code.

-- J.

Sebastián Gurin

unread,
Oct 3, 2012, 5:39:54 PM10/3/12
to google-we...@googlegroups.com
Aja Jens I understand now thanks.  Tests are normal java programs that perform RPC targetting your  running gwt application somewhere else.Trying to get it work that way. Thanks again.

Brian Slesinsky

unread,
Oct 4, 2012, 9:46:59 PM10/4/12
to google-we...@googlegroups.com
Yes, running in the JVM is better. I've tried pretty hard to make this work well and as a result, I don't recommend writing integration tests in GWT (or JavaScript). Integration tests are naturally represented as a sequence of actions that perform I/O, and this works better on a platform that supports blocking I/O natively. There are design patterns to make a chain of async callbacks look more sequential, but the resulting code still ends up looking pretty unnatural and hard to debug.

- Brian

Sebastián Gurin

unread,
Oct 5, 2012, 4:42:40 PM10/5/12
to google-we...@googlegroups.com
Brian: well said. I'm on that road now. As a commend: I tried block the client with some dirty sleep() function like the following , and in devmode, as I suspected, It didn't worked, because, the devmode itlself was being blocked too !!!!

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

Sebastián Gurin

unread,
Oct 15, 2012, 7:01:12 PM10/15/12
to google-we...@googlegroups.com
just published some notes about how getting started with syncproxy and eclipse + google plugin for testing GWT services synchronously: http://cancerberonia.blogspot.com/2012/10/testing-gwt-service-classes.html

Reply all
Reply to author
Forward
0 new messages