diff options
author | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-07-28 11:26:53 (GMT) |
---|---|---|
committer | Alex Clayton <alex.clayton@isode.com> | 2016-01-14 15:13:35 (GMT) |
commit | 701abcb162dfb3e7cc8c6a9ada81a16d1fc8d4ee (patch) | |
tree | 64f49648eefdca1e2245b6899a3b49e428063f13 | |
parent | c72128ba30a386704244322c15146a1b4a609a12 (diff) | |
download | stroke-701abcb162dfb3e7cc8c6a9ada81a16d1fc8d4ee.zip stroke-701abcb162dfb3e7cc8c6a9ada81a16d1fc8d4ee.tar.bz2 |
Minor changes to PubSubManagerImpl and StanzaAckRequester.
Small changes to PubSubManagerImpl and StanzaAckRequester making them equivalent with Swiften.
License:
This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.
Test-Information:
Added tests for StanzaAckResponder and StanzaAckRequester, which passes.
Change-Id: I99878fc6552ed18e353848a0cea6584c26b2ba0c
5 files changed, 308 insertions, 13 deletions
diff --git a/src/com/isode/stroke/pubsub/PubSubManagerImpl.java b/src/com/isode/stroke/pubsub/PubSubManagerImpl.java index ab1e8c6..a8bdafd 100644 --- a/src/com/isode/stroke/pubsub/PubSubManagerImpl.java +++ b/src/com/isode/stroke/pubsub/PubSubManagerImpl.java @@ -14,6 +14,7 @@ import com.isode.stroke.elements.Message; import com.isode.stroke.parser.payloadparsers.PubSubEvent; import com.isode.stroke.queries.IQRouter; import com.isode.stroke.signals.Slot1; +import com.isode.stroke.signals.SignalConnection; public class PubSubManagerImpl extends PubSubManager { @@ -21,16 +22,30 @@ public class PubSubManagerImpl extends PubSubManager { stanzaChannel_ = stanzaChannel; router_ = router; - stanzaChannel.onMessageReceived.connect(new Slot1<Message>() { + onMessageReceivedConnection = stanzaChannel.onMessageReceived.connect(new Slot1<Message>() { public void call(Message message) { - PubSubEvent event = (PubSubEvent)message.getPayload(new PubSubEvent()); - if (event != null) { - onEvent.emit(message.getFrom(), event.getPayload()); - } + handleMessageRecevied(message); } }); } - - StanzaChannel stanzaChannel_; - IQRouter router_; + + protected void finalize() throws Throwable { + try { + onMessageReceivedConnection.disconnect(); + } + finally { + super.finalize(); + } + } + + private void handleMessageRecevied(Message message) { + if (message.getPayload(new PubSubEvent()) != null) { + PubSubEvent event = (PubSubEvent)message.getPayload(new PubSubEvent()); + onEvent.emit(message.getFrom(), event.getPayload()); + } + } + + private SignalConnection onMessageReceivedConnection; + private StanzaChannel stanzaChannel_; + private IQRouter router_; } diff --git a/src/com/isode/stroke/streammanagement/StanzaAckRequester.java b/src/com/isode/stroke/streammanagement/StanzaAckRequester.java index c106851..8ff7dc7 100644 --- a/src/com/isode/stroke/streammanagement/StanzaAckRequester.java +++ b/src/com/isode/stroke/streammanagement/StanzaAckRequester.java @@ -20,7 +20,7 @@ public class StanzaAckRequester { static final long MAX_HANDLED_STANZA_COUNT = Long.parseLong("4294967295"); //boost::numeric_cast<unsigned int>((1ULL<<32) - 1); public StanzaAckRequester() { - + this.lastHandledStanzasCount = 0L; } public void handleStanzaSent(Stanza stanza) { @@ -34,7 +34,7 @@ public class StanzaAckRequester { long i = lastHandledStanzasCount; while (i != handledStanzasCount) { if (unackedStanzas.isEmpty()) { - //std::cerr << "Warning: Server acked more stanzas than we sent" << std::endl; + System.err.println("Warning: Server acked more stanzas than we sent"); break; } Stanza ackedStanza = unackedStanzas.get(0); @@ -49,7 +49,7 @@ public class StanzaAckRequester { public Signal1<Stanza> onStanzaAcked = new Signal1<Stanza>(); - private long lastHandledStanzasCount; + long lastHandledStanzasCount; - private List<Stanza> unackedStanzas = new ArrayList<Stanza>(); + List<Stanza> unackedStanzas = new ArrayList<Stanza>(); } diff --git a/src/com/isode/stroke/streammanagement/StanzaAckResponder.java b/src/com/isode/stroke/streammanagement/StanzaAckResponder.java index 43faca2..7c4fd22 100644 --- a/src/com/isode/stroke/streammanagement/StanzaAckResponder.java +++ b/src/com/isode/stroke/streammanagement/StanzaAckResponder.java @@ -25,5 +25,5 @@ public class StanzaAckResponder { onAck.emit(handledStanzasCount); } public Signal1<Long> onAck = new Signal1<Long>(); - private long handledStanzasCount = 0; + long handledStanzasCount = 0; } diff --git a/test/com/isode/stroke/streammanagement/StanzaAckRequesterTest.java b/test/com/isode/stroke/streammanagement/StanzaAckRequesterTest.java new file mode 100644 index 0000000..38b6943 --- /dev/null +++ b/test/com/isode/stroke/streammanagement/StanzaAckRequesterTest.java @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.streammanagement; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.Before; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.Presence; +import com.isode.stroke.elements.Message; +import com.isode.stroke.elements.Stanza; +import com.isode.stroke.signals.Slot1; +import com.isode.stroke.signals.Slot; +import java.util.Vector; + +public class StanzaAckRequesterTest { + + private int acksRequested; + private Vector<Stanza> ackedStanzas = new Vector<Stanza>(); + + private Message createMessage(final String id) { + Message result = new Message(); + result.setID(id); + return result; + } + + private IQ createIQ(final String id) { + IQ result = new IQ(); + result.setID(id); + return result; + } + + private Presence createPresence(final String id) { + Presence result = new Presence(); + result.setID(id); + return result; + } + + private StanzaAckRequester createRequester() { + StanzaAckRequester requester = new StanzaAckRequester(); + requester.onRequestAck.connect(new Slot() { + @Override + public void call() { + handleRequestAck(); + } + }); + requester.onStanzaAcked.connect(new Slot1<Stanza>() { + @Override + public void call(Stanza s) { + handleStanzaAcked(s); + } + }); + return requester; + } + + private void handleRequestAck() { + acksRequested++; + } + + private void handleStanzaAcked(Stanza stanza) { + ackedStanzas.add(stanza); + } + + @Before + public void setUp() { + acksRequested = 0; + } + + @Test + public void testHandleStanzaSent_MessageRequestsAck() { + StanzaAckRequester testling = createRequester(); + testling.handleStanzaSent(createMessage("m1")); + + assertEquals(1, acksRequested); + } + + @Test + public void testHandleStanzaSent_IQDoesNotRequestAck() { + StanzaAckRequester testling = createRequester(); + testling.handleStanzaSent(createIQ("iq1")); + + assertEquals(0, acksRequested); + } + + @Test + public void testHandleStanzaSent_PresenceDoesNotRequestAck() { + StanzaAckRequester testling = createRequester(); + testling.handleStanzaSent(createPresence("p1")); + + assertEquals(0, acksRequested); + } + + @Test + public void testHandleAckReceived_AcksStanza() { + StanzaAckRequester testling = createRequester(); + testling.handleStanzaSent(createMessage("m1")); + + testling.handleAckReceived(1); + + assertEquals(1, (ackedStanzas.size())); + assertEquals(("m1"), ackedStanzas.get(0).getID()); + } + + @Test + public void testHandleAckReceived_AcksMultipleMessages() { + StanzaAckRequester testling = createRequester(); + testling.handleStanzaSent(createMessage("m1")); + testling.handleStanzaSent(createMessage("m2")); + + testling.handleAckReceived(2); + + assertEquals(2, (ackedStanzas.size())); + assertEquals(("m1"), ackedStanzas.get(0).getID()); + assertEquals(("m2"), ackedStanzas.get(1).getID()); + } + + @Test + public void testHandleAckReceived_AcksMultipleStanzas() { + StanzaAckRequester testling = createRequester(); + testling.handleStanzaSent(createIQ("iq1")); + testling.handleStanzaSent(createPresence("p1")); + testling.handleStanzaSent(createMessage("m1")); + + testling.handleAckReceived(3); + + assertEquals(3, (ackedStanzas.size())); + assertEquals(("iq1"), ackedStanzas.get(0).getID()); + assertEquals(("p1"), ackedStanzas.get(1).getID()); + assertEquals(("m1"), ackedStanzas.get(2).getID()); + } + + @Test + public void testHandleAckReceived_MultipleAcks() { + StanzaAckRequester testling = createRequester(); + testling.handleStanzaSent(createMessage("m1")); + testling.handleAckReceived(1); + + testling.handleStanzaSent(createMessage("m2")); + testling.handleStanzaSent(createMessage("m3")); + testling.handleAckReceived(3); + + assertEquals(3, (ackedStanzas.size())); + assertEquals(("m1"), ackedStanzas.get(0).getID()); + assertEquals(("m2"), ackedStanzas.get(1).getID()); + assertEquals(("m3"), ackedStanzas.get(2).getID()); + } + + // Handle stanza ack count wrapping, as per the XEP + @Test + public void testHandleAckReceived_WrapAround() { + StanzaAckRequester testling = createRequester(); + testling.lastHandledStanzasCount = Long.parseLong("4294967295"); + testling.handleStanzaSent(createMessage("m1")); + testling.handleStanzaSent(createMessage("m2")); + + testling.handleAckReceived(1); + + assertEquals(2, (ackedStanzas.size())); + assertEquals(("m1"), ackedStanzas.get(0).getID()); + assertEquals(("m2"), ackedStanzas.get(1).getID()); + } +} diff --git a/test/com/isode/stroke/streammanagement/StanzaAckResponderTest.java b/test/com/isode/stroke/streammanagement/StanzaAckResponderTest.java new file mode 100644 index 0000000..dc0074e --- /dev/null +++ b/test/com/isode/stroke/streammanagement/StanzaAckResponderTest.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.streammanagement; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.Before; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.Presence; +import com.isode.stroke.elements.Message; +import com.isode.stroke.elements.Stanza; +import com.isode.stroke.signals.Slot1; +import com.isode.stroke.signals.Slot; +import java.util.Vector; + +public class StanzaAckResponderTest { + + private Vector<Long> acks = new Vector<Long>(); + + private Message createMessage(final String id) { + Message result = new Message(); + result.setID(id); + return result; + } + + private StanzaAckResponder createResponder() { + StanzaAckResponder responder = new StanzaAckResponder(); + responder.onAck.connect(new Slot1<Long>() { + @Override + public void call(Long l) { + handleAck(l); + } + }); + return responder; + } + + private void handleAck(Long h) { + acks.add(h); + } + + @Test + public void testHandleAckRequestReceived_AcksStanza() { + StanzaAckResponder testling = createResponder(); + testling.handleStanzaReceived(); + + testling.handleAckRequestReceived(); + + assertEquals(1, (acks.size())); + assertEquals(Long.valueOf(1L), acks.get(0)); + } + + @Test + public void testHandleAckRequestReceived_AcksMultipleStanzas() { + StanzaAckResponder testling = createResponder(); + testling.handleStanzaReceived(); + testling.handleStanzaReceived(); + + testling.handleAckRequestReceived(); + + assertEquals(1, (acks.size())); + assertEquals(Long.valueOf(2L), acks.get(0)); + } + + @Test + public void testHandleAckRequestReceived_MultipleAcks() { + StanzaAckResponder testling = createResponder(); + testling.handleStanzaReceived(); + testling.handleAckRequestReceived(); + + testling.handleStanzaReceived(); + testling.handleAckRequestReceived(); + + assertEquals(2, (acks.size())); + assertEquals(Long.valueOf(1L), acks.get(0)); + assertEquals(Long.valueOf(2L), acks.get(1)); + } + + // Handle stanza ack count wrapping, as per the XEP + @Test + public void testHandleAckRequestReceived_WrapAround() { + StanzaAckResponder testling = createResponder(); + testling.handledStanzasCount = Long.parseLong("4294967295"); + testling.handleStanzaReceived(); + testling.handleStanzaReceived(); + + testling.handleAckRequestReceived(); + + assertEquals(1, (acks.size())); + assertEquals(Long.valueOf(1L), acks.get(0)); + } + +}
\ No newline at end of file |