Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Bansal <bansvaru@amazon.com>
  • Loading branch information
linuxpi committed Mar 14, 2024
1 parent dde1609 commit 1406e16
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,7 @@ public void onSuccess(TransferFileSnapshot fileSnapshot) {
logger.error("Failure to update translog upload success stats", ex);
}

try {
// add() gets a dedicated try/catch as its on the critical path
add(fileSnapshot.getName(), TransferState.SUCCESS);
} catch (IllegalStateException ex) {
throw new FileTransferException(fileSnapshot, ex);
}
add(fileSnapshot.getName(), TransferState.SUCCESS);
}

void add(String file, boolean success) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import java.util.List;
import java.util.Set;

import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.spy;

public class FileTransferTrackerTests extends OpenSearchTestCase {

protected final ShardId shardId = new ShardId("index", "_na_", 1);
Expand Down Expand Up @@ -94,6 +98,32 @@ public void testOnFailure() throws IOException {
}
}

public void testOnSuccessStatsFailure() throws IOException {
RemoteTranslogTransferTracker localRemoteTranslogTransferTracker = spy(remoteTranslogTransferTracker);
doAnswer((count) -> { throw new NullPointerException("Error while updating stats"); }).when(localRemoteTranslogTransferTracker)
.addUploadBytesSucceeded(anyLong());

FileTransferTracker localFileTransferTracker = new FileTransferTracker(shardId, localRemoteTranslogTransferTracker);

Path testFile = createTempFile();
int fileSize = 128;
Files.write(testFile, randomByteArrayOfLength(fileSize), StandardOpenOption.APPEND);
try (
FileSnapshot.TransferFileSnapshot transferFileSnapshot = new FileSnapshot.TransferFileSnapshot(
testFile,
randomNonNegativeLong(),
null
);
) {
Set<FileSnapshot.TransferFileSnapshot> toUpload = new HashSet<>(2);
toUpload.add(transferFileSnapshot);
localFileTransferTracker.recordBytesForFiles(toUpload);
localRemoteTranslogTransferTracker.addUploadBytesStarted(fileSize);
localFileTransferTracker.onSuccess(transferFileSnapshot);
assertEquals(localFileTransferTracker.allUploaded().size(), 1);
}
}

public void testUploaded() throws IOException {
Path testFile = createTempFile();
int fileSize = 128;
Expand Down

0 comments on commit 1406e16

Please sign in to comment.