Search in sources :

Example 6 with XmppStringprepException

use of org.jxmpp.stringprep.XmppStringprepException in project Smack by igniterealtime.

the class PacketWriterTest method shouldBlockAndUnblockTest.

/**
     * Make sure that stanza(/packet) writer does block once the queue reaches
     * {@link PacketWriter#QUEUE_SIZE} and that
     * {@link PacketWriter#sendStanza(org.jivesoftware.smack.tcp.packet.Packet)} does unblock after the
     * interrupt.
     * 
     * @throws InterruptedException
     * @throws BrokenBarrierException
     * @throws NotConnectedException 
     * @throws XmppStringprepException 
     */
@SuppressWarnings("javadoc")
@Test
public void shouldBlockAndUnblockTest() throws InterruptedException, BrokenBarrierException, NotConnectedException, XmppStringprepException {
    XMPPTCPConnection connection = new XMPPTCPConnection("user", "pass", "example.org");
    final PacketWriter pw = connection.new PacketWriter();
    connection.packetWriter = pw;
    connection.packetReader = connection.new PacketReader();
    connection.setWriter(new BlockingStringWriter());
    pw.init();
    for (int i = 0; i < XMPPTCPConnection.PacketWriter.QUEUE_SIZE; i++) {
        pw.sendStreamElement(new Message());
    }
    final CyclicBarrier barrier = new CyclicBarrier(2);
    shutdown = false;
    prematureUnblocked = false;
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                barrier.await();
                pw.sendStreamElement(new Message());
                // should only return after the pw was interrupted
                if (!shutdown) {
                    prematureUnblocked = true;
                }
            } catch (Exception e) {
            }
            try {
                barrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
            }
        }
    });
    t.start();
    // This barrier is not strictly necessary, but may increases the chances that the threat
    // will block before we call shutdown. Otherwise we may get false positives (which is still
    // better then false negatives).
    barrier.await();
    // Not really cool, but may increases the chances for 't' to block in sendStanza.
    Thread.sleep(250);
    // Set to true for testing purposes, so that shutdown() won't wait packet writer
    pw.shutdownDone.reportSuccess();
    // Shutdown the packetwriter
    pw.shutdown(false);
    shutdown = true;
    barrier.await();
    if (prematureUnblocked) {
        fail("Should not unblock before the thread got shutdown");
    }
    synchronized (t) {
        t.notify();
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) PacketWriter(org.jivesoftware.smack.tcp.XMPPTCPConnection.PacketWriter) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Aggregations

XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)6 Jid (org.jxmpp.jid.Jid)3 IOException (java.io.IOException)2 Test (org.junit.Test)2 ConnectException (java.net.ConnectException)1 HashMap (java.util.HashMap)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 SmackException (org.jivesoftware.smack.SmackException)1 FeatureNotSupportedException (org.jivesoftware.smack.SmackException.FeatureNotSupportedException)1 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)1 XMPPException (org.jivesoftware.smack.XMPPException)1 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)1 Message (org.jivesoftware.smack.packet.Message)1 Presence (org.jivesoftware.smack.packet.Presence)1 PacketWriter (org.jivesoftware.smack.tcp.XMPPTCPConnection.PacketWriter)1 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)1 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)1 Identity (org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity)1 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)1