bind failures while running tests
[voldemort/jeffpc.git] / test / unit / voldemort / utils / ServerTestUtilsTest.java
blob2635ae8bed581da98f00402700e59b29669a0e79
1 /*
2 * Copyright 2008-2012 LinkedIn, Inc
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
17 package voldemort.utils;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
22 import java.io.IOException;
23 import java.net.InetSocketAddress;
24 import java.nio.channels.ServerSocketChannel;
25 import java.util.Properties;
27 import org.junit.Test;
29 import voldemort.ServerTestUtils;
30 import voldemort.TestUtils;
31 import voldemort.cluster.Cluster;
32 import voldemort.server.VoldemortServer;
33 import voldemort.store.socket.SocketStoreFactory;
34 import voldemort.store.socket.clientrequest.ClientRequestExecutorPool;
36 public class ServerTestUtilsTest {
38 private static String storesXmlfile = "test/common/voldemort/config/stores.xml";
39 private SocketStoreFactory socketStoreFactory = new ClientRequestExecutorPool(2,
40 10000,
41 100000,
42 32 * 1024);
44 @Test
45 public void testStartVoldemortCluster() throws IOException {
46 int numServers = 8;
47 VoldemortServer[] servers = new VoldemortServer[numServers];
48 int partitionMap[][] = { { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 } };
49 Cluster cluster = ServerTestUtils.startVoldemortCluster(numServers,
50 servers,
51 partitionMap,
52 socketStoreFactory,
53 true,
54 null,
55 storesXmlfile,
56 new Properties());
57 assertTrue(cluster != null);
60 // **********************************************************************
61 // * START : "commented out" tests
62 // These tests helped to find the root case of BindException problem when
63 // clusters were started. These tests were used in debugging and stress
64 // testing and should not be part of our general junit tests. The @Test
65 // parameter is therefore commented out. The debugging methods themselves
66 // are not commented out so that they can be kept up to date with other code
67 // changes.
69 // @Test
70 public void stressTestStartVoldemortCluster() throws IOException {
71 for(int i = 0; i < 10; i++) {
72 testStartVoldemortCluster();
76 // @Test
77 public void startMultipleVoldemortServers() throws IOException {
78 Cluster cluster = ServerTestUtils.getLocalCluster(16, new int[][] { { 0 }, { 1 }, { 2 },
79 { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 11 }, { 12 }, { 13 },
80 { 14 }, { 15 } });
82 VoldemortServer[] servers = new VoldemortServer[16];
84 for(int i = 0; i < 16; i++) {
85 servers[i] = ServerTestUtils.startVoldemortServer(socketStoreFactory,
86 ServerTestUtils.createServerConfig(true,
88 TestUtils.createTempDir()
89 .getAbsolutePath(),
90 null,
91 storesXmlfile,
92 new Properties()),
93 cluster);
95 assertTrue(true);
97 for(VoldemortServer server: servers) {
98 ServerTestUtils.stopVoldemortServer(server);
102 // @Test
103 public void startMultipleVoldemortServersUnsafe5() throws IOException {
104 for(int i = 0; i < 5; i++) {
105 startMultipleVoldemortServers();
107 assertTrue(true);
110 // @Test
111 public void startMultipleVoldemortServers10() {
112 for(int i = 0; i < 10; i++) {
113 boolean started = false;
114 boolean caught = false;
115 while(!started) {
116 try {
117 startMultipleVoldemortServers();
118 started = true;
119 } catch(IOException ioe) {
120 System.err.println("CAUGHT BIND ERROR! Trying again...");
121 ioe.printStackTrace();
122 caught = true;
125 assertFalse(caught);
129 // @Test
130 public void testFindFreePort() throws Exception {
131 ServerSocketChannel serverSocketChannel;
132 try {
133 serverSocketChannel = ServerSocketChannel.open();
134 } catch(IOException ioe) {
135 ioe.printStackTrace();
136 assertTrue(false);
137 return;
140 int port = ServerTestUtils.findFreePort();
142 try {
143 serverSocketChannel.socket().bind(new InetSocketAddress(port));
144 } catch(IOException ioe) {
145 ioe.printStackTrace();
146 assertTrue(false);
147 return;
150 assertTrue(true);
153 // @Test
154 public void testFindFreePort1000() throws Exception {
155 for(int i = 0; i < 1000; i++) {
156 testFindFreePort();
160 // @Test
161 public void testFindFreePorts() throws Exception {
162 int numPorts = 25000;
163 ServerSocketChannel serverSocketChannel[] = new ServerSocketChannel[numPorts];
164 int ports[] = ServerTestUtils.findFreePorts(numPorts);
166 for(int i = 0; i < numPorts; i++) {
167 boolean bound = false;
168 while(!bound) {
169 try {
170 serverSocketChannel[i] = ServerSocketChannel.open();
171 serverSocketChannel[i].socket().bind(new InetSocketAddress(ports[i]));
172 serverSocketChannel[i].socket().setReuseAddress(true);
173 bound = true;
174 } catch(IOException ioe) {
175 System.err.println("Attempt: " + i + ", port: " + ports[i]);
176 ioe.printStackTrace();
177 Thread.sleep(10);
182 for(int i = 0; i < numPorts; i++) {
183 try {
184 serverSocketChannel[i].socket().close();
185 } catch(IOException ioe) {
186 System.err.println("Attempt: " + i + ", port: " + ports[i]);
187 ioe.printStackTrace();
188 assertTrue(false);
189 return;
193 assertTrue(true);
196 // @Test
197 public void testFindFreePorts100() throws Exception {
198 for(int i = 0; i < 100; i++) {
199 System.out.println("testFindFreePorts100: " + i);
200 testFindFreePorts();
204 // * END : "commented out" tests
205 // **********************************************************************