public static int[][] generatePorts(int numServers) { int[][] ports = new int[numServers][]; for (int i = 0; i < numServers; i++) { ports[i] = new int[3]; for (int j = 0; j < 3; j++) { ports[i][j] = PortAssignment.unique(); } } return ports; }
private String generateQuorumConfiguration() { int portQp1 = PortAssignment.unique(); int portQp2 = PortAssignment.unique(); int portQp3 = PortAssignment.unique(); int portLe1 = PortAssignment.unique(); int portLe2 = PortAssignment.unique(); int portLe3 = PortAssignment.unique(); return "server.1=127.0.0.1:" + (portQp1) + ":" + (portLe1) + ";" + clientPortQp1 + "\n" + "server.2=127.0.0.1:" + (portQp2) + ":" + (portLe2) + ";" + clientPortQp2 + "\n" + "server.3=127.0.0.1:" + (portQp3) + ":" + (portLe3) + ";" + clientPortQp3; }
/** * Create config strings that will be used for * the test servers. */ private ArrayList<String> buildServerStrings() { ArrayList<String> serverStrings = new ArrayList<String>(); for(int i = 0; i < NUM_SERVERS; i++) { clientPorts[i] = PortAssignment.unique(); String server = "server." + i + "=localhost:" + PortAssignment.unique() +":"+PortAssignment.unique() + ":participant;" + "localhost:" + clientPorts[i]; serverStrings.add(server); } return serverStrings; }
private String generateNewQuorumConfig(int serverCount) { StringBuilder sb = new StringBuilder(); String server; for (int i = 0; i < serverCount; i++) { clientPorts.put(i, PortAssignment.unique()); server = "server." + i + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant;localhost:" + clientPorts.get(i); serverAddress.put(i, server); sb.append(server + "\n"); } return sb.toString(); }
private String updateExistingQuorumConfig(List<Integer> sidsToAdd, List<Integer> sidsToRemove) { StringBuilder sb = new StringBuilder(); for (Integer sid : sidsToAdd) { clientPorts.put(sid, PortAssignment.unique()); serverAddress.put(sid, "server." + sid + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant;localhost:" + clientPorts.get(sid)); } for (Integer sid : sidsToRemove) { clientPorts.remove(sid); serverAddress.remove(sid); } for (String server : serverAddress.values()) { sb.append(server + "\n"); } return sb.toString(); }
@Test public void testUnspecifiedClientAddress() throws Exception { int[] ports = { PortAssignment.unique(), PortAssignment.unique(), PortAssignment.unique() }; String server = "server.0=localhost:" + ports[0] + ":" + ports[1] + ";" + ports[2]; QuorumServer qs = new QuorumServer(0, server); Assert.assertEquals(qs.clientAddr.getHostString(), "0.0.0.0"); Assert.assertEquals(qs.clientAddr.getPort(), ports[2]); }
/** * This test creates a dynamic config of new format. * The dynamic config is written in static config file. * It checks that the client port will be read from the dynamic config. */ @Test public void testClientPortInStaticFile() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); String quorumCfgSection = "server.1=127.0.0.1:" + (PortAssignment.unique()) + ":" + (PortAssignment.unique()) + ":participant;" + CLIENT_PORT + "\n"; MainThread mt = new MainThread(1, quorumCfgSection, false); verifyStandalone(mt, CLIENT_PORT); }
@Before public void setUp() throws Exception { File tmpdir = ClientBase.createTmpDir(); Map<Long, QuorumServer> peers = new HashMap<Long,QuorumServer>(); for(int i = 0; i < 5; i++) { peers.put(Long.valueOf(i), new QuorumServer(Long.valueOf(i), new InetSocketAddress("127.0.0.1", PortAssignment.unique()))); } QuorumPeer peer = new QuorumPeer(peers, tmpdir, tmpdir, PortAssignment.unique(), 3, 3, 1000, 2, 2); fle = new FastLeaderElection(peer, peer.createCnxnManager()); }
/** * Ensure observer cannot start by itself **/ @Test public void startObserver() throws Exception { int clientPort = PortAssignment.unique(); String config = "server." + observer1 + "=localhost:"+ PortAssignment.unique() + ":" + clientPort + ":observer;" + "localhost:" + PortAssignment.unique(); MainThread observer = new MainThread(observer1, clientPort, config); observer.start(); Assert.assertFalse("Observer was able to start by itself!", ClientBase.waitForServerUp("127.0.0.1:" + clientPort, CONNECTION_TIMEOUT)); } }
@Before public void setUp() throws Exception { localServerAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), PortAssignment.unique()); x509Util = new ClientX509Util(); x509TestContext.setSystemProperties(x509Util, KeyStoreFileType.JKS, KeyStoreFileType.JKS); }
@Before public void setUp() throws IOException { listenAddress = new InetSocketAddress(PortAssignment.unique()); factory = new NIOServerCnxnFactory(); factory.configure(listenAddress, 100); }
private boolean reconfigPort() throws KeeperException, InterruptedException { List<String> joiningServers = new ArrayList<String>(); int leaderId = 1; while (qu.getPeer(leaderId).peer.leader == null) leaderId++; int followerId = leaderId == 1 ? 2 : 1; joiningServers.add("server." + followerId + "=localhost:" + qu.getPeer(followerId).peer.getQuorumAddress().getPort() /*quorum port*/ + ":" + qu.getPeer(followerId).peer.getElectionAddress().getPort() /*election port*/ + ":participant;localhost:" + PortAssignment.unique()/* new client port */); zkAdmin.reconfigure(joiningServers, null, null, -1, new Stat()); return true; } }
private boolean reconfigPort() throws KeeperException, InterruptedException { List<String> joiningServers = new ArrayList<String>(); int leaderId = 1; while (qu.getPeer(leaderId).peer.leader == null) leaderId++; int followerId = leaderId == 1 ? 2 : 1; joiningServers.add("server." + followerId + "=localhost:" + qu.getPeer(followerId).peer.getQuorumAddress().getPort() /*quorum port*/ + ":" + qu.getPeer(followerId).peer.getElectionAddress().getPort() /*election port*/ + ":participant;localhost:" + PortAssignment.unique()/* new client port */); zkAdmin.reconfigure(joiningServers, null, null, -1, new Stat()); return true; } }
/** * This test ensures that an Observer does not elect itself as a leader, or * indeed come up properly, if it is the lone member of an ensemble. * @throws Exception */ @Test public void testObserverOnly() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT_QP1 = PortAssignment.unique(); String quorumCfgSection = "server.1=127.0.0.1:" + (PortAssignment.unique()) + ":" + (PortAssignment.unique()) + ":observer;" + CLIENT_PORT_QP1 + "\n"; MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); q1.start(); q1.join(ClientBase.CONNECTION_TIMEOUT); Assert.assertFalse(q1.isAlive()); }
/** * Test verifies that the server shouldn't boot with a faulty metrics provider */ @Test public void testFaultyMetricsProviderOnStart() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); final String configs = "metricsProvider.className="+MetricsProviderWithErrorInStart.class.getName()+"\n"; MainThread main = new MainThread(CLIENT_PORT, true, configs); String args[] = new String[1]; args[0] = main.confFile.toString(); try { main.main.initializeAndRun(args); Assert.fail("Must throw exception as metrics provider cannot boot"); } catch (IOException iae) { // expected } }
/** * This test wouldn't create any dynamic config. * However, it adds a "clientPort=XXX" in static config file. * It checks the standard way of standalone mode. */ @Test public void testNoDynamicConfig() throws Exception { ClientBase.setupTestEnv(); final int CLIENT_PORT = PortAssignment.unique(); MainThread mt = new MainThread( MainThread.UNSET_MYID, CLIENT_PORT, "", false); verifyStandalone(mt, CLIENT_PORT); }
private ServerCnxnFactory startServer(File tmpDir) throws IOException, InterruptedException { final int CLIENT_PORT = PortAssignment.unique(); ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); ServerCnxnFactory f = ServerCnxnFactory.createFactory(CLIENT_PORT, -1); f.startup(zks); Assert.assertNotNull("JMX initialization failed!", zks.jmxServerBean); Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT)); return f; }
@Test(timeout = 5000) public void testCreateSSLServerSocketWithPort() throws Exception { int port = PortAssignment.unique(); setCustomCipherSuites(); SSLServerSocket sslServerSocket = x509Util.createSSLServerSocket(port); Assert.assertEquals(sslServerSocket.getLocalPort(), port); Assert.assertArrayEquals(customCipherSuites, sslServerSocket.getEnabledCipherSuites()); Assert.assertTrue(sslServerSocket.getNeedClientAuth()); }