01: package com.xoetrope.service.test;
02:
03: import java.util.Date;
04:
05: import net.xoetrope.optional.service.XRouteManager;
06: import net.xoetrope.optional.service.XServiceModelNode;
07: import com.xoetrope.service.CallbackToken;
08: import com.xoetrope.service.XPersistantService;
09: import com.xoetrope.service.XServiceCallback;
10: import com.xoetrope.service.servlet.XUnreliableServiceServlet;
11: import net.xoetrope.xml.XmlElement;
12: import net.xoetrope.xui.data.XModel;
13: import net.xoetrope.xui.XProjectManager;
14:
15: /**
16: * Test the unreliable service servelt to see that it returns the expected errors
17: * <p>Copyright (c) Xoetrope Ltd. 2001-2004</p>
18: * $Revision: 1.2 $
19: */
20: public class XPersistantProxyTest extends XServiceTest implements
21: XServiceCallback {
22: int callbackToken = -1;
23:
24: public XPersistantProxyTest() {
25: super ();
26: defaultURLStr = "http://localhost:8080/xunreliableservlet";
27: }
28:
29: public void testServer() {
30: String[] argNames = { "reliability" };
31:
32: XRouteManager routeMgr = (XRouteManager) XProjectManager
33: .getCurrentProject().getObject("Routes");
34: XServiceModelNode node = new XServiceModelNode();
35: XModel.getInstance().set("TestPersistenProxyService", node);
36: try {
37: node.setupService("getName64", routeMgr.getRoute(
38: "persistRoute",
39: "com.xoetrope.service.test.CalcTestService"),
40: argNames);
41: XPersistantService sp = (XPersistantService) node
42: .getServiceProxy();
43: sp.setServiceName("TestPersistenProxyService");
44: long sleepTime = sleepTime = new Long(XProjectManager
45: .getCurrentProject().getStartupParam(
46: "CacheSleepTime")).longValue();
47: long endTime = new Date().getTime() + 5 * sleepTime
48: + sp.getNumStoredCalls() * sleepTime;
49: node.setNumAttributes(1);
50: node.setAttribValue(0, Integer.toString(
51: XUnreliableServiceServlet.FOURTH_RESPONSE)
52: .toString());
53: sp.setCallbackObject(this );
54: Object result = node.get();
55: assertTrue(result != null);
56: // If the request just happened to succeed then the next request should
57: // fail as it can't be the fourth request
58: if (!(result instanceof CallbackToken))
59: result = node.get();
60: assertTrue(result instanceof CallbackToken);
61:
62: callbackToken = ((CallbackToken) result).getToken();
63: assertTrue(callbackToken >= 0);
64: while ((callbackToken >= 0) || (sp.getNumStoredCalls() > 0)) {
65: Thread.currentThread().sleep(100);
66: if (new Date().getTime() > endTime)
67: break;
68: }
69: assertTrue(callbackToken == -1);
70: } catch (Exception e) {
71: e.printStackTrace();
72: assertTrue(false);
73: }
74: }
75:
76: public void callCompleted(int token, Object response,
77: XmlElement request) {
78: System.out.println("job token completed:" + token);
79: if (token == callbackToken)
80: callbackToken = -1;
81: }
82: }
|