bilaharith commented on a change in pull request #2548: URL: https://github.com/apache/hadoop/pull/2548#discussion_r557593680
########## File path: hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/ITestAzureBlobFileSystemListStatusIterator.java ########## @@ -0,0 +1,190 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.fs.azurebfs; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.assertj.core.api.Assertions; +import org.junit.Test; +import org.mockito.Mockito; + +import org.apache.hadoop.fs.azurebfs.services.ListStatusRemoteIterator; +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.fs.RemoteIterator; + +/** + * Test listStatus operation. + */ +public class ITestAzureBlobFileSystemListStatusIterator + extends AbstractAbfsIntegrationTest { + + private static final int TEST_FILES_NUMBER = 1000; + + public ITestAzureBlobFileSystemListStatusIterator() throws Exception { + super(); + } + + @Test + public void testListPath() throws Exception { + final AzureBlobFileSystem fs = getFileSystem(); + String rootPath = "testRoot1"; + final List<String> fileNames = createFiles(TEST_FILES_NUMBER, rootPath, + "testListPath"); + AzureBlobFileSystemStore abfsStore = getAbfsStore(fs); + abfsStore.getAbfsConfiguration().setListMaxResults(10); + RemoteIterator<FileStatus> fsIt = fs.listStatusIterator(new Path(rootPath)); + int itrCount = 0; + while (fsIt.hasNext()) { + FileStatus fileStatus = fsIt.next(); + String pathStr = fileStatus.getPath().toString(); + fileNames.remove(pathStr); + itrCount++; + } + assertEquals(TEST_FILES_NUMBER, itrCount); + assertEquals(0, fileNames.size()); + } + + @Test + public void testNextWhenNoMoreElementsPresent() throws Exception { + final AzureBlobFileSystem fs = getFileSystem(); + String rootPathStr = "testRoot2"; + Path rootPath = new Path(rootPathStr); + getFileSystem().mkdirs(rootPath); + RemoteIterator<FileStatus> fsItr = fs.listStatusIterator(rootPath); + fsItr = Mockito.spy(fsItr); + Mockito.doReturn(false).when(fsItr).hasNext(); + + RemoteIterator<FileStatus> finalFsItr = fsItr; + Assertions.assertThatThrownBy(() -> finalFsItr.next()).describedAs( + "next() should throw NoSuchElementException if hasNext() return " + + "false").isInstanceOf(NoSuchElementException.class); + } + + @Test + public void testHasNextForEmptyDir() throws Exception { + final AzureBlobFileSystem fs = getFileSystem(); + String rootPathStr = "testRoot3"; + Path rootPath = new Path(rootPathStr); + getFileSystem().mkdirs(rootPath); + RemoteIterator<FileStatus> fsItr = fs.listStatusIterator(rootPath); + Assertions.assertThat(fsItr.hasNext()) + .describedAs("hasNext returns false for empty directory").isFalse(); + } + + @Test + public void testHasNextForFile() throws Exception { + final AzureBlobFileSystem fs = getFileSystem(); + String rootPathStr = "testRoot4"; + Path rootPath = new Path(rootPathStr); + getFileSystem().create(rootPath); + RemoteIterator<FileStatus> fsItr = fs.listStatusIterator(rootPath); + Assertions.assertThat(fsItr.hasNext()) + .describedAs("hasNext returns true for file").isTrue(); + } + + @Test + public void testHasNextForIOException() throws Exception { + final AzureBlobFileSystem fs = getFileSystem(); + String rootPathStr = "testRoot5"; + Path rootPath = new Path(rootPathStr); + getFileSystem().mkdirs(rootPath); + ListStatusRemoteIterator fsItr = (ListStatusRemoteIterator) fs + .listStatusIterator(rootPath); + Thread.sleep(1000); + + String exceptionMessage = "test exception"; + setPrivateField(fsItr, ListStatusRemoteIterator.class, "ioException", + new IOException(exceptionMessage)); + setPrivateFinalField(fsItr, ListStatusRemoteIterator.class, Review comment: Done ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
