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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.filedistribution;
import com.yahoo.cloud.config.ConfigserverConfig;
import com.yahoo.io.IOUtils;
import com.yahoo.net.HostName;
import com.yahoo.vespa.filedistribution.FileReferenceData;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public class FileServerTest {
private FileServer fileServer;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Before
public void setup() throws IOException {
File rootDir = new File(temporaryFolder.newFolder("fileserver-root").getAbsolutePath());
fileServer = new FileServer(rootDir);
}
@Test
public void requireThatExistingFileCanBeFound() throws IOException {
File dir = getFileServerRootDir();
IOUtils.createDirectory(dir + "/123");
IOUtils.writeFile(dir + "/123/f1", "test", false);
assertTrue(fileServer.hasFile("123"));
}
@Test
public void requireThatNonExistingFileCanNotBeFound() {
assertFalse(fileServer.hasFile("12x"));
}
@Test
public void requireThatFileReferenceWithDirectoryCanBeFound() throws IOException {
File dir = getFileServerRootDir();
IOUtils.writeFile(dir + "/124/subdir/f1", "test", false);
IOUtils.writeFile(dir + "/124/subdir/f2", "test", false);
assertTrue(fileServer.hasFile("124/subdir"));
}
@Test
public void requireThatWeCanReplayFile() throws IOException, InterruptedException, ExecutionException {
File dir = getFileServerRootDir();
IOUtils.writeFile(dir + "/12y/f1", "dummy-data", true);
CompletableFuture<byte []> content = new CompletableFuture<>();
fileServer.startFileServing("12y", new FileReceiver(content));
assertEquals(new String(content.get()), "dummy-data");
}
@Test
public void requireThatDifferentNumberOfConfigServersWork() throws IOException {
// Empty connection pool in tests etc.
ConfigserverConfig.Builder builder = new ConfigserverConfig.Builder()
.configServerDBDir(temporaryFolder.newFolder("serverdb").getAbsolutePath())
.configDefinitionsDir(temporaryFolder.newFolder("configdefinitions").getAbsolutePath());
FileServer fileServer = createFileServer(builder);
assertEquals(0, fileServer.downloader().fileReferenceDownloader().connectionPool().getSize());
// Empty connection pool when only one server, no use in downloading from yourself
List<ConfigserverConfig.Zookeeperserver.Builder> servers = new ArrayList<>();
ConfigserverConfig.Zookeeperserver.Builder serverBuilder = new ConfigserverConfig.Zookeeperserver.Builder();
serverBuilder.hostname(HostName.getLocalhost());
serverBuilder.port(123456);
servers.add(serverBuilder);
builder.zookeeperserver(servers);
fileServer = createFileServer(builder);
assertEquals(0, fileServer.downloader().fileReferenceDownloader().connectionPool().getSize());
// connection pool of size 1 when 2 servers
ConfigserverConfig.Zookeeperserver.Builder serverBuilder2 = new ConfigserverConfig.Zookeeperserver.Builder();
serverBuilder2.hostname("bar");
serverBuilder2.port(123456);
servers.add(serverBuilder2);
builder.zookeeperserver(servers);
fileServer = createFileServer(builder);
assertEquals(1, fileServer.downloader().fileReferenceDownloader().connectionPool().getSize());
}
private FileServer createFileServer(ConfigserverConfig.Builder configBuilder) throws IOException {
File fileReferencesDir = temporaryFolder.newFolder();
configBuilder.fileReferencesDir(fileReferencesDir.getAbsolutePath());
return new FileServer(new ConfigserverConfig(configBuilder));
}
private static class FileReceiver implements FileServer.Receiver {
CompletableFuture<byte []> content;
FileReceiver(CompletableFuture<byte []> content) {
this.content = content;
}
@Override
public void receive(FileReferenceData fileData, FileServer.ReplayStatus status) {
this.content.complete(fileData.content().array());
}
}
private File getFileServerRootDir() {
return fileServer.getRootDir().getRoot();
}
}
|