Skip to content

Commit

Permalink
okhttp: unit testing sending initialWindowSize in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Schechter committed Feb 25, 2020
1 parent 2d72688 commit 2fd5b20
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
28 changes: 18 additions & 10 deletions okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java
Expand Up @@ -605,16 +605,7 @@ sslSocketFactory, hostnameVerifier, sock, getOverridenHost(), getOverridenPort()
});
// Schedule to send connection preface & settings before any other write.
try {
synchronized (lock) {
frameWriter.connectionPreface();
Settings settings = new Settings();
OkHttpSettingsUtil.set(settings, OkHttpSettingsUtil.INITIAL_WINDOW_SIZE, initialWindowSize);
frameWriter.settings(settings);
if (initialWindowSize > DEFAULT_WINDOW_SIZE) {
frameWriter.windowUpdate(
Utils.CONNECTION_STREAM_ID, initialWindowSize - DEFAULT_WINDOW_SIZE);
}
}
sendConnectionPrefaceAndSettings();
} finally {
latch.countDown();
}
Expand All @@ -634,6 +625,23 @@ public void run() {
return null;
}

/**
* Should only be called once when the transport is first established.
*/
@VisibleForTesting
void sendConnectionPrefaceAndSettings() {
synchronized (lock) {
frameWriter.connectionPreface();
Settings settings = new Settings();
OkHttpSettingsUtil.set(settings, OkHttpSettingsUtil.INITIAL_WINDOW_SIZE, initialWindowSize);
frameWriter.settings(settings);
if (initialWindowSize > DEFAULT_WINDOW_SIZE) {
frameWriter.windowUpdate(
Utils.CONNECTION_STREAM_ID, initialWindowSize - DEFAULT_WINDOW_SIZE);
}
}
}

private Socket createHttpProxySocket(InetSocketAddress address, InetSocketAddress proxyAddress,
String proxyUsername, String proxyPassword) throws StatusException {
try {
Expand Down
30 changes: 30 additions & 0 deletions okhttp/src/test/java/io/grpc/okhttp/OkHttpClientTransportTest.java
Expand Up @@ -404,6 +404,36 @@ public void maxMessageSizeShouldBeEnforced() throws Exception {
shutdownAndVerify();
}

@Test
public void includeInitialWindowSizeInFirstSettings() throws Exception {
int initialWindowSize = 65535;
startTransport(
DEFAULT_START_STREAM_ID, null, true, DEFAULT_MAX_MESSAGE_SIZE, initialWindowSize, null);
clientTransport.sendConnectionPrefaceAndSettings();

ArgumentCaptor<Settings> settings = ArgumentCaptor.forClass(Settings.class);
verify(frameWriter, timeout(TIME_OUT_MS)).settings(settings.capture());
assertEquals(65535, settings.getValue().get(7));
}

/**
* A "large" window size is anything over 65535 (the starting size for any connection-level
* flow control value).
*/
@Test
public void includeInitialWindowSizeInFirstSettings_largeWindowSize() throws Exception {
int initialWindowSize = 75535; // 65535 + 10000
startTransport(
DEFAULT_START_STREAM_ID, null, true, DEFAULT_MAX_MESSAGE_SIZE, initialWindowSize, null);
clientTransport.sendConnectionPrefaceAndSettings();

ArgumentCaptor<Settings> settings = ArgumentCaptor.forClass(Settings.class);
verify(frameWriter, timeout(TIME_OUT_MS)).settings(settings.capture());
assertEquals(75535, settings.getValue().get(7));

verify(frameWriter, timeout(TIME_OUT_MS)).windowUpdate(0, 10000);
}

/**
* When nextFrame throws IOException, the transport should be aborted.
*/
Expand Down

0 comments on commit 2fd5b20

Please sign in to comment.