Skip to content

Commit

Permalink
Explicitly wait for the initial SETTINGS frame for some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Chambers committed May 2, 2016
1 parent 66083b1 commit 24c1b06
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/main/java/com/relayrides/pushy/apns/ApnsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,13 @@ public boolean isConnected() {
return (connectionReadyPromise != null && connectionReadyPromise.isSuccess());
}

/*
* Waits for the initial SETTINGS frame from the server after connecting. For testing purposes only.
*/
void waitForInitialSettings() throws InterruptedException {
this.connectionReadyPromise.channel().pipeline().get(ApnsClientHandler.class).waitForInitialSettings();
}

/**
* <p>Returns a {@code Future} that will succeed when the client has re-established a connection to the APNs gateway.
* Callers may use this method to determine when it is safe to resume sending notifications after a send attempt
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/relayrides/pushy/apns/ApnsClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -58,6 +59,7 @@

class ApnsClientHandler<T extends ApnsPushNotification> extends Http2ConnectionHandler {

private final AtomicBoolean receivedInitialSettings = new AtomicBoolean(false);
private long nextStreamId = 1;

private final Map<Integer, T> pushNotificationsByStreamId = new HashMap<>();
Expand Down Expand Up @@ -137,6 +139,16 @@ public ApnsClientHandler<S> build() {
}

private class ApnsClientHandlerFrameAdapter extends Http2FrameAdapter {
@Override
public void onSettingsRead(final ChannelHandlerContext context, final Http2Settings settings) {
log.trace("Received settings from APNs gateway: {}", settings);

synchronized (ApnsClientHandler.this.receivedInitialSettings) {
ApnsClientHandler.this.receivedInitialSettings.set(true);
ApnsClientHandler.this.receivedInitialSettings.notifyAll();
}
}

@Override
public int onDataRead(final ChannelHandlerContext context, final int streamId, final ByteBuf data, final int padding, final boolean endOfStream) throws Http2Exception {
log.trace("Received data from APNs gateway on stream {}: {}", streamId, data.toString(UTF8));
Expand Down Expand Up @@ -338,4 +350,15 @@ public void exceptionCaught(final ChannelHandlerContext context, final Throwable
log.warn("APNs client pipeline caught an exception.", cause);
}
}

/*
* Waits for the initial SETTINGS frame from the server after connecting. For testing purposes only.
*/
void waitForInitialSettings() throws InterruptedException {
synchronized (this.receivedInitialSettings) {
while (!this.receivedInitialSettings.get()) {
this.receivedInitialSettings.wait();
}
}
}
}
21 changes: 20 additions & 1 deletion src/test/java/com/relayrides/pushy/apns/ApnsClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.After;
import org.junit.AfterClass;
Expand Down Expand Up @@ -434,6 +434,25 @@ public void testSendNotification() throws Exception {
assertTrue(response.isAccepted());
}

/*
* This addresses an issue identified in https://github.com/relayrides/pushy/issues/283 where the initial SETTINGS
* frame from the server would trigger a flush, which was masking some issues with the deferred flush code.
*/
@Test
public void testSendNotificationAfterInitialSettings() throws Exception {
this.client.waitForInitialSettings();

final String testToken = ApnsClientTest.generateRandomToken();

this.server.registerToken(DEFAULT_TOPIC, testToken);

final SimpleApnsPushNotification pushNotification = new SimpleApnsPushNotification(testToken, null, "test-payload");
final PushNotificationResponse<SimpleApnsPushNotification> response =
this.client.sendNotification(pushNotification).get();

assertTrue(response.isAccepted());
}

@Test
public void testSendNotificationWithImmediateFlush() throws Exception {
this.client.disconnect().await();
Expand Down

0 comments on commit 24c1b06

Please sign in to comment.