bilaharith commented on a change in pull request #2548: URL: https://github.com/apache/hadoop/pull/2548#discussion_r557592714
########## File path: hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/ListStatusRemoteIterator.java ########## @@ -0,0 +1,149 @@ +/** + * 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.services; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.CompletableFuture; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.fs.RemoteIterator; +import org.apache.hadoop.fs.azurebfs.AzureBlobFileSystemStore; + +public class ListStatusRemoteIterator implements RemoteIterator<FileStatus> { + + private static final Logger LOG = LoggerFactory + .getLogger(ListStatusRemoteIterator.class); + + private static final boolean FETCH_ALL_FALSE = false; + private static final int MAX_QUEUE_SIZE = 10; + + private final Path path; + private final AzureBlobFileSystemStore abfsStore; + private final ArrayBlockingQueue<Iterator<FileStatus>> iteratorsQueue; + private final Object asyncOpLock = new Object(); + + private boolean firstBatch = true; + private boolean isAsyncInProgress = false; + private String continuation; + private Iterator<FileStatus> currIterator; + private IOException ioException; + + public ListStatusRemoteIterator(final Path path, + final AzureBlobFileSystemStore abfsStore) { + this.path = path; + this.abfsStore = abfsStore; + iteratorsQueue = new ArrayBlockingQueue<>(MAX_QUEUE_SIZE); + currIterator = Collections.emptyIterator(); + fetchBatchesAsync(); + } + + @Override + public boolean hasNext() throws IOException { + if (currIterator.hasNext()) { + return true; + } + updateCurrentIterator(); + return currIterator.hasNext(); + } + + @Override + public FileStatus next() throws IOException { + if (!this.hasNext()) { + throw new NoSuchElementException(); + } + return currIterator.next(); + } + + private void updateCurrentIterator() throws IOException { + fetchBatchesAsync(); + synchronized (this) { + if (iteratorsQueue.isEmpty()) { + if (ioException != null) { + throw ioException; + } + if (isListingComplete()) { + return; + } + } + } + try { + currIterator = iteratorsQueue.take(); + if (!currIterator.hasNext() && !isListingComplete()) { + updateCurrentIterator(); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + LOG.error("Thread got interrupted: {}", e); + } + } + + private boolean isListingComplete() { + return !firstBatch && (continuation == null || continuation.isEmpty()); + } + + private void fetchBatchesAsync() { + CompletableFuture.runAsync(() -> asyncOp()); + } + + private void asyncOp() { + if (isAsyncInProgress) { + return; + } + synchronized (asyncOpLock) { + if (isAsyncInProgress) { + return; + } + isAsyncInProgress = true; + } + try { + while (!isListingComplete() && iteratorsQueue.size() <= MAX_QUEUE_SIZE) { + addNextBatchIteratorToQueue(); + } + } catch (IOException e) { + ioException = e; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + LOG.error("Thread got interrupted: {}", e); Review comment: Yes. Also we need to set the interrut flag again. ########## 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()) Review comment: Done ########## 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"; Review comment: Done ########## 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(); Review comment: Done ########## 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); Review comment: Done ########## 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"; Review comment: Done ########## File path: hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/AzureBlobFileSystemStore.java ########## @@ -865,16 +873,16 @@ public FileStatus getFileStatus(final Path path) throws IOException { startFrom); final String relativePath = getRelativePath(path); - String continuation = null; - // generate continuation token if a valid startFrom is provided. - if (startFrom != null && !startFrom.isEmpty()) { - continuation = getIsNamespaceEnabled() - ? generateContinuationTokenForXns(startFrom) - : generateContinuationTokenForNonXns(relativePath, startFrom); + if (continuation == null || continuation.length() < 1) { 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]
