Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sachin Kale <[email protected]>
  • Loading branch information
Sachin Kale committed Oct 18, 2022
1 parent 5291135 commit a1bad44
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.opensearch.common.io.stream.FilterStreamInput;
import org.opensearch.common.io.stream.StreamInput;

import java.io.EOFException;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
Expand Down Expand Up @@ -117,7 +118,11 @@ public void reset() throws IOException {

@Override
public int read() throws IOException {
return readByte() & 0xFF;
try {
return readByte() & 0xFF;
} catch (EOFException e) {
return -1;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class FileSnapshot implements Closeable {

public FileSnapshot(Path path) throws IOException {
Objects.requireNonNull(path);
this.name = path.toString();
this.name = path.getFileName().toString();
this.path = path;
this.fileChannel = FileChannel.open(path, StandardOpenOption.READ);
}
Expand All @@ -61,7 +61,7 @@ public String getName() {
}

public long getContentLength() throws IOException {
return fileChannel == null ? fileChannel.size() : content.length;
return fileChannel == null ? content.length : fileChannel.size();
}

public InputStream inputStream() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -45,20 +47,18 @@ public void setUp() throws Exception {
}

public void testUploadBlob() throws IOException {
FileSnapshot.TransferFileSnapshot transferFileSnapshot = new FileSnapshot.TransferFileSnapshot(
createTempFile(),
randomNonNegativeLong()
);
Path testFile = createTempFile();
Files.write(testFile, randomByteArrayOfLength(128), StandardOpenOption.APPEND);
FileSnapshot.TransferFileSnapshot transferFileSnapshot = new FileSnapshot.TransferFileSnapshot(testFile, randomNonNegativeLong());
TransferService transferService = new BlobStoreTransferService(repository.blobStore(), executorService);
transferService.uploadBlob(transferFileSnapshot, repository.basePath());
}

public void testUploadBlobAsync() throws IOException, InterruptedException {
Path testFile = createTempFile();
Files.write(testFile, randomByteArrayOfLength(128), StandardOpenOption.APPEND);
AtomicBoolean succeeded = new AtomicBoolean(false);
FileSnapshot.TransferFileSnapshot transferFileSnapshot = new FileSnapshot.TransferFileSnapshot(
createTempFile(),
randomNonNegativeLong()
);
FileSnapshot.TransferFileSnapshot transferFileSnapshot = new FileSnapshot.TransferFileSnapshot(testFile, randomNonNegativeLong());
CountDownLatch latch = new CountDownLatch(1);
TransferService transferService = new BlobStoreTransferService(repository.blobStore(), executorService);
transferService.uploadBlobAsync(transferFileSnapshot, repository.basePath(), new LatchedActionListener<>(new ActionListener<>() {
Expand All @@ -74,7 +74,7 @@ public void onFailure(Exception e) {
throw new AssertionError("Failed to perform uploadBlobAsync", e);
}
}, latch));
latch.await(100, TimeUnit.MILLISECONDS);
latch.await(1000, TimeUnit.MILLISECONDS);
assertEquals(true, succeeded.get());
}

Expand Down

0 comments on commit a1bad44

Please sign in to comment.