blob: baaad164f8f8d4561d578fe0dccf6a8c1125e720 (
plain) (
blame)
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core;
import com.yahoo.net.HostName;
import org.apache.zookeeper.server.NIOServerCnxnFactory;
import org.apache.zookeeper.server.ZooKeeperServer;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.time.Duration;
/**
* This class sets up a zookeeper server, such that we can test fleetcontroller zookeeper parts without stubbing in the client.
*/
public class ZooKeeperTestServer {
private final File zooKeeperDir;
private final ZooKeeperServer server;
private static final Duration tickTime = Duration.ofMillis(2000);
private final NIOServerCnxnFactory factory;
private static final String DIR_PREFIX = "test_fltctrl_zk";
private static final String DIR_POSTFIX = "sdir";
ZooKeeperTestServer() throws IOException {
this(0);
}
private ZooKeeperTestServer(int port) throws IOException {
zooKeeperDir = getTempDir();
delete(zooKeeperDir);
if (!zooKeeperDir.mkdir()) {
throw new IllegalStateException("Failed to create directory " + zooKeeperDir);
}
zooKeeperDir.deleteOnExit();
server = new ZooKeeperServer(zooKeeperDir, zooKeeperDir, (int)tickTime.toMillis());
final int maxcc = 10000; // max number of connections from the same client
factory = new NIOServerCnxnFactory();
factory.configure(new InetSocketAddress(port), maxcc); // Use any port
try{
factory.startup(server);
} catch (InterruptedException e) {
throw new IllegalStateException("Interrupted during test startup: ", e);
}
}
static ZooKeeperTestServer createWithFixedPort(int port) throws IOException {
return new ZooKeeperTestServer(port);
}
private int getPort() {
return factory.getLocalPort();
}
String getAddress() {
return HostName.getLocalhost() + ":" + getPort();
}
public void shutdown(boolean cleanupZooKeeperDir) {
server.shutdown();
if (cleanupZooKeeperDir) {
delete(zooKeeperDir);
}
factory.shutdown();
}
private void delete(File f) {
if (f.isDirectory()) {
for (File file : f.listFiles()) {
delete(file);
}
}
f.delete();
}
private static File getTempDir() throws IOException {
// The pom file sets java.io.tmpdir to ${project.build.directory}. This doesn't happen within (e.g.) IntelliJ, but happens
// on Screwdriver (tm). So if we're running tests on Screwdriver (tm), put the log in 'surefire-reports' instead so the
// user can find them along with the other test reports.
final File surefireReportsDir = new File(System.getProperty("java.io.tmpdir") + File.separator + "surefire-reports");
if (surefireReportsDir.isDirectory()) {
return File.createTempFile(DIR_PREFIX, DIR_POSTFIX, surefireReportsDir);
}
return File.createTempFile(DIR_PREFIX, DIR_POSTFIX);
}
}
|