Skip to content

Commit 4f2b110

Browse files
authored
s2a: Improve S2AStubTest clarity and performance (#12618)
Refactors S2AStubTest to address issues in #12581: - Rename `send_receiveOkStatus` to test successful responses using FakeWriter - Add `send_withUnavailableService_throwsDeadlineExceeded` with 1-second deadline for timeout testing - as is <img width="484" height="351" alt="image2" src="https://github.com/user-attachments/assets/24320f43-32fe-4305-9daf-29d2570ecaf1" /> - to be <img width="596" height="404" alt="image" src="https://github.com/user-attachments/assets/6f8b9c4c-ca6c-46ae-8e99-d405a504308e" /> Reduces test execution time from 20 seconds to 1 second while clarifying test intent.
1 parent 1be6815 commit 4f2b110

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

s2a/src/main/java/io/grpc/s2a/internal/handshaker/S2AStub.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class S2AStub implements AutoCloseable {
4343
private final BlockingQueue<Result> responses = new ArrayBlockingQueue<>(10);
4444
private S2AServiceGrpc.S2AServiceStub serviceStub;
4545
private StreamObserver<SessionReq> writer;
46+
private long deadlineSeconds = HANDSHAKE_RPC_DEADLINE_SECS;
4647
private boolean doneReading = false;
4748
private boolean doneWriting = false;
4849
private boolean isClosed = false;
@@ -53,6 +54,14 @@ public static S2AStub newInstance(S2AServiceGrpc.S2AServiceStub serviceStub) {
5354
return new S2AStub(serviceStub);
5455
}
5556

57+
@VisibleForTesting
58+
static S2AStub newInstanceWithDeadline(
59+
S2AServiceGrpc.S2AServiceStub serviceStub, long deadlineSeconds) {
60+
checkNotNull(serviceStub);
61+
checkArgument(deadlineSeconds > 0);
62+
return new S2AStub(serviceStub, deadlineSeconds);
63+
}
64+
5665
@VisibleForTesting
5766
static S2AStub newInstanceForTesting(StreamObserver<SessionReq> writer) {
5867
checkNotNull(writer);
@@ -63,6 +72,11 @@ private S2AStub(S2AServiceGrpc.S2AServiceStub serviceStub) {
6372
this.serviceStub = serviceStub;
6473
}
6574

75+
private S2AStub(S2AServiceGrpc.S2AServiceStub serviceStub, long deadlineSeconds) {
76+
this.serviceStub = serviceStub;
77+
this.deadlineSeconds = deadlineSeconds;
78+
}
79+
6680
private S2AStub(StreamObserver<SessionReq> writer) {
6781
this.writer = writer;
6882
}
@@ -154,7 +168,7 @@ private void createWriterIfNull() {
154168
writer =
155169
serviceStub
156170
.withWaitForReady()
157-
.withDeadlineAfter(HANDSHAKE_RPC_DEADLINE_SECS, SECONDS)
171+
.withDeadlineAfter(deadlineSeconds, SECONDS)
158172
.setUpSession(reader);
159173
}
160174
}

s2a/src/test/java/io/grpc/s2a/internal/handshaker/S2AStubTest.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ public void setUp() {
6161

6262
@Test
6363
public void send_receiveOkStatus() throws Exception {
64-
ObjectPool<Channel> channelPool =
65-
SharedResourcePool.forResource(
66-
S2AHandshakerServiceChannel.getChannelResource(
67-
S2A_ADDRESS, InsecureChannelCredentials.create()));
68-
S2AServiceGrpc.S2AServiceStub serviceStub = S2AServiceGrpc.newStub(channelPool.getObject());
69-
S2AStub newStub = S2AStub.newInstance(serviceStub);
64+
SessionReq req =
65+
SessionReq.newBuilder()
66+
.setGetTlsConfigurationReq(
67+
GetTlsConfigurationReq.newBuilder()
68+
.setConnectionSide(ConnectionSide.CONNECTION_SIDE_CLIENT))
69+
.build();
7070

71-
IOException expected =
72-
assertThrows(IOException.class, () -> newStub.send(SessionReq.getDefaultInstance()));
71+
SessionResp resp = stub.send(req);
7372

74-
assertThat(expected).hasMessageThat().contains("DEADLINE_EXCEEDED");
73+
assertThat(resp.hasGetTlsConfigurationResp()).isTrue();
74+
assertThat(resp.getGetTlsConfigurationResp().hasClientTlsConfiguration()).isTrue();
7575
}
7676

7777
@Test
@@ -233,6 +233,21 @@ public void send_afterEarlyClose_receivesClosedException() throws InterruptedExc
233233
assertThat(expected).hasMessageThat().contains("Stream to the S2A is closed.");
234234
}
235235

236+
@Test
237+
public void send_withUnavailableService_throwsDeadlineExceeded() throws Exception {
238+
ObjectPool<Channel> channelPool =
239+
SharedResourcePool.forResource(
240+
S2AHandshakerServiceChannel.getChannelResource(
241+
S2A_ADDRESS, InsecureChannelCredentials.create()));
242+
S2AServiceGrpc.S2AServiceStub serviceStub = S2AServiceGrpc.newStub(channelPool.getObject());
243+
S2AStub newStub = S2AStub.newInstanceWithDeadline(serviceStub, 1);
244+
245+
IOException expected =
246+
assertThrows(IOException.class, () -> newStub.send(SessionReq.getDefaultInstance()));
247+
248+
assertThat(expected).hasMessageThat().contains("DEADLINE_EXCEEDED");
249+
}
250+
236251
@Test
237252
public void send_failToWrite() throws Exception {
238253
FailWriter failWriter = new FailWriter();

0 commit comments

Comments
 (0)