aboutsummaryrefslogtreecommitdiffstats
path: root/filedistribution_test
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /filedistribution_test
Publish
Diffstat (limited to 'filedistribution_test')
-rw-r--r--filedistribution_test/.gitignore8
-rw-r--r--filedistribution_test/OWNERS1
-rw-r--r--filedistribution_test/pom.xml43
-rw-r--r--filedistribution_test/src/test/java/FileDistributionManagerTestCase.java120
4 files changed, 172 insertions, 0 deletions
diff --git a/filedistribution_test/.gitignore b/filedistribution_test/.gitignore
new file mode 100644
index 00000000000..9f355e75159
--- /dev/null
+++ b/filedistribution_test/.gitignore
@@ -0,0 +1,8 @@
+/boost
+/filedistribution
+/libtorrent
+/zookeeper_c_client_dev
+pom.xml.build
+/target
+/appdir
+/dbdir
diff --git a/filedistribution_test/OWNERS b/filedistribution_test/OWNERS
new file mode 100644
index 00000000000..31af040f698
--- /dev/null
+++ b/filedistribution_test/OWNERS
@@ -0,0 +1 @@
+bratseth
diff --git a/filedistribution_test/pom.xml b/filedistribution_test/pom.xml
new file mode 100644
index 00000000000..db6584b24b5
--- /dev/null
+++ b/filedistribution_test/pom.xml
@@ -0,0 +1,43 @@
+<!-- Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>com.yahoo.vespa</groupId>
+ <artifactId>parent</artifactId>
+ <version>6-SNAPSHOT</version>
+ <relativePath>../parent/pom.xml</relativePath>
+ </parent>
+ <artifactId>filedistribution_test</artifactId>
+ <version>6-SNAPSHOT</version>
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.yahoo.vespa</groupId>
+ <artifactId>filedistributionmanager</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <argLine>-Djava.library.path=${libraryPath}</argLine>
+ <environmentVariables>
+ <LD_LIBRARY_PATH>${libraryPath}</LD_LIBRARY_PATH>
+ </environmentVariables>
+ <skipTests>false</skipTests>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <properties>
+ <libraryPath>boost/lib64:libtorrent/lib:zookeeper_c_client_dev/lib64:filedistribution/lib</libraryPath>
+ </properties>
+</project>
diff --git a/filedistribution_test/src/test/java/FileDistributionManagerTestCase.java b/filedistribution_test/src/test/java/FileDistributionManagerTestCase.java
new file mode 100644
index 00000000000..6e2236ad235
--- /dev/null
+++ b/filedistribution_test/src/test/java/FileDistributionManagerTestCase.java
@@ -0,0 +1,120 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+import com.yahoo.vespa.filedistribution.FileDistributionManager;
+
+import static org.junit.Assume.assumeTrue;
+import org.junit.*;
+import org.junit.rules.TemporaryFolder;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.FileWriter;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+
+
+/**
+ * @author tonytv
+ */
+public class FileDistributionManagerTestCase {
+ @Rule
+ public TemporaryFolder folder = new TemporaryFolder();
+
+ File testDir;
+ File dbDir = new File(testDir, "dbdir");
+ File appDir = new File(testDir, "appdir");
+ File components = new File(appDir, "components");
+ FileDistributionManager manager;
+ MockLock lock;
+
+ private String addFile(String name, String contents) throws IOException {
+ File file = new File(components, name);
+ FileWriter writer = new FileWriter(file, false);
+ writer.write(contents);
+ writer.close();
+
+ return manager.addFile("components/" + name);
+ }
+
+ @Before
+ public void before() throws IOException {
+ assumeTrue(FileDistributionManager.isAvailable());
+
+ System.out.println(System.getProperty("java.library.path"));
+ testDir = folder.newFolder("filedistributionmanagertest" + System.currentTimeMillis());
+ dbDir.mkdir();
+ appDir.mkdir();
+ components.mkdir();
+ lock = new MockLock();
+
+ manager = new FileDistributionManager(dbDir, appDir, "mockfiledistributionmodel.testing", "foo", lock);
+ }
+
+ @After
+ public void after() {
+ if (manager != null)
+ manager.shutdown();
+ }
+
+ @Test
+ public void addFiles() throws IOException {
+ final String commonContent = "content";
+ final String name1 = "searcher1", name2 = "searcher2", name3 = "searcher3";
+
+ String hash1 = addFile(name1, commonContent);
+ String hash2 = addFile(name2, commonContent);
+ String hash3 = addFile(name3, "different content");
+ assertThat(lock.numAcquire, is(3));
+ assertThat(lock.numRelease, is(3));
+
+ assertNotSame(hash1, hash2);
+ assertNotSame(hash1, hash3);
+ assertNotSame(hash2, hash3);
+
+ assertTrue(hash1.length() == 40);
+
+ assertFileExists(name1, hash1);
+ assertFileExists(name2, hash2);
+ assertFileExists(name3, hash3);
+ }
+
+ private void assertFileExists(String name, String hash) {
+ File destinationDir = new File(dbDir, hash + ".new");
+ assertTrue(destinationDir.exists());
+
+ File file = new File(destinationDir, name);
+ assertTrue(file.exists());
+ }
+
+ private class MockLock implements Lock {
+ int numAcquire = 0;
+ int numRelease = 0;
+
+ public void lock() {
+ numAcquire++;
+
+ }
+
+ public void lockInterruptibly() throws InterruptedException {
+ }
+
+ public boolean tryLock() {
+ return false;
+ }
+
+ public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
+ return false;
+ }
+
+ public void unlock() {
+ numRelease++;
+ }
+
+ public Condition newCondition() {
+ return null;
+ }
+ }
+}