1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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;
}
}
}
|