Skip to content

Commit

Permalink
Added tests for handling incorrectly-reported sequence numbers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jchambers committed Feb 22, 2015
1 parent 50279a1 commit d22a79b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
49 changes: 49 additions & 0 deletions src/test/java/com/relayrides/pushy/apns/ApnsConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,53 @@ public void testShutdownAtSendAttemptLimit() throws Exception {
this.waitForLatch(limitLatch);
assertEquals(notificationCount - sendAttemptLimit, totalLatch.getCount());
}

@Test
public void testHandleBogusSequenceNumber() throws Exception {
// This covers a weird upstream regression/behavior change where the APNs gateway will send a sequence number of
// zero if we send a notification with a zero-length token. See https://github.com/relayrides/pushy/issues/149
// for additional discussion.

this.getApnsServer().setShouldSendIncorrectSequenceNumber(true);

try {
final Object mutex = new Object();

final TestListener listener = new TestListener(mutex);
final ApnsConnection<SimpleApnsPushNotification> apnsConnection =
new ApnsConnection<SimpleApnsPushNotification>(
TEST_ENVIRONMENT, SSLTestUtil.createSSLContextForTestClient(), this.getEventLoopGroup(),
new ApnsConnectionConfiguration(), listener, TEST_CONNECTION_NAME);

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);

synchronized (mutex) {
apnsConnection.shutdownGracefully();

while (!listener.connectionClosed) {
mutex.wait();
}
}

assertTrue(listener.connectionClosed);
assertNull(listener.rejectedNotification);
assertNull(listener.rejectionReason);
assertTrue(listener.unprocessedNotifications.isEmpty());
} finally {
this.getApnsServer().setShouldSendIncorrectSequenceNumber(false);
}
}
}
13 changes: 12 additions & 1 deletion src/test/java/com/relayrides/pushy/apns/MockApnsServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class MockApnsServer {
private Channel channel;

private boolean shouldSendErrorResponses = true;
private boolean shouldSendIncorrectSequenceNumber = false;

public static final int EXPECTED_TOKEN_SIZE = 32;
public static final int MAX_PAYLOAD_SIZE = 2048;
Expand Down Expand Up @@ -292,8 +293,10 @@ public void exceptionCaught(final ChannelHandlerContext context, final Throwable
if (decoderException.getCause() instanceof ApnsDecoderException) {
if (this.server.shouldSendErrorResponses()) {
final ApnsDecoderException apnsDecoderException = (ApnsDecoderException)decoderException.getCause();
final int sequenceNumber = this.server.shouldSendIncorrectSequenceNumber ? 0 : apnsDecoderException.sequenceNumber;

final RejectedNotification rejectedNotification =
new RejectedNotification(apnsDecoderException.sequenceNumber, apnsDecoderException.reason);
new RejectedNotification(sequenceNumber, apnsDecoderException.reason);

context.writeAndFlush(rejectedNotification).addListener(ChannelFutureListener.CLOSE);
}
Expand Down Expand Up @@ -349,6 +352,14 @@ public boolean shouldSendErrorResponses() {
return this.shouldSendErrorResponses;
}

public boolean shouldSendIncorrectSequenceNumber() {
return shouldSendIncorrectSequenceNumber;
}

public void setShouldSendIncorrectSequenceNumber(boolean shouldSendIncorrectSequenceNumber) {
this.shouldSendIncorrectSequenceNumber = shouldSendIncorrectSequenceNumber;
}

private void acceptNotification(final SendableApnsPushNotification<SimpleApnsPushNotification> receivedNotification) {
synchronized (this.countdownLatches) {
for (final CountDownLatch latch : this.countdownLatches) {
Expand Down

0 comments on commit d22a79b

Please sign in to comment.