@Test public void testWaitForPendingOperationsToFinish() throws Exception { // For the purposes of this test, we're happy just as long as we don't time out waiting for // writes to finish. { final Object mutex = new Object(); final TestListener listener = new TestListener(mutex); final ApnsConnection<SimpleApnsPushNotification> apnsConnection = new ApnsConnection<SimpleApnsPushNotification>( TEST_ENVIRONMENT, SSLTestUtil.createSSLContextForTestClient(), this.getEventLoopGroup(), listener); apnsConnection.waitForPendingWritesToFinish(); apnsConnection.shutdownImmediately(); } { final Object mutex = new Object(); final TestListener listener = new TestListener(mutex); final ApnsConnection<SimpleApnsPushNotification> apnsConnection = new ApnsConnection<SimpleApnsPushNotification>( TEST_ENVIRONMENT, SSLTestUtil.createSSLContextForTestClient(), this.getEventLoopGroup(), listener); synchronized (mutex) { apnsConnection.connect(); while (!listener.connectionSucceeded) { mutex.wait(); } } assertTrue(listener.connectionSucceeded); for (int i = 0; i < 1000; i++) { apnsConnection.sendNotification(this.createTestNotification()); } apnsConnection.waitForPendingWritesToFinish(); apnsConnection.shutdownGracefully(); } }
@Test public void testShutdownImmediately() throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, InterruptedException { final Object mutex = new Object(); final TestListener listener = new TestListener(mutex); final ApnsConnection<SimpleApnsPushNotification> apnsConnection = new ApnsConnection<SimpleApnsPushNotification>( TEST_ENVIRONMENT, SSLTestUtil.createSSLContextForTestClient(), this.getEventLoopGroup(), listener); synchronized (mutex) { apnsConnection.connect(); while (!listener.connectionSucceeded) { mutex.wait(); } } assertTrue(listener.connectionSucceeded); synchronized (mutex) { apnsConnection.shutdownImmediately(); while (!listener.connectionClosed) { mutex.wait(); } } assertTrue(listener.connectionClosed); }
@Test public void testSendNotification() throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, InterruptedException { final Object mutex = new Object(); final TestListener listener = new TestListener(mutex); final ApnsConnection<SimpleApnsPushNotification> apnsConnection = new ApnsConnection<SimpleApnsPushNotification>( TEST_ENVIRONMENT, SSLTestUtil.createSSLContextForTestClient(), this.getEventLoopGroup(), listener); final CountDownLatch latch = this.getApnsServer().getAcceptedNotificationCountDownLatch(1); synchronized (mutex) { apnsConnection.connect(); while (!listener.connectionSucceeded) { mutex.wait(); } } assertTrue(listener.connectionSucceeded); apnsConnection.sendNotification(this.createTestNotification()); this.waitForLatch(latch); }
@Test public void testConnectionRefusal() throws Exception { final Object mutex = new Object(); final TestListener listener = new TestListener(mutex); final ApnsEnvironment connectionRefusedEnvironment = new ApnsEnvironment("localhost", 7876, "localhost", 7877); final ApnsConnection<SimpleApnsPushNotification> apnsConnection = new ApnsConnection<SimpleApnsPushNotification>( connectionRefusedEnvironment, SSLTestUtil.createSSLContextForTestClient("/pushy-test-client.jks"), this.getEventLoopGroup(), listener); synchronized (mutex) { apnsConnection.connect(); while (!listener.connectionFailed) { mutex.wait(); } } assertTrue(listener.connectionFailed); }
@Test public void testConnectUntrustedKeystore() throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, InterruptedException { final Object mutex = new Object(); final TestListener listener = new TestListener(mutex); final ApnsConnection<SimpleApnsPushNotification> apnsConnection = new ApnsConnection<SimpleApnsPushNotification>( TEST_ENVIRONMENT, SSLTestUtil.createSSLContextForTestClient("/pushy-test-client-untrusted.jks"), this.getEventLoopGroup(), listener); synchronized (mutex) { apnsConnection.connect(); while (!listener.connectionFailed) { mutex.wait(); } } assertTrue(listener.connectionFailed); assertTrue(listener.connectionFailureCause instanceof SSLHandshakeException); }
@Test public void testConnect() throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, InterruptedException { // For this test, we just want to make sure that connection succeeds and nothing explodes. final Object mutex = new Object(); final TestListener listener = new TestListener(mutex); final ApnsConnection<SimpleApnsPushNotification> apnsConnection = new ApnsConnection<SimpleApnsPushNotification>( TEST_ENVIRONMENT, SSLTestUtil.createSSLContextForTestClient(), this.getEventLoopGroup(), listener); synchronized (mutex) { apnsConnection.connect(); while (!listener.connectionSucceeded) { mutex.wait(); } } assertTrue(listener.connectionSucceeded); }
@Test public void testShutdownImmediatelyBeforeConnect() throws Exception { final Object mutex = new Object(); final TestListener listener = new TestListener(mutex); final ApnsConnection<SimpleApnsPushNotification> apnsConnection = new ApnsConnection<SimpleApnsPushNotification>( TEST_ENVIRONMENT, SSLTestUtil.createSSLContextForTestClient(), this.getEventLoopGroup(), listener); apnsConnection.shutdownImmediately(); }
@Test(expected = IllegalStateException.class) public void testDoubleConnect() throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { final ApnsConnection<SimpleApnsPushNotification> apnsConnection = new ApnsConnection<SimpleApnsPushNotification>( TEST_ENVIRONMENT, SSLTestUtil.createSSLContextForTestClient(), this.getEventLoopGroup(), new TestListener(new Object())); apnsConnection.connect(); apnsConnection.connect(); }
@Test public void testSendNotificationWithError() throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, InterruptedException { final Object mutex = new Object(); final TestListener listener = new TestListener(mutex); final ApnsConnection<SimpleApnsPushNotification> apnsConnection = new ApnsConnection<SimpleApnsPushNotification>( TEST_ENVIRONMENT, SSLTestUtil.createSSLContextForTestClient(), this.getEventLoopGroup(), listener); synchronized (mutex) { apnsConnection.connect(); while (!listener.connectionSucceeded) { mutex.wait(); } } assertTrue(listener.connectionSucceeded); final SimpleApnsPushNotification bogusNotification = new SimpleApnsPushNotification( new byte[] {}, "This is a bogus notification and should be rejected."); synchronized (mutex) { apnsConnection.sendNotification(bogusNotification); while (!listener.connectionClosed) { mutex.wait(); } } assertTrue(listener.connectionClosed); assertEquals(bogusNotification, listener.rejectedNotification); assertEquals(RejectedNotificationReason.MISSING_TOKEN, listener.rejectionReason); }
@Test public void testDoubleShutdownGracefully() throws Exception { final Object mutex = new Object(); final TestListener listener = new TestListener(mutex); final ApnsConnection<SimpleApnsPushNotification> apnsConnection = new ApnsConnection<SimpleApnsPushNotification>( TEST_ENVIRONMENT, SSLTestUtil.createSSLContextForTestClient(), this.getEventLoopGroup(), listener); synchronized (mutex) { apnsConnection.connect(); while (!listener.connectionSucceeded) { mutex.wait(); } } assertTrue(listener.connectionSucceeded); synchronized (mutex) { apnsConnection.shutdownGracefully(); apnsConnection.shutdownGracefully(); while (!listener.connectionClosed) { mutex.wait(); } } assertTrue(listener.connectionClosed); assertNull(listener.rejectedNotification); assertNull(listener.rejectionReason); assertTrue(listener.unprocessedNotifications.isEmpty()); }