Skip to content

Commit 8d4dc8e

Browse files
committed
servlet: add unit test verifying AsyncServletOutputStreamWriter.onWritePossible non-blocking behavior (grpc#12778)
1 parent fd48842 commit 8d4dc8e

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2026 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.servlet;
18+
19+
import static org.junit.Assert.assertTrue;
20+
21+
import io.grpc.servlet.AsyncServletOutputStreamWriter.ActionItem;
22+
import io.grpc.servlet.AsyncServletOutputStreamWriter.Log;
23+
import java.io.IOException;
24+
import java.util.concurrent.atomic.AtomicBoolean;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.JUnit4;
28+
29+
/** Unit tests for {@link AsyncServletOutputStreamWriter}. */
30+
@RunWith(JUnit4.class)
31+
public class AsyncServletOutputStreamWriterTest {
32+
33+
@Test(timeout = 5000)
34+
public void onWritePossibleWhenAlreadyReadyAndDrainedReturnsImmediately() throws IOException {
35+
AtomicBoolean written = new AtomicBoolean(false);
36+
ActionItem writeAction = () -> written.set(true);
37+
38+
AsyncServletOutputStreamWriter writer =
39+
new AsyncServletOutputStreamWriter(
40+
(bytes, len) -> writeAction,
41+
() -> {},
42+
() -> {},
43+
() -> true,
44+
new Log() {});
45+
46+
// Initial onWritePossible call turns readyAndDrained to true
47+
writer.onWritePossible();
48+
49+
long startTime = System.nanoTime();
50+
// Subsequent onWritePossible call when readyAndDrained is already true must return non-blockingly
51+
writer.onWritePossible();
52+
long durationMs = (System.nanoTime() - startTime) / 1_000_000;
53+
54+
assertTrue("onWritePossible must not park or block thread", durationMs < 1000);
55+
}
56+
}

0 commit comments

Comments
 (0)