Fixed issue #25 due to bad packetID generation

This commit is contained in:
andrea
2015-04-07 04:29:00 +02:00
parent 8574299ed5
commit 1a2dade994
2 changed files with 33 additions and 3 deletions
@@ -199,7 +199,7 @@ public class MapDBPersistentStore implements IMessagesStore, ISessionsStore {
} }
/** /**
* Return the next valid packetIdentifer for the given client session. * Return the next valid packetIdentifier for the given client session.
* */ * */
@Override @Override
public int nextPacketID(String clientID) { public int nextPacketID(String clientID) {
@@ -211,7 +211,7 @@ public class MapDBPersistentStore implements IMessagesStore, ISessionsStore {
this.m_inFlightIds.put(clientID, inFlightForClient); this.m_inFlightIds.put(clientID, inFlightForClient);
return nextPacketId; return nextPacketId;
} }
int maxId = Collections.max(inFlightForClient); int maxId = inFlightForClient.isEmpty() ? 0 : Collections.max(inFlightForClient);
int nextPacketId = (maxId + 1) % 0xFFFF; int nextPacketId = (maxId + 1) % 0xFFFF;
inFlightForClient.add(nextPacketId); inFlightForClient.add(nextPacketId);
return nextPacketId; return nextPacketId;
@@ -38,7 +38,6 @@ public class MapDBPersistentStoreTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
File dbFile = new File(DEFAULT_PERSISTENT_PATH); File dbFile = new File(DEFAULT_PERSISTENT_PATH);
assertFalse(String.format("The DB storagefile %s already exists", DEFAULT_PERSISTENT_PATH), dbFile.exists()); assertFalse(String.format("The DB storagefile %s already exists", DEFAULT_PERSISTENT_PATH), dbFile.exists());
@@ -72,4 +71,35 @@ public class MapDBPersistentStoreTest {
Subscription sub = subscriptions.get(0); Subscription sub = subscriptions.get(0);
assertEquals(overrindingSubscription.getRequestedQos(), sub.getRequestedQos()); assertEquals(overrindingSubscription.getRequestedQos(), sub.getRequestedQos());
} }
@Test
public void testNextPacketID_notExistingClientSession() {
int packetId = m_storageService.nextPacketID("NOT_EXISTING_CLI");
assertEquals(1, packetId);
}
@Test
public void testNextPacketID_existingClientSession() {
//Force creation of inflight map for the CLIENT session
int packetId = m_storageService.nextPacketID("CLIENT");
assertEquals(1, packetId);
//request a second packetID
packetId = m_storageService.nextPacketID("CLIENT");
assertEquals(2, packetId);
}
@Test
public void testNextPacketID() {
//request a first ID
int packetId = m_storageService.nextPacketID("CLIENT");
assertEquals(1, packetId);
//release the ID
m_storageService.cleanInFlight("CLIENT", packetId);
//request a second packetID, counter restarts from 0
packetId = m_storageService.nextPacketID("CLIENT");
assertEquals(1, packetId);
}
} }