2424import io .grpc .ServerBuilder ;
2525import io .grpc .ServerInterceptor ;
2626import io .grpc .ServerInterceptors ;
27+ import io .grpc .internal .BooleanLatch ;
2728import io .grpc .netty .GrpcHttp2ConnectionHandler ;
2829import io .grpc .netty .InternalNettyChannelBuilder ;
2930import io .grpc .netty .InternalNettyChannelBuilder .ProtocolNegotiatorFactory ;
4142import io .netty .util .AsciiString ;
4243import java .io .IOException ;
4344import java .net .InetSocketAddress ;
44- import java .util .concurrent .CountDownLatch ;
4545import java .util .concurrent .Executors ;
4646import java .util .concurrent .TimeUnit ;
4747import java .util .concurrent .atomic .AtomicReference ;
@@ -190,10 +190,9 @@ private void startServer(int serverFlowControlWindow) {
190190 private static class TestStreamObserver implements StreamObserver <StreamingOutputCallResponse > {
191191
192192 final AtomicReference <GrpcHttp2ConnectionHandler > grpcHandlerRef ;
193- final CountDownLatch latch = new CountDownLatch ( 1 );
193+ final BooleanLatch completed = new BooleanLatch ( );
194194 final long expectedWindow ;
195- int lastWindow ;
196- boolean wasCompleted ;
195+ volatile int lastWindow ;
197196
198197 public TestStreamObserver (
199198 AtomicReference <GrpcHttp2ConnectionHandler > grpcHandlerRef , long window ) {
@@ -208,31 +207,30 @@ public void onNext(StreamingOutputCallResponse value) {
208207 int curWindow = grpcHandler .decoder ().flowController ().initialWindowSize (connectionStream );
209208 synchronized (this ) {
210209 if (curWindow >= expectedWindow ) {
211- if (wasCompleted ) {
210+ if (completed . isSignaled () ) {
212211 return ;
213212 }
214- wasCompleted = true ;
215213 lastWindow = curWindow ;
216- onCompleted ();
217- } else if (!wasCompleted ) {
214+ completed . signal ();
215+ } else if (!completed . isSignaled () ) {
218216 lastWindow = curWindow ;
219217 }
220218 }
221219 }
222220
223221 @ Override
224222 public void onError (Throwable t ) {
225- latch . countDown ();
223+ completed . signal ();
226224 throw new RuntimeException (t );
227225 }
228226
229227 @ Override
230228 public void onCompleted () {
231- latch . countDown ();
229+ completed . signal ();
232230 }
233231
234232 public int waitFor (long duration , TimeUnit unit ) throws InterruptedException {
235- latch .await (duration , unit );
233+ assertTrue ( "should be completed" , completed .await (duration , unit ) );
236234 return lastWindow ;
237235 }
238236 }
0 commit comments