Skip to content

Commit

Permalink
added few tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
  • Loading branch information
sarthakaggarwal97 committed Jul 29, 2023
1 parent ecb6979 commit 3d58641
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions server/src/test/java/org/opensearch/index/codec/CodecTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import java.util.Collections;

import static org.hamcrest.Matchers.instanceOf;
import static org.opensearch.index.engine.EngineConfig.INDEX_CODEC_COMPRESSION_LEVEL_SETTING;

@SuppressCodecs("*") // we test against default codec so never get a random one here!
public class CodecTests extends OpenSearchTestCase {
Expand Down Expand Up @@ -114,7 +115,7 @@ public void testZstdNoDictWithCompressionLevel() throws Exception {

public void testBestCompressionWithCompressionLevel() {
final Settings zstdSettings = Settings.builder()
.put(EngineConfig.INDEX_CODEC_COMPRESSION_LEVEL_SETTING.getKey(), randomIntBetween(1, 6))
.put(INDEX_CODEC_COMPRESSION_LEVEL_SETTING.getKey(), randomIntBetween(1, 6))
.put(EngineConfig.INDEX_CODEC_SETTING.getKey(), randomFrom(CodecService.ZSTD_CODEC, CodecService.ZSTD_NO_DICT_CODEC))
.build();

Expand All @@ -126,7 +127,7 @@ public void testBestCompressionWithCompressionLevel() {
zstdIndexScopedSettings.validate(zstdSettings, true);

final Settings settings = Settings.builder()
.put(EngineConfig.INDEX_CODEC_COMPRESSION_LEVEL_SETTING.getKey(), randomIntBetween(1, 6))
.put(INDEX_CODEC_COMPRESSION_LEVEL_SETTING.getKey(), randomIntBetween(1, 6))
.put(EngineConfig.INDEX_CODEC_SETTING.getKey(), randomFrom(CodecService.DEFAULT_CODEC, CodecService.BEST_COMPRESSION_CODEC))
.build();
final IndexScopedSettings indexScopedSettings = new IndexScopedSettings(settings, IndexScopedSettings.BUILT_IN_INDEX_SETTINGS);
Expand All @@ -135,6 +136,44 @@ public void testBestCompressionWithCompressionLevel() {
assertTrue(e.getMessage().startsWith("Compression level cannot be set"));
}

public void testLuceneCodecsWithCompressionLevel() {
String codecName = randomFrom(Codec.availableCodecs());
Codec codec = Codec.forName(codecName);

final Settings customCodecSettings = Settings.builder()
.put(INDEX_CODEC_COMPRESSION_LEVEL_SETTING.getKey(), randomIntBetween(1, 6))
.put(EngineConfig.INDEX_CODEC_SETTING.getKey(), "Lucene95CustomCodec")
.build();

final IndexScopedSettings customCodecIndexScopedSettings = new IndexScopedSettings(
customCodecSettings,
IndexScopedSettings.BUILT_IN_INDEX_SETTINGS
);
customCodecIndexScopedSettings.validate(customCodecSettings, true);

final Settings settings = Settings.builder()
.put(INDEX_CODEC_COMPRESSION_LEVEL_SETTING.getKey(), randomIntBetween(1, 6))
.put(EngineConfig.INDEX_CODEC_SETTING.getKey(), codecName)
.build();
final IndexScopedSettings indexScopedSettings = new IndexScopedSettings(settings, IndexScopedSettings.BUILT_IN_INDEX_SETTINGS);

if (!(codec instanceof CodecSettings && ((CodecSettings) codec).supports(INDEX_CODEC_COMPRESSION_LEVEL_SETTING))) {
final IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> indexScopedSettings.validate(settings, true)
);
assertTrue(e.getMessage().startsWith("Compression level cannot be set"));
}
}

public void testZstandardCompressionLevelSupport() throws Exception {
CodecService codecService = createCodecService(false);
CodecSettings zstdCodec = (CodecSettings) codecService.codec("zstd");
CodecSettings zstdNoDictCodec = (CodecSettings) codecService.codec("zstd_no_dict");
assertTrue(zstdCodec.supports(INDEX_CODEC_COMPRESSION_LEVEL_SETTING));
assertTrue(zstdNoDictCodec.supports(INDEX_CODEC_COMPRESSION_LEVEL_SETTING));
}

public void testDefaultMapperServiceNull() throws Exception {
Codec codec = createCodecService(true).codec("default");
assertStoredFieldsCompressionEquals(Lucene95Codec.Mode.BEST_SPEED, codec);
Expand Down

0 comments on commit 3d58641

Please sign in to comment.