added test support for shared Igtimi LiveDataConnection which discovered a few bugs which are now also fixed by this commit

This commit is contained in:
Axel Uhl
2014-01-31 17:12:37 +01:00
parent 454487fd2c
commit 6d7dd6af92
3 changed files with 20 additions and 3 deletions
@@ -2,6 +2,8 @@ package com.sap.sailing.domain.igtimiadapter.websocket;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
@@ -159,6 +161,10 @@ public class WebSocketTest {
Account account = igtimiConnectionFactory.registerAccountForWhichClientIsAuthorized("9fded995cf21c8ed91ddaec13b220e8d5e44c65808d22ec2b1b7c32261121f26");
IgtimiConnection conn = igtimiConnectionFactory.connect(account);
LiveDataConnection liveDataConnection = conn.getOrCreateLiveConnection(Collections.singleton("GA-EN-AAEJ"));
LiveDataConnection redundantSecondSharedConnection = conn.getOrCreateLiveConnection(Collections.singleton("GA-EN-AAEJ"));
assertTrue(liveDataConnection instanceof LiveDataConnectionWrapper);
assertTrue(redundantSecondSharedConnection instanceof LiveDataConnectionWrapper);
assertSame(((LiveDataConnectionWrapper) liveDataConnection).getActualConnection(), ((LiveDataConnectionWrapper) redundantSecondSharedConnection).getActualConnection());
liveDataConnection.addListener(new BulkFixReceiver() {
@Override
public void received(Iterable<Fix> fixes) {
@@ -167,6 +173,11 @@ public class WebSocketTest {
});
assertNotNull(liveDataConnection);
assertTrue("Connection handshake not successful within 5s", liveDataConnection.waitForConnection(5000l));
liveDataConnection.stop();
liveDataConnection.stop(); // this won't stop the actual connection because it's still shared with redundantSecondSharedConnection
redundantSecondSharedConnection.stop(); // now this should stop the actual connection
LiveDataConnection secondRedundantSecondSharedConnection = conn.getOrCreateLiveConnection(Collections.singleton("GA-EN-AAEJ"));
assertTrue(secondRedundantSecondSharedConnection instanceof LiveDataConnectionWrapper);
// a new actual connection is expected to have been created
assertNotSame(((LiveDataConnectionWrapper) liveDataConnection).getActualConnection(), ((LiveDataConnectionWrapper) secondRedundantSecondSharedConnection).getActualConnection());
}
}
@@ -31,7 +31,7 @@ public class LiveDataConnectionFactoryImpl implements LiveDataConnectionFactory
public synchronized LiveDataConnection getOrCreateLiveDataConnection(Iterable<String> deviceSerialNumbers) throws Exception {
Set<String> deviceSerialNumbersAsSet = new HashSet<>();
Util.addAll(deviceSerialNumbers, deviceSerialNumbersAsSet);
LiveDataConnection result = dataConnectionsForDeviceSerialNumbers.get(dataConnectionsForDeviceSerialNumbers);
LiveDataConnection result = dataConnectionsForDeviceSerialNumbers.get(deviceSerialNumbersAsSet);
if (result == null) {
result = new WebSocketConnectionManager(connectionFactory, deviceSerialNumbers, account);
dataConnectionsForDeviceSerialNumbers.put(deviceSerialNumbersAsSet, result);
@@ -43,7 +43,7 @@ public class LiveDataConnectionFactoryImpl implements LiveDataConnectionFactory
}
usageCount++;
usageCounts.put(result, usageCount);
return result;
return new LiveDataConnectionWrapper(this, result);
}
public synchronized void stop(LiveDataConnection actualConnection) throws Exception {
@@ -34,4 +34,10 @@ public class LiveDataConnectionWrapper implements LiveDataConnection {
actualConnection.addListener(listener);
}
/**
* Makes the actual connection available to other classes in this package, particularly the test classes in the test fragment of the same package
*/
LiveDataConnection getActualConnection() {
return actualConnection;
}
}