-
Notifications
You must be signed in to change notification settings - Fork 4k
servlet: fix write when not ready in AsyncServletOutputStreamWriter #12790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mgustimz
wants to merge
25
commits into
grpc:master
Choose a base branch
from
mgustimz:fix/12723-servlet-clean
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3f87103
servlet: fix write when not ready in AsyncServletOutputStreamWriter
mgustimz 60e9f16
servlet: fix write when not ready, narrow to writeBytes only
mgustimz 44ee627
servlet: fix type comparison in runOrBuffer
mgustimz 55e6629
Add AsyncServletOutputStreamWriterTest with mock isReady supplier
mgustimz 3ebac15
Remove unused AtomicInteger in AsyncServletOutputStreamWriterTest
mgustimz 1ef9eb8
Fix test and log message per Copilot review
mgustimz 3f81019
Split flush and writeBytes into separate branches
mgustimz d79bdb6
Clean up test per Copilot review
mgustimz c73e67a
Add test validating write without onWritePossible is buffered
mgustimz e898755
Address Copilot review items from #4251754607
mgustimz 8c217f5
Replace third test to cover flush-specific behavior
mgustimz ccb13ee
Fix flush test per Copilot review
mgustimz 1a07b84
servlet: fix flush test assertion to account for buffered writeBytes
mgustimz 5ec98e1
servlet: fix comment and add test for consecutive flush behavior
mgustimz 7924aae
fix(servlet): address review comments in AsyncServletOutputStreamWriter
mgustimz ca8408e
fix(servlet): update log message for clarity and improve test imports
mgustimz 512680b
fix(servlet): correct unpark placement and format action items in tests
mgustimz 09c4a02
fix(servlet): reorder checkState calls and improve log message for wr…
mgustimz 7ffb701
Merge branch 'grpc:master' into fix/12723-servlet-clean
mgustimz 33b5258
servlet: avoid Tomcat write stall when still ready
mgustimz 8b68f2b
servlet: preserve Undertow direct write flow
mgustimz 835a5de
servlet: improve writer test coverage
mgustimz dfe0f33
servlet: cover writer race recovery paths
mgustimz e600550
servlet: fix reflective rethrow in writer test
mgustimz 6c6a2e4
servlet: fix writer test import order
mgustimz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
214 changes: 214 additions & 0 deletions
214
servlet/src/test/java/io/grpc/servlet/AsyncServletOutputStreamWriterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| /* | ||
| * Copyright 2026 The gRPC Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.grpc.servlet; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| import io.grpc.servlet.AsyncServletOutputStreamWriter.ActionItem; | ||
| import io.grpc.servlet.AsyncServletOutputStreamWriter.Log; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
|
mgustimz marked this conversation as resolved.
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.function.BiFunction; | ||
| import java.util.function.BooleanSupplier; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| /** | ||
| * Unit test for {@link AsyncServletOutputStreamWriter} with a mock isReady supplier. | ||
| * Tests four scenarios: | ||
| * (1) A write without intervening onWritePossible() is buffered. | ||
| * (2) Consecutive writes succeed with onWritePossible() between them. | ||
| * (3) Flush behavior: goes direct when readyAndDrained=true, buffers when readyAndDrained=false. | ||
| * (4) Consecutive flushes go direct when isReady() stays true. | ||
| */ | ||
| @RunWith(JUnit4.class) | ||
| public class AsyncServletOutputStreamWriterTest { | ||
|
|
||
| /** | ||
| * Test that a write without an intervening onWritePossible() is buffered and | ||
| * does not execute immediately. This validates the Tomcat fix: writeBytes sets | ||
| * readyAndDrained=false so subsequent writes go through the container callback. | ||
| */ | ||
| @Test | ||
| public void writeBytes_withoutOnWritePossible_isBuffered() throws IOException { | ||
| List<String> actions = new ArrayList<>(); | ||
|
|
||
| BiFunction<byte[], Integer, ActionItem> writeAction = | ||
| (bytes, numBytes) -> () -> { | ||
| actions.add("write"); | ||
| }; | ||
|
|
||
| ActionItem flushAction = () -> {}; | ||
|
|
||
| ActionItem completeAction = () -> {}; | ||
|
|
||
| BooleanSupplier isReadySupplier = () -> true; | ||
|
|
||
| AsyncServletOutputStreamWriter writer = | ||
| new AsyncServletOutputStreamWriter( | ||
| writeAction, flushAction, completeAction, isReadySupplier, new Log() {}); | ||
|
mgustimz marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Initial onWritePossible to set readyAndDrained=true | ||
| writer.onWritePossible(); | ||
|
|
||
| // First write - goes direct, readyAndDrained becomes false | ||
| byte[] data1 = new byte[]{1}; | ||
| writer.writeBytes(data1, 1); | ||
| assertEquals("First write should execute", 1, actions.size()); | ||
|
|
||
| // Second write without onWritePossible - should be buffered since readyAndDrained=false | ||
| byte[] data2 = new byte[]{2}; | ||
| writer.writeBytes(data2, 1); | ||
|
|
||
| // Without onWritePossible, second write should be buffered, not executed | ||
| assertEquals("Second write should be buffered until onWritePossible", 1, actions.size()); | ||
|
|
||
| // onWritePossible drains the buffered write | ||
| writer.onWritePossible(); | ||
| assertEquals("Buffered write should drain after onWritePossible", 2, actions.size()); | ||
| } | ||
|
|
||
| /** | ||
| * Test that multiple consecutive writes succeed when isReady() always returns true | ||
| * and the container calls onWritePossible() between writes. | ||
| */ | ||
| @Test | ||
| public void writeBytes_onWritePossibleBetweenWrites_succeeds() throws IOException { | ||
| List<byte[]> writtenData = new ArrayList<>(); | ||
|
|
||
| BiFunction<byte[], Integer, ActionItem> writeAction = | ||
| (bytes, numBytes) -> () -> { | ||
| writtenData.add(Arrays.copyOf(bytes, numBytes)); | ||
| }; | ||
|
|
||
| ActionItem flushAction = () -> {}; | ||
|
|
||
| ActionItem completeAction = () -> {}; | ||
|
|
||
| BooleanSupplier isReadySupplier = () -> true; | ||
|
|
||
| AsyncServletOutputStreamWriter writer = | ||
| new AsyncServletOutputStreamWriter( | ||
| writeAction, flushAction, completeAction, isReadySupplier, new Log() {}); | ||
|
|
||
| // Initial onWritePossible to set readyAndDrained=true | ||
| writer.onWritePossible(); | ||
|
|
||
| // Write 5 times, calling onWritePossible after each write. | ||
| // With isReady staying true, each onWritePossible sets readyAndDrained | ||
| // back to true so the next write goes direct again. | ||
| for (int i = 0; i < 5; i++) { | ||
| byte[] data = new byte[]{(byte) i}; | ||
| writer.writeBytes(data, 1); | ||
| writer.onWritePossible(); | ||
| } | ||
|
|
||
| // Verify all 5 writes completed | ||
| assertEquals("All writes should complete", 5, writtenData.size()); | ||
| } | ||
|
|
||
| /** | ||
| * Test flush behavior: flush goes direct when readyAndDrained is true, | ||
| * but is buffered when readyAndDrained is false (regardless of isReady state). | ||
| * This covers the flush-specific path in runOrBuffer(). | ||
| */ | ||
| @Test | ||
| public void flush_withReadyAndDrainedFalse_isBuffered() throws IOException { | ||
|
mgustimz marked this conversation as resolved.
Outdated
|
||
| List<String> actions = new ArrayList<>(); | ||
|
|
||
| BiFunction<byte[], Integer, ActionItem> writeAction = | ||
| (bytes, numBytes) -> () -> { | ||
| actions.add("write"); | ||
| }; | ||
|
|
||
| ActionItem flushAction = () -> { | ||
| actions.add("flush"); | ||
| }; | ||
|
|
||
| ActionItem completeAction = () -> {}; | ||
|
|
||
| BooleanSupplier isReadySupplier = () -> true; | ||
|
|
||
| AsyncServletOutputStreamWriter writer = | ||
| new AsyncServletOutputStreamWriter( | ||
| writeAction, flushAction, completeAction, isReadySupplier, new Log() {}); | ||
|
|
||
| // Initial onWritePossible to set readyAndDrained=true | ||
| writer.onWritePossible(); | ||
|
|
||
| // First flush - readyAndDrained=true, isReady=true -> goes direct | ||
| writer.flush(); | ||
| assertEquals("First flush should execute directly", 1, actions.size()); | ||
|
mgustimz marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Write a byte to set readyAndDrained=false (writeBytes always clears it) | ||
| writer.writeBytes(new byte[]{1}, 1); | ||
| // The write goes direct (readyAndDrained was true) and readyAndDrained becomes false. | ||
| // Now readyAndDrained=false. | ||
|
|
||
| // Flush with readyAndDrained=false -> should be buffered (isReady doesn't matter) | ||
| writer.flush(); | ||
|
|
||
| // Only the first flush should have executed (the write was also executed, making it 2) | ||
| assertEquals("Second flush should be buffered", 2, actions.size()); | ||
|
|
||
| // Container calls onWritePossible to drain buffered flush | ||
| writer.onWritePossible(); | ||
|
|
||
| // Both flushes should have completed (3 total: write + 2 flushes) | ||
| assertEquals("Both flushes should complete after onWritePossible", 3, actions.size()); | ||
| } | ||
|
|
||
| /** | ||
| * Test that two consecutive flushes both go direct when isReady() stays true. | ||
| * This validates the new flush behavior: flush keeps readyAndDrained=true when | ||
| * isReady() is still true, allowing subsequent flushes to go direct without buffering. | ||
| */ | ||
| @Test | ||
| public void flush_consecutiveWithIsReadyTrue_bothGoDirect() throws IOException { | ||
| List<String> actions = new ArrayList<>(); | ||
|
|
||
| BiFunction<byte[], Integer, ActionItem> writeAction = | ||
| (bytes, numBytes) -> () -> { | ||
| actions.add("write"); | ||
| }; | ||
|
|
||
| ActionItem flushAction = () -> { | ||
| actions.add("flush"); | ||
| }; | ||
|
|
||
| ActionItem completeAction = () -> {}; | ||
|
|
||
| BooleanSupplier isReadySupplier = () -> true; | ||
|
|
||
| AsyncServletOutputStreamWriter writer = | ||
| new AsyncServletOutputStreamWriter( | ||
| writeAction, flushAction, completeAction, isReadySupplier, new Log() {}); | ||
|
|
||
| // Initial onWritePossible to set readyAndDrained=true | ||
| writer.onWritePossible(); | ||
|
|
||
| // Two consecutive flushes when isReady stays true - both should go direct | ||
| writer.flush(); | ||
| writer.flush(); | ||
|
|
||
| // Both flushes should execute directly (no buffering) | ||
| assertEquals("Both flushes should execute directly", 2, actions.size()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.