2 * Copyright 2008-2012 LinkedIn, Inc
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
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,
45 public void testStartVoldemortCluster() throws IOException
{
47 VoldemortServer
[] servers
= new VoldemortServer
[numServers
];
48 int partitionMap
[][] = { { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 } };
49 Cluster cluster
= ServerTestUtils
.startVoldemortCluster(numServers
,
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
70 public void stressTestStartVoldemortCluster() throws IOException
{
71 for(int i
= 0; i
< 10; i
++) {
72 testStartVoldemortCluster();
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 },
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()
97 for(VoldemortServer server
: servers
) {
98 ServerTestUtils
.stopVoldemortServer(server
);
103 public void startMultipleVoldemortServersUnsafe5() throws IOException
{
104 for(int i
= 0; i
< 5; i
++) {
105 startMultipleVoldemortServers();
111 public void startMultipleVoldemortServers10() {
112 for(int i
= 0; i
< 10; i
++) {
113 boolean started
= false;
114 boolean caught
= false;
117 startMultipleVoldemortServers();
119 } catch(IOException ioe
) {
120 System
.err
.println("CAUGHT BIND ERROR! Trying again...");
121 ioe
.printStackTrace();
130 public void testFindFreePort() throws Exception
{
131 ServerSocketChannel serverSocketChannel
;
133 serverSocketChannel
= ServerSocketChannel
.open();
134 } catch(IOException ioe
) {
135 ioe
.printStackTrace();
140 int port
= ServerTestUtils
.findFreePort();
143 serverSocketChannel
.socket().bind(new InetSocketAddress(port
));
144 } catch(IOException ioe
) {
145 ioe
.printStackTrace();
154 public void testFindFreePort1000() throws Exception
{
155 for(int i
= 0; i
< 1000; i
++) {
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;
170 serverSocketChannel
[i
] = ServerSocketChannel
.open();
171 serverSocketChannel
[i
].socket().bind(new InetSocketAddress(ports
[i
]));
172 serverSocketChannel
[i
].socket().setReuseAddress(true);
174 } catch(IOException ioe
) {
175 System
.err
.println("Attempt: " + i
+ ", port: " + ports
[i
]);
176 ioe
.printStackTrace();
182 for(int i
= 0; i
< numPorts
; i
++) {
184 serverSocketChannel
[i
].socket().close();
185 } catch(IOException ioe
) {
186 System
.err
.println("Attempt: " + i
+ ", port: " + ports
[i
]);
187 ioe
.printStackTrace();
197 public void testFindFreePorts100() throws Exception
{
198 for(int i
= 0; i
< 100; i
++) {
199 System
.out
.println("testFindFreePorts100: " + i
);
204 // * END : "commented out" tests
205 // **********************************************************************