ss77892 commented on code in PR #11195: URL: https://github.com/apache/ozone/pull/11195#discussion_r3961248745
########## hadoop-hdds/common/src/test/java/org/apache/hadoop/hdds/scm/TestStreamingReadResponse.java: ########## @@ -0,0 +1,163 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.hadoop.hdds.scm; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; +import org.apache.hadoop.hdds.protocol.MockDatanodeDetails; +import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerCommandRequestProto; +import org.apache.ratis.thirdparty.io.grpc.stub.ClientCallStreamObserver; +import org.junit.jupiter.api.Test; + +/** + * Tests for {@link StreamingReadResponse#awaitReady(long)}. + */ +class TestStreamingReadResponse { + + private static final class FakeRequestObserver extends ClientCallStreamObserver<ContainerCommandRequestProto> { + private final AtomicBoolean ready = new AtomicBoolean(); + + @Override + public boolean isReady() { + return ready.get(); + } + + @Override + public void setOnReadyHandler(Runnable onReadyHandler) { + } + + @Override + public void disableAutoInboundFlowControl() { + } + + @Override + public void request(int count) { + } + + @Override + public void setMessageCompression(boolean enable) { + } + + @Override + public void cancel(String message, Throwable cause) { + } + + @Override + public void onNext(ContainerCommandRequestProto value) { + } + + @Override + public void onError(Throwable t) { + } + + @Override + public void onCompleted() { + } + } + + private static StreamingReadResponse newResponse(FakeRequestObserver observer) { + return new StreamingReadResponse(MockDatanodeDetails.randomDatanodeDetails(), observer); + } + + @Test + void returnsImmediatelyWhenAlreadyReady() throws Exception { + final FakeRequestObserver observer = new FakeRequestObserver(); + observer.ready.set(true); + assertTrue(newResponse(observer).awaitReady(TimeUnit.SECONDS.toNanos(10))); + } + + @Test + void timesOutWithoutASignal() throws Exception { + final FakeRequestObserver observer = new FakeRequestObserver(); + final long start = System.nanoTime(); + assertFalse(newResponse(observer).awaitReady(TimeUnit.MILLISECONDS.toNanos(50))); + assertTrue(System.nanoTime() - start >= TimeUnit.MILLISECONDS.toNanos(50), "must wait out the timeout"); + } + + @Test + void wakesOnSignalReady() throws Exception { + final FakeRequestObserver observer = new FakeRequestObserver(); + final StreamingReadResponse response = newResponse(observer); + final CountDownLatch waiting = new CountDownLatch(1); + final AtomicBoolean result = new AtomicBoolean(); + final Thread waiter = new Thread(() -> { + try { + waiting.countDown(); Review Comment: Yeah, that might happen. I've updated the test to avoid it. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
