Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
Signed-off-by: Bukhtawar Khan <bukhtawa@amazon.com>
  • Loading branch information
Bukhtawar committed Aug 24, 2023
1 parent cc5ef6b commit d10d2ee
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.common.blobstore.transfer.stream;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.NIOFSDirectory;
import org.apache.lucene.store.RateLimiter;
import org.junit.After;
import org.junit.Before;

import java.io.IOException;

public class RateLimitingOffsetRangeInputStreamTests extends ResettableCheckedInputStreamBaseTest {

private Directory directory;

@Override
@Before
public void setUp() throws Exception {
super.setUp();
directory = new NIOFSDirectory(testFile.getParent());
}

@Override
protected OffsetRangeInputStream getOffsetRangeInputStream(long size, long position) throws IOException {
return new RateLimitingOffsetRangeInputStream(
new OffsetRangeIndexInputStream(directory.openInput(testFile.getFileName().toString(), IOContext.DEFAULT), size, position),
() -> new RateLimiter.SimpleRateLimiter(randomIntBetween(10, 20)),
(t) -> {}
);
}

@Override
@After
public void tearDown() throws Exception {
directory.close();
super.tearDown();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.repositories.blobstore;

import org.opensearch.cluster.metadata.RepositoryMetadata;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.env.Environment;
import org.opensearch.env.TestEnvironment;
import org.opensearch.indices.recovery.RecoverySettings;
import org.opensearch.repositories.Repository;
import org.opensearch.repositories.fs.FsRepository;
import org.opensearch.test.OpenSearchTestCase;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;

import static org.hamcrest.Matchers.equalTo;

public class BlobStoreRemoteTransferTests extends OpenSearchTestCase {

private BlobStoreRepository repository;

public void testDefaultThrottlingValues() throws Exception {
repository = (BlobStoreRepository) createRepository();
try (ByteArrayInputStream input = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8))) {
InputStream is = repository.maybeRateLimitRemoteDownloadTransfers(input);
assertBusy(() -> {
is.read();
assertThat(repository.getRemoteDownloadThrottleTimeInNanos(), equalTo(0L));
assertThat(repository.getRemoteUploadThrottleTimeInNanos(), equalTo(0L));
});

}
}

/** Create a {@link Repository} with a random name **/
protected Repository createRepository() {
Settings settings = Settings.builder().put("location", randomAlphaOfLength(10)).build();
RepositoryMetadata repositoryMetadata = new RepositoryMetadata(randomAlphaOfLength(10), FsRepository.TYPE, settings);
final ClusterService clusterService = BlobStoreTestUtil.mockClusterService(repositoryMetadata);
final FsRepository repository = new FsRepository(
repositoryMetadata,
createEnvironment(),
xContentRegistry(),
clusterService,
new RecoverySettings(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS))
) {
@Override
protected void assertSnapshotOrGenericThread() {
// eliminate thread name check as we create repo manually
}
};
clusterService.addStateApplier(event -> repository.updateState(event.state()));
// Apply state once to initialize repo properly like RepositoriesService would
repository.updateState(clusterService.state());
repository.start();
return repository;
}

/** Create a {@link Environment} with random path.home and path.repo **/
private Environment createEnvironment() {
Path home = createTempDir();
return TestEnvironment.newEnvironment(
Settings.builder()
.put(Environment.PATH_HOME_SETTING.getKey(), home.toAbsolutePath())
.put(Environment.PATH_REPO_SETTING.getKey(), home.resolve("repo").toAbsolutePath())
.build()
);
}
}

0 comments on commit d10d2ee

Please sign in to comment.