Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Test utils created to help on implementation of DRY phylosophy

  • Loading branch information...
commit 0ce94f49d360263957428b1df56518d45ad15ed7 1 parent 1952241
Francisco Perez-Sorrosal authored
View
45 src/test/java/com/yahoo/omid/OmidTestBase.java
@@ -19,8 +19,6 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
-import java.net.Socket;
-import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -106,7 +104,7 @@ public void run() {
};
tsoExecutor.execute(tsoTask);
- waitForSocketListening("localhost", 1234);
+ TestUtils.waitForSocketListening("localhost", 1234, 1000);
// HBase setup
hbaseConf = HBaseConfiguration.create();
@@ -149,7 +147,7 @@ public static void teardownOmid() throws Exception {
tsoExecutor.shutdownNow();
bkExecutor.shutdownNow();
- waitForSocketNotListening("localhost", 1234);
+ TestUtils.waitForSocketNotListening("localhost", 1234, 1000);
}
@Before
@@ -187,45 +185,6 @@ public void tearDown() {
}
}
- private static void waitForSocketListening(String host, int port)
- throws UnknownHostException, IOException, InterruptedException {
- while (true) {
- Socket sock = null;
- try {
- sock = new Socket(host, port);
- } catch (IOException e) {
- // ignore as this is expected
- Thread.sleep(1000);
- continue;
- } finally {
- if (sock != null) {
- sock.close();
- }
- }
- LOG.info(host + ":" + port + " is UP");
- break;
- }
- }
-
- private static void waitForSocketNotListening(String host, int port)
- throws UnknownHostException, IOException, InterruptedException {
- while (true) {
- Socket sock = null;
- try {
- sock = new Socket(host, port);
- } catch (IOException e) {
- // ignore as this is expected
- break;
- } finally {
- if (sock != null) {
- sock.close();
- }
- }
- Thread.sleep(1000);
- LOG.info(host + ":" + port + " is still up");
- }
- }
-
protected static void dumpTable(String table) throws Exception {
HTable t = new HTable(hbaseConf, table);
if (LOG.isTraceEnabled()) {
View
77 src/test/java/com/yahoo/omid/TestUtils.java
@@ -0,0 +1,77 @@
+/**
+ * Copyright (c) 2011 Yahoo! Inc. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License. See accompanying LICENSE file.
+ */
+
+package com.yahoo.omid;
+
+import java.io.IOException;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ *
+ * This class contains functionality that is useful for the Omid tests.
+ *
+ * @author Francisco Perez-Sorrosal - fperez@yahoo-inc.com
+ *
+ */
+public class TestUtils {
+ private static final Log LOG = LogFactory.getLog(TestUtils.class);
+
+ public static void waitForSocketListening(String host, int port,
+ int sleepTimeMillis) throws UnknownHostException, IOException,
+ InterruptedException {
+ while (true) {
+ Socket sock = null;
+ try {
+ sock = new Socket(host, port);
+ } catch (IOException e) {
+ // ignore as this is expected
+ Thread.sleep(sleepTimeMillis);
+ continue;
+ } finally {
+ if (sock != null) {
+ sock.close();
+ }
+ }
+ LOG.info("Host " + host + ":" + port + " is up");
+ break;
+ }
+ }
+
+ public static void waitForSocketNotListening(String host, int port,
+ int sleepTimeMillis) throws UnknownHostException, IOException,
+ InterruptedException {
+ while (true) {
+ Socket sock = null;
+ try {
+ sock = new Socket(host, port);
+ } catch (IOException e) {
+ // ignore as this is expected
+ break;
+ } finally {
+ if (sock != null) {
+ sock.close();
+ }
+ }
+ Thread.sleep(sleepTimeMillis);
+ LOG.info("Host " + host + ":" + port + " is up");
+ }
+ }
+
+}
View
51 src/test/java/com/yahoo/omid/tso/TSOTestBase.java
@@ -17,8 +17,6 @@
package com.yahoo.omid.tso;
import java.io.IOException;
-import java.net.Socket;
-import java.net.UnknownHostException;
import java.util.concurrent.Executors;
import org.apache.bookkeeper.util.LocalBookKeeper;
@@ -35,6 +33,7 @@
import org.junit.Before;
import org.junit.BeforeClass;
+import com.yahoo.omid.TestUtils;
import com.yahoo.omid.tso.messages.TimestampRequest;
import com.yahoo.omid.tso.messages.TimestampResponse;
@@ -131,7 +130,7 @@ public static void teardownBookkeeper() throws Exception {
bkthread.interrupt();
bkthread.join();
}
- waitForSocketNotListening("localhost", 1234);
+ TestUtils.waitForSocketNotListening("localhost", 1234, 1000);
Thread.sleep(10);
}
@@ -153,7 +152,7 @@ public void setupTSO() throws Exception {
LOG.info("Starting TSO");
tsothread.start();
- waitForSocketListening("localhost", 1234);
+ TestUtils.waitForSocketListening("localhost", 1234, 100);
LOG.info("Finished loading TSO");
state = tso.getState();
@@ -165,8 +164,7 @@ public void setupTSO() throws Exception {
@After
public void teardownTSO() throws Exception {
-
-
+
clientHandler.sendMessage(new TimestampRequest());
while (!(clientHandler.receiveMessage() instanceof TimestampResponse))
; // Do nothing
@@ -185,7 +183,7 @@ public void teardownTSO() throws Exception {
}
teardownClient();
- waitForSocketNotListening("localhost", 1234);
+ TestUtils.waitForSocketNotListening("localhost", 1234, 1000);
Thread.sleep(10);
}
@@ -194,43 +192,4 @@ protected boolean recoveryEnabled() {
return false;
}
- private static void waitForSocketListening(String host, int port) throws UnknownHostException, IOException,
- InterruptedException {
- while (true) {
- Socket sock = null;
- try {
- sock = new Socket(host, port);
- } catch (IOException e) {
- // ignore as this is expected
- Thread.sleep(100);
- continue;
- } finally {
- if (sock != null) {
- sock.close();
- }
- }
- LOG.info(host + ":" + port + " is UP");
- break;
- }
- }
-
- private static void waitForSocketNotListening(String host, int port) throws UnknownHostException, IOException,
- InterruptedException {
- while (true) {
- Socket sock = null;
- try {
- sock = new Socket(host, port);
- } catch (IOException e) {
- // ignore as this is expected
- break;
- } finally {
- if (sock != null) {
- sock.close();
- }
- }
- Thread.sleep(1000);
- LOG.info(host + ":" + port + " is still up");
- }
- }
-
}
Please sign in to comment.
Something went wrong with that request. Please try again.