Search in sources :

Example 1 with ApnsDelegate

use of com.notnoop.apns.ApnsDelegate in project java-apns by notnoop.

the class ApnsConnectionCacheTest method cacheLengthNotification.

/**
     * Test to make sure that if the cache length is violated we get
     * a notification
     *
     * @throws InterruptedException
     */
@Test(timeout = 5000)
public void cacheLengthNotification() throws InterruptedException {
    server = new ApnsServerStub(FixedCertificates.serverContext().getServerSocketFactory());
    final CountDownLatch sync = new CountDownLatch(1);
    int ORIGINAL_CACHE_LENGTH = 100;
    final AtomicInteger modifiedCacheLength = new AtomicInteger();
    server.getWaitForError().acquire();
    server.start();
    ApnsService service = APNS.newService().withSSLContext(clientContext()).withGatewayDestination(LOCALHOST, server.getEffectiveGatewayPort()).withDelegate(new ApnsDelegate() {

        public void messageSent(ApnsNotification message, boolean resent) {
        }

        public void messageSendFailed(ApnsNotification message, Throwable e) {
        }

        public void connectionClosed(DeliveryError e, int messageIdentifier) {
        }

        public void cacheLengthExceeded(int newCacheLength) {
            modifiedCacheLength.set(newCacheLength);
            sync.countDown();
        }

        public void notificationsResent(int resendCount) {
        }
    }).build();
    server.stopAt(eMsg1.length() * 5 + eMsg2.length() + eMsg3.length() * 14);
    for (int i = 0; i < 5; ++i) {
        service.push(eMsg1);
    }
    service.push(eMsg2);
    for (int i = 0; i < 101; ++i) {
        service.push(eMsg3);
    }
    server.sendError(8, eMsg2.getIdentifier());
    server.getWaitForError().release();
    server.getMessages().acquire();
    sync.await();
    Assert.assertTrue(ORIGINAL_CACHE_LENGTH < modifiedCacheLength.get());
}
Also used : ApnsServerStub(com.notnoop.apns.utils.ApnsServerStub) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeliveryError(com.notnoop.apns.DeliveryError) ApnsNotification(com.notnoop.apns.ApnsNotification) SimpleApnsNotification(com.notnoop.apns.SimpleApnsNotification) EnhancedApnsNotification(com.notnoop.apns.EnhancedApnsNotification) CountDownLatch(java.util.concurrent.CountDownLatch) StartSendingApnsDelegate(com.notnoop.apns.StartSendingApnsDelegate) ApnsDelegate(com.notnoop.apns.ApnsDelegate) ApnsService(com.notnoop.apns.ApnsService)

Example 2 with ApnsDelegate

use of com.notnoop.apns.ApnsDelegate in project java-apns by notnoop.

the class ApnsConnectionCacheTest method handleTransmissionErrorInQueuedConnection.

/**
     * Test to make sure that after rejected notification
     * in-flight notifications are re-transmitted with a queued connection
     *
     * @throws InterruptedException
     */
@Ignore("Fails because old ApnsServerStub does not accept() on the connection socket for all the time.")
@Test(timeout = 10000)
public void handleTransmissionErrorInQueuedConnection() throws InterruptedException {
    server = new ApnsServerStub(FixedCertificates.serverContext().getServerSocketFactory());
    final AtomicInteger sync = new AtomicInteger(138);
    final AtomicInteger numResent = new AtomicInteger();
    final AtomicInteger numSent = new AtomicInteger();
    server.getWaitForError().acquire();
    server.start();
    ApnsService service = APNS.newService().withSSLContext(clientContext()).withGatewayDestination(LOCALHOST, server.getEffectiveGatewayPort()).asQueued().withDelegate(new ApnsDelegate() {

        public void messageSent(ApnsNotification message, boolean resent) {
            if (!resent) {
                numSent.incrementAndGet();
            }
            sync.getAndDecrement();
        }

        public void messageSendFailed(ApnsNotification message, Throwable e) {
            numSent.decrementAndGet();
            sync.incrementAndGet();
        }

        public void connectionClosed(DeliveryError e, int messageIdentifier) {
        }

        public void cacheLengthExceeded(int newCacheLength) {
        }

        public void notificationsResent(int resendCount) {
            numResent.set(resendCount);
            sync.getAndAdd(resendCount);
        }
    }).build();
    server.stopAt(eMsg3.length() * 50 + msg1.length() * 3 + eMsg2.length() * 2 + eMsg1.length() * 85);
    for (int i = 0; i < 50; ++i) {
        service.push(eMsg3);
    }
    service.push(msg1);
    service.push(eMsg2);
    service.push(eMsg1);
    service.push(msg2);
    for (int i = 0; i < 85; ++i) {
        service.push(eMsg1);
    }
    server.sendError(8, eMsg2.getIdentifier());
    server.getWaitForError().release();
    server.getMessages().acquire();
    while (sync.get() != 0) {
        Thread.yield();
    }
}
Also used : ApnsServerStub(com.notnoop.apns.utils.ApnsServerStub) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeliveryError(com.notnoop.apns.DeliveryError) ApnsNotification(com.notnoop.apns.ApnsNotification) SimpleApnsNotification(com.notnoop.apns.SimpleApnsNotification) EnhancedApnsNotification(com.notnoop.apns.EnhancedApnsNotification) StartSendingApnsDelegate(com.notnoop.apns.StartSendingApnsDelegate) ApnsDelegate(com.notnoop.apns.ApnsDelegate) ApnsService(com.notnoop.apns.ApnsService)

Aggregations

ApnsDelegate (com.notnoop.apns.ApnsDelegate)2 ApnsNotification (com.notnoop.apns.ApnsNotification)2 ApnsService (com.notnoop.apns.ApnsService)2 DeliveryError (com.notnoop.apns.DeliveryError)2 EnhancedApnsNotification (com.notnoop.apns.EnhancedApnsNotification)2 SimpleApnsNotification (com.notnoop.apns.SimpleApnsNotification)2 StartSendingApnsDelegate (com.notnoop.apns.StartSendingApnsDelegate)2 ApnsServerStub (com.notnoop.apns.utils.ApnsServerStub)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 CountDownLatch (java.util.concurrent.CountDownLatch)1