2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNull;
|
|
|
|
|
2015-02-26 18:41:17 +01:00
|
|
|
import org.jivesoftware.smack.filter.StanzaFilter;
|
2015-02-05 11:17:27 +01:00
|
|
|
import org.jivesoftware.smack.packet.Stanza;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.junit.Test;
|
|
|
|
|
2017-01-03 11:12:34 +01:00
|
|
|
public class StanzaCollectorTest
|
2014-02-17 23:58:40 +01:00
|
|
|
{
|
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
@Test
|
|
|
|
public void verifyRollover() throws InterruptedException
|
|
|
|
{
|
|
|
|
TestStanzaCollector collector = new TestStanzaCollector(null, new OKEverything(), 5);
|
|
|
|
|
2017-05-23 16:45:04 +02:00
|
|
|
for (int i = 0; i < 6; i++)
|
2017-02-07 22:02:40 +01:00
|
|
|
{
|
|
|
|
Stanza testPacket = new TestPacket(i);
|
|
|
|
collector.processStanza(testPacket);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that '0' has rolled off
|
|
|
|
assertEquals("1", collector.nextResultBlockForever().getStanzaId());
|
|
|
|
assertEquals("2", collector.nextResultBlockForever().getStanzaId());
|
|
|
|
assertEquals("3", collector.nextResultBlockForever().getStanzaId());
|
|
|
|
assertEquals("4", collector.nextResultBlockForever().getStanzaId());
|
|
|
|
assertEquals("5", collector.pollResult().getStanzaId());
|
|
|
|
assertNull(collector.pollResult());
|
|
|
|
|
2017-05-23 16:45:04 +02:00
|
|
|
for (int i = 10; i < 15; i++)
|
2017-02-07 22:02:40 +01:00
|
|
|
{
|
|
|
|
Stanza testPacket = new TestPacket(i);
|
|
|
|
collector.processStanza(testPacket);
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals("10", collector.nextResultBlockForever().getStanzaId());
|
|
|
|
assertEquals("11", collector.nextResultBlockForever().getStanzaId());
|
|
|
|
assertEquals("12", collector.nextResultBlockForever().getStanzaId());
|
|
|
|
assertEquals("13", collector.nextResultBlockForever().getStanzaId());
|
|
|
|
assertEquals("14", collector.pollResult().getStanzaId());
|
|
|
|
assertNull(collector.pollResult());
|
|
|
|
|
|
|
|
assertNull(collector.nextResult(1000));
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2015-03-18 21:51:25 +01:00
|
|
|
/**
|
|
|
|
* Although this doesn't guarentee anything due to the nature of threading, it can potentially
|
|
|
|
* catch problems.
|
|
|
|
*/
|
2017-02-07 22:02:40 +01:00
|
|
|
@Test
|
|
|
|
public void verifyThreadSafety()
|
|
|
|
{
|
|
|
|
int insertCount = 500;
|
|
|
|
final TestStanzaCollector collector = new TestStanzaCollector(null, new OKEverything(), insertCount);
|
|
|
|
|
|
|
|
Thread consumer1 = new Thread(new Runnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Thread.sleep(3);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
Stanza packet = collector.nextResultBlockForever();
|
|
|
|
// System.out.println(Thread.currentThread().getName() + " packet: " + packet);
|
|
|
|
}
|
|
|
|
}
|
2015-02-14 09:43:44 +01:00
|
|
|
catch (InterruptedException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
consumer1.setName("consumer 1");
|
|
|
|
|
|
|
|
Thread consumer2 = new Thread(new Runnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2017-12-13 23:10:11 +01:00
|
|
|
Stanza p;
|
2017-02-07 22:02:40 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Thread.sleep(3);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
try {
|
2015-02-14 09:43:44 +01:00
|
|
|
p = collector.nextResult(1);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2017-02-07 22:02:40 +01:00
|
|
|
// System.out.println(Thread.currentThread().getName() + " packet: " + p);
|
|
|
|
}
|
|
|
|
while (p != null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
consumer2.setName("consumer 2");
|
|
|
|
|
|
|
|
Thread consumer3 = new Thread(new Runnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2017-12-13 23:10:11 +01:00
|
|
|
Stanza p;
|
2017-02-07 22:02:40 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Thread.sleep(3);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
p = collector.pollResult();
|
|
|
|
// System.out.println(Thread.currentThread().getName() + " packet: " + p);
|
|
|
|
}
|
|
|
|
while (p != null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
consumer3.setName("consumer 3");
|
|
|
|
|
|
|
|
consumer1.start();
|
|
|
|
consumer2.start();
|
|
|
|
consumer3.start();
|
|
|
|
|
2017-05-23 16:45:04 +02:00
|
|
|
for (int i = 0; i < insertCount; i++)
|
2017-02-07 22:02:40 +01:00
|
|
|
{
|
|
|
|
collector.processStanza(new TestPacket(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Thread.sleep(5000);
|
|
|
|
consumer3.join();
|
|
|
|
consumer2.join();
|
|
|
|
consumer1.interrupt();
|
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
}
|
2017-11-20 08:53:19 +01:00
|
|
|
// We cannot guarantee that this is going to pass due to the possible issue of timing between consumer 1
|
2017-02-07 22:02:40 +01:00
|
|
|
// and main, but the probability is extremely remote.
|
|
|
|
assertNull(collector.pollResult());
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
static class OKEverything implements StanzaFilter
|
2017-02-07 22:02:40 +01:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public boolean accept(Stanza packet)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
static class TestStanzaCollector extends StanzaCollector
|
2017-02-07 22:02:40 +01:00
|
|
|
{
|
|
|
|
protected TestStanzaCollector(XMPPConnection conection, StanzaFilter packetFilter, int size)
|
|
|
|
{
|
|
|
|
super(conection, StanzaCollector.newConfiguration().setStanzaFilter(packetFilter).setSize(size));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
static class TestPacket extends Stanza
|
2017-02-07 22:02:40 +01:00
|
|
|
{
|
|
|
|
public TestPacket(int i)
|
|
|
|
{
|
|
|
|
setStanzaId(String.valueOf(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toXML()
|
|
|
|
{
|
|
|
|
return "<packetId>" + getStanzaId() + "</packetId>";
|
|
|
|
}
|
2016-05-25 22:52:29 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return toXML();
|
|
|
|
}
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|