2020import static java .util .concurrent .TimeUnit .SECONDS ;
2121
2222import com .google .common .collect .ImmutableList ;
23+ import com .google .common .util .concurrent .SettableFuture ;
2324import com .google .protobuf .ByteString ;
2425import io .grpc .Grpc ;
2526import io .grpc .InsecureChannelCredentials ;
2627import io .grpc .ManagedChannel ;
2728import io .grpc .Server ;
2829import io .grpc .ServerBuilder ;
29- import io .grpc .benchmarks .Utils ;
3030import io .grpc .s2a .internal .handshaker .ValidatePeerCertificateChainReq .VerificationMode ;
3131import io .grpc .stub .StreamObserver ;
3232import java .io .IOException ;
@@ -49,51 +49,52 @@ public final class FakeS2AServerTest {
4949
5050 private static final ImmutableList <ByteString > FAKE_CERT_DER_CHAIN =
5151 ImmutableList .of (ByteString .copyFrom ("fake-der-chain" .getBytes (StandardCharsets .US_ASCII )));
52- private int port ;
5352 private String serverAddress ;
54- private SessionResp response = null ;
5553 private Server fakeS2AServer ;
5654
5755 @ Before
5856 public void setUp () throws Exception {
59- port = Utils .pickUnusedPort ();
60- fakeS2AServer = ServerBuilder .forPort (port ).addService (new FakeS2AServer ()).build ();
57+ fakeS2AServer = ServerBuilder .forPort (0 ).addService (new FakeS2AServer ()).build ();
6158 fakeS2AServer .start ();
62- serverAddress = String .format ("localhost:%d" , port );
59+ serverAddress = String .format ("localhost:%d" , fakeS2AServer . getPort () );
6360 }
6461
6562 @ After
66- public void tearDown () {
63+ public void tearDown () throws Exception {
6764 fakeS2AServer .shutdown ();
65+ fakeS2AServer .awaitTermination (10 , SECONDS );
6866 }
6967
7068 @ Test
7169 public void callS2AServerOnce_getTlsConfiguration_returnsValidResult ()
72- throws InterruptedException , IOException {
70+ throws InterruptedException , IOException , java . util . concurrent . ExecutionException {
7371 ExecutorService executor = Executors .newSingleThreadExecutor ();
7472 logger .info ("Client connecting to: " + serverAddress );
7573 ManagedChannel channel =
7674 Grpc .newChannelBuilder (serverAddress , InsecureChannelCredentials .create ())
7775 .executor (executor )
7876 .build ();
79-
77+ SettableFuture < SessionResp > respFuture = SettableFuture . create ();
8078 try {
8179 S2AServiceGrpc .S2AServiceStub asyncStub = S2AServiceGrpc .newStub (channel );
8280 StreamObserver <SessionReq > requestObserver =
8381 asyncStub .setUpSession (
8482 new StreamObserver <SessionResp >() {
83+ SessionResp recvResp ;
8584 @ Override
8685 public void onNext (SessionResp resp ) {
87- response = resp ;
86+ recvResp = resp ;
8887 }
8988
9089 @ Override
9190 public void onError (Throwable t ) {
92- throw new RuntimeException (t );
91+ respFuture . setException (t );
9392 }
9493
9594 @ Override
96- public void onCompleted () {}
95+ public void onCompleted () {
96+ respFuture .set (recvResp );
97+ }
9798 });
9899 try {
99100 requestObserver .onNext (
@@ -138,36 +139,39 @@ public void onCompleted() {}
138139 .addCiphersuites (
139140 Ciphersuite .CIPHERSUITE_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 )))
140141 .build ();
141- assertThat (response ).ignoringRepeatedFieldOrder ().isEqualTo (expected );
142+ assertThat (respFuture . get () ).ignoringRepeatedFieldOrder ().isEqualTo (expected );
142143 }
143144
144145 @ Test
145146 public void callS2AServerOnce_validatePeerCertifiate_returnsValidResult ()
146- throws InterruptedException {
147+ throws InterruptedException , java . util . concurrent . ExecutionException {
147148 ExecutorService executor = Executors .newSingleThreadExecutor ();
148149 logger .info ("Client connecting to: " + serverAddress );
149150 ManagedChannel channel =
150151 Grpc .newChannelBuilder (serverAddress , InsecureChannelCredentials .create ())
151152 .executor (executor )
152153 .build ();
153-
154+ SettableFuture < SessionResp > respFuture = SettableFuture . create ();
154155 try {
155156 S2AServiceGrpc .S2AServiceStub asyncStub = S2AServiceGrpc .newStub (channel );
156157 StreamObserver <SessionReq > requestObserver =
157158 asyncStub .setUpSession (
158159 new StreamObserver <SessionResp >() {
160+ private SessionResp recvResp ;
159161 @ Override
160162 public void onNext (SessionResp resp ) {
161- response = resp ;
163+ recvResp = resp ;
162164 }
163165
164166 @ Override
165167 public void onError (Throwable t ) {
166- throw new RuntimeException (t );
168+ respFuture . setException (t );
167169 }
168170
169171 @ Override
170- public void onCompleted () {}
172+ public void onCompleted () {
173+ respFuture .set (recvResp );
174+ }
171175 });
172176 try {
173177 requestObserver .onNext (
@@ -200,7 +204,7 @@ public void onCompleted() {}
200204 ValidatePeerCertificateChainResp .newBuilder ()
201205 .setValidationResult (ValidatePeerCertificateChainResp .ValidationResult .SUCCESS ))
202206 .build ();
203- assertThat (response ).ignoringRepeatedFieldOrder ().isEqualTo (expected );
207+ assertThat (respFuture . get () ).ignoringRepeatedFieldOrder ().isEqualTo (expected );
204208 }
205209
206210 @ Test
0 commit comments