Esempio n. 1
0
  @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();
    }
  }
Esempio n. 2
0
  @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);
  }
Esempio n. 3
0
  @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);
  }
Esempio n. 4
0
  @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);
  }
Esempio n. 5
0
  @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);
  }
Esempio n. 6
0
  @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);
  }
Esempio n. 7
0
  @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();
  }
Esempio n. 8
0
  @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();
  }
Esempio n. 9
0
  @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);
  }
Esempio n. 10
0
  @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());
  }