Skip to content

Commit

Permalink
Update REST Action test to no longer mock a final class
Browse files Browse the repository at this point in the history
Instead, the test now populates dummy data.

Signed-off-by: Kartik Ganesh <gkart@amazon.com>
  • Loading branch information
kartg committed Apr 26, 2022
1 parent 529b676 commit 5f974bb
Showing 1 changed file with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,7 @@ public void testRestRecoveryAction() {
when(targetNode.getHostName()).thenReturn(randomAlphaOfLength(8));
when(state.getTargetNode()).thenReturn(targetNode);

ReplicationLuceneIndex index = mock(ReplicationLuceneIndex.class);

final int totalRecoveredFiles = randomIntBetween(1, 64);
when(index.totalRecoverFiles()).thenReturn(totalRecoveredFiles);
final int recoveredFileCount = randomIntBetween(0, totalRecoveredFiles);
when(index.recoveredFileCount()).thenReturn(recoveredFileCount);
when(index.recoveredFilesPercent()).thenReturn((100f * recoveredFileCount) / totalRecoveredFiles);
when(index.totalFileCount()).thenReturn(randomIntBetween(totalRecoveredFiles, 2 * totalRecoveredFiles));

final int totalRecoveredBytes = randomIntBetween(1, 1 << 24);
when(index.totalRecoverBytes()).thenReturn((long) totalRecoveredBytes);
final int recoveredBytes = randomIntBetween(0, totalRecoveredBytes);
when(index.recoveredBytes()).thenReturn((long) recoveredBytes);
when(index.recoveredBytesPercent()).thenReturn((100f * recoveredBytes) / totalRecoveredBytes);
when(index.totalRecoverBytes()).thenReturn((long) randomIntBetween(totalRecoveredBytes, 2 * totalRecoveredBytes));
ReplicationLuceneIndex index = createTestIndex();
when(state.getIndex()).thenReturn(index);

final RecoveryState.Translog translog = mock(RecoveryState.Translog.class);
Expand Down Expand Up @@ -215,6 +201,36 @@ public void testRestRecoveryAction() {
}
}

private ReplicationLuceneIndex createTestIndex() {
ReplicationLuceneIndex index = new ReplicationLuceneIndex();
final int filesToRecoverCount = randomIntBetween(1, 64);
final int recoveredFilesCount = randomIntBetween(0, filesToRecoverCount);
addTestFileMetadata(index, 0, recoveredFilesCount, false, true);
addTestFileMetadata(index, recoveredFilesCount, filesToRecoverCount, false, false);

final int totalFilesCount = randomIntBetween(filesToRecoverCount, 2 * filesToRecoverCount);
addTestFileMetadata(index, filesToRecoverCount, totalFilesCount, true, false);
return index;
}

private void addTestFileMetadata(ReplicationLuceneIndex index, int startIndex, int endIndex, boolean reused, boolean isFullyRecovered) {
for (int i = startIndex; i < endIndex; i++) {
final int completeFileSize = randomIntBetween(1, 1024);
index.addFileDetail(String.valueOf(i), completeFileSize, reused);

if (!reused) {
final int recoveredFileSize;
if (isFullyRecovered) {
recoveredFileSize = completeFileSize;

} else {
recoveredFileSize = randomIntBetween(0, completeFileSize);
}
index.addRecoveredBytesToFile(String.valueOf(i), recoveredFileSize);
}
}
}

private static String percent(float percent) {
return String.format(Locale.ROOT, "%1.1f%%", percent);
}
Expand Down

0 comments on commit 5f974bb

Please sign in to comment.