Debian Codegraph @

src/test/java/org/astrogrid/samp/client/HubConnectorTest.java

raw
1 package org.astrogrid.samp.client;
2
3 import java.io.IOException;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.Random;
9 import java.util.logging.Level;
10 import java.util.logging.Logger;
11 import junit.framework.TestCase;
12 import org.astrogrid.samp.Client;
13 import org.astrogrid.samp.Message;
14 import org.astrogrid.samp.Metadata;
15 import org.astrogrid.samp.SampUtils;
16 import org.astrogrid.samp.RegInfo;
17 import org.astrogrid.samp.Response;
18 import org.astrogrid.samp.Subscriptions;
19 import org.astrogrid.samp.TestProfile;
20 import org.astrogrid.samp.xmlrpc.internal.InternalServer;
21
22 public class HubConnectorTest extends TestCase {
23
24 private final Random random_ = new Random( 2323L );
25 private static final String ECHO_MTYPE = "test.echo";
26
27 protected void setUp() {
28 Logger.getLogger( "org.astrogrid.samp" ).setLevel( Level.SEVERE );
29 }
30
31 public void testConnector() throws IOException, InterruptedException {
32 TestProfile[] profiles = TestProfile.createTestProfiles( random_ );
33 for ( int i = 0; i < profiles.length; i++ ) {
34 testConnector( profiles[ i ] );
35 }
36 }
37
38 private void testConnector( TestProfile profile )
39 throws IOException, InterruptedException {
40 assertNull( profile.register() );
41 HubConnector connector = new HubConnector( profile );
42 connector.setAutoconnect( 1 );
43 connector.declareSubscriptions( connector.computeSubscriptions() );
44
45 Map clientMap = connector.getClientMap();
46 assertTrue( clientMap.isEmpty() );
47
48 Metadata meta = new Metadata();
49 meta.setName( "HubConnectorTest" );
50 meta.put( "colour", "yellow" );
51 connector.declareMetadata( meta );
52 assertEquals( meta, connector.getMetadata() );
53
54 assertTrue( ! connector.isConnected() );
55 profile.startHub();
56 HubConnection c0 = profile.register();
57 assertNotNull( c0 );
58 assertEquals( new HashMap(), getSubscriptions( c0 ) );
59
60 // Sometimes these tests just don't work. It's because of the
61 // indeterminate nature of SAMP - message delivery order is not
62 // guaranteed etc. I could remove these tests but .. most of the
63 // time they should, and do, pass.
64 synchronized ( clientMap ) {
65 while ( clientMap.size() != 3 ) clientMap.wait();
66 }
67 c0.unregister();
68 synchronized ( clientMap ) {
69 while ( clientMap.size() != 2 ) clientMap.wait();
70 }
71 assertTrue( connector.isConnected() );
72 assertNotNull( connector.getConnection() );
73 assertEquals( 2, clientMap.size() );
74 assertTrue( clientMap.containsKey( connector.getConnection()
75 .getRegInfo().getHubId() ) );
76 assertTrue( clientMap.containsKey( connector.getConnection()
77 .getRegInfo().getSelfId() ) );
78
79 assertEquals( meta, connector.getMetadata() );
80 assertEquals( meta, getMetadata( connector.getConnection() ) );
81 String selfId = connector.getConnection().getRegInfo().getSelfId();
82 synchronized ( clientMap ) {
83 while ( ! meta.equals( ((Client) clientMap.get( selfId ))
84 .getMetadata() ) ) {
85 clientMap.wait();
86 }
87 }
88
89 Subscriptions subs = connector.getSubscriptions();
90 assertTrue( subs.containsKey( "samp.hub.event.register" ) );
91 assertTrue( ! subs.containsKey( ECHO_MTYPE ) );
92 assertEquals( subs, getSubscriptions( connector.getConnection() ) );
93 synchronized ( clientMap ) {
94 while ( ! subs.equals( ((Client) clientMap.get( selfId ))
95 .getSubscriptions() ) ) {
96 clientMap.wait();
97 }
98 }
99
100 connector.addMessageHandler( new TestMessageHandler() );
101 subs = connector.computeSubscriptions();
102 assertTrue( subs.containsKey( ECHO_MTYPE ) );
103 assertTrue( ! subs.equals( connector.getSubscriptions() ) );
104 assertTrue( ! subs
105 .equals( getSubscriptions( connector.getConnection() ) ) );
106 connector.declareSubscriptions( subs );
107 assertEquals( subs, connector.getSubscriptions() );
108 assertEquals( subs, getSubscriptions( connector.getConnection() ) );
109
110 assertEquals( meta,
111 ((Client) clientMap.get( connector.getConnection()
112 .getRegInfo().getSelfId() ))
113 .getMetadata() );
114 meta.put( "colour", "blue" );
115 connector.declareMetadata( meta );
116 assertEquals( meta, connector.getMetadata() );
117 assertEquals( meta, getMetadata( connector.getConnection() ) );
118
119 delay( 500 );
120 assertEquals( subs,
121 ((Client) clientMap.get( connector.getConnection()
122 .getRegInfo().getSelfId() ))
123 .getSubscriptions() );
124 assertEquals( meta,
125 ((Client) clientMap.get( connector.getConnection()
126 .getRegInfo().getSelfId() ))
127 .getMetadata() );
128
129 RegInfo regInfo0 = connector.getConnection().getRegInfo();
130 connector.setActive( false );
131 assertNull( connector.getConnection() );
132 assertTrue( clientMap.isEmpty() );
133 connector.setActive( true );
134 RegInfo regInfo1 = connector.getConnection().getRegInfo();
135 assertTrue( ! regInfo0.getSelfId().equals( regInfo1.getSelfId() ) );
136 assertEquals( regInfo0.getHubId(), regInfo1.getHubId() );
137 connector.setAutoconnect( 0 );
138 profile.stopHub();
139 delay( 500 );
140 assertNull( connector.getConnection() );
141 assertTrue( clientMap.isEmpty() );
142 profile.startHub();
143 RegInfo regInfo2 = connector.getConnection().getRegInfo();
144 assertTrue( ! regInfo0.getPrivateKey()
145 .equals( regInfo2.getPrivateKey() ) );
146 assertTrue( ! regInfo1.getPrivateKey()
147 .equals( regInfo2.getPrivateKey() ) );
148
149 assertEquals( 2, clientMap.size() );
150 assertEquals( meta, getMetadata( connector.getConnection() ) );
151 assertEquals( subs, getSubscriptions( connector.getConnection() ) );
152 assertTrue( clientMap.containsKey( connector.getConnection()
153 .getRegInfo().getHubId() ) );
154 assertTrue( clientMap.containsKey( connector.getConnection()
155 .getRegInfo().getSelfId() ) );
156 connector.getConnection().unregister();
157 profile.stopHub();
158 }
159
160 public void testSynch() throws IOException {
161 TestProfile[] profiles = TestProfile.createTestProfiles( random_ );
162 for ( int i = 0; i < profiles.length; i++ ) {
163 testSynch( profiles[ i ] );
164 }
165 }
166
167 private void testSynch( TestProfile profile ) throws IOException {
168 profile.startHub();
169 TestMessageHandler echo = new TestMessageHandler();
170
171 HubConnector c1 = new HubConnector( profile );
172 String id1 = c1.getConnection().getRegInfo().getSelfId();
173 c1.addMessageHandler( echo );
174 Subscriptions subs1 = new Subscriptions();
175 subs1.addMType( ECHO_MTYPE );
176 c1.declareSubscriptions( subs1 );
177
178 HubConnector c2 = new HubConnector( profile );
179 String id2 = c2.getConnection().getRegInfo().getSelfId();
180 c2.addMessageHandler( echo );
181 c2.declareSubscriptions( c2.computeSubscriptions() );
182
183 Map params = new HashMap();
184 params.put( "names", Arrays.asList( new String[] { "Arthur", "Gordon",
185 "Pym" } ) );
186 Message msg = new Message( ECHO_MTYPE, params );
187 msg.check();
188 Response r1 =
189 c1.callAndWait( id2, msg, 0 );
190 Response r2 =
191 c2.callAndWait( id1, msg, 0 );
192 assertEquals( Response.OK_STATUS, r1.getStatus() );
193 assertEquals( Response.OK_STATUS, r2.getStatus() );
194 assertEquals( params, r1.getResult() );
195 assertEquals( params, r2.getResult() );
196
197 TestResultHandler tHandler = new TestResultHandler();
198 c1.call( id2, msg, tHandler, 0 );
199 int millis = tHandler.waitTillDone();
200 assertTrue( tHandler.getResponse( id2 ).isOK() );
201
202 Message msg5 = new Message( msg );
203 msg5.addParam( "waitMillis", "5000" );
204 TestResultHandler th2 = new TestResultHandler();
205 TestResultHandler th3 = new TestResultHandler();
206 c1.call( id2, msg5, th2, 1 );
207 c1.callAll( msg5, th3, 1 );
208 assertTrue( ! th2.isDone_ );
209 assertTrue( ! th3.isDone_ );
210 assertNull( th2.getResponse( id2 ) );
211 assertNull( th3.getResponse( id2 ) );
212 int delay = th2.waitTillDone() + th3.waitTillDone();
213 assertTrue( delay > 400 ); // should be about 1 sec
214 assertTrue( th2.isDone_ );
215 assertTrue( th3.isDone_ );
216 assertNull( th2.getResponse( id2 ) );
217 assertNull( th3.getResponse( id2 ) );
218
219 Message msg02 = new Message( msg );
220 msg02.addParam( "waitMillis", "200" );
221 TestResultHandler th4 = new TestResultHandler();
222 TestResultHandler th5 = new TestResultHandler();
223 long start02 = System.currentTimeMillis();
224 c1.call( id2, msg02, th4, 5 );
225 c1.callAll( msg02, th5, 5 );
226 assertTrue( ! th4.isDone_ );
227 assertTrue( ! th5.isDone_ );
228 th4.waitTillDone();
229 th5.waitTillDone();
230 assertTrue( System.currentTimeMillis() - start02 > 150 );
231 assertTrue( th4.isDone_ );
232 assertTrue( th5.isDone_ );
233 assertTrue( th4.getResponse( id2 ).isOK() );
234 assertTrue( th5.getResponse( id2 ).isOK() );
235
236 profile.stopHub();
237 }
238
239 private Subscriptions getSubscriptions( HubConnection connection )
240 throws SampException {
241 return connection
242 .getSubscriptions( connection.getRegInfo().getSelfId() );
243 }
244
245 private Metadata getMetadata( HubConnection connection )
246 throws SampException {
247 return connection.getMetadata( connection.getRegInfo().getSelfId() );
248 }
249
250 static void delay( int millis ) {
251 Object o = new Object();
252 synchronized ( o ) {
253 try {
254 o.wait( millis );
255 }
256 catch ( InterruptedException e ) {
257 throw new RuntimeException( e );
258 }
259 }
260 }
261
262 private static class TestMessageHandler extends AbstractMessageHandler {
263 TestMessageHandler() {
264 super( ECHO_MTYPE );
265 }
266 public Map processCall( HubConnection conn, String senderId,
267 Message msg ) {
268 String waitParam = (String) msg.getParam( "waitMillis" );
269 if ( waitParam != null ) {
270 int delay = SampUtils.decodeInt( waitParam );
271 delay( delay );
272 }
273 return msg.getParams();
274 }
275 }
276
277 private static class TestResultHandler implements ResultHandler {
278 volatile boolean isDone_;
279 final Map resultMap_ = Collections.synchronizedMap( new HashMap() );
280
281 public synchronized void result( Client client, Response response ) {
282 resultMap_.put( client.getId(), response );
283 notifyAll();
284 }
285
286 public synchronized void done() {
287 isDone_ = true;
288 notifyAll();
289 }
290
291 public Response getResponse( String clientId ) {
292 return (Response) resultMap_.get( clientId );
293 }
294
295 public synchronized int waitTillDone() {
296 long start = System.currentTimeMillis();
297 while ( ! isDone_ ) {
298 try {
299 wait();
300 }
301 catch ( InterruptedException e ) {
302 fail();
303 }
304 }
305 return (int) ( System.currentTimeMillis() - start );
306 }
307
308 public void reset() {
309 isDone_ = false;
310 resultMap_.clear();
311 }
312 }
313
314 public static void main( String[] args ) throws Exception {
315 HubConnectorTest t = new HubConnectorTest();
316 t.setUp();
317 t.testConnector();
318 }
319 }