Skip to content

Commit

Permalink
Moved the "sleep" util from UTILS to U.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jul 12, 2015
1 parent 3162536 commit 653292c
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 51 deletions.
5 changes: 2 additions & 3 deletions rapidoid-app/src/main/java/org/rapidoid/app/Apps.java
Expand Up @@ -49,7 +49,6 @@
import org.rapidoid.plugins.users.UsersPlugin;
import org.rapidoid.scan.Scan;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;
import org.rapidoid.util.Usage;

@Authors("Nikolche Mihajlovski")
Expand Down Expand Up @@ -174,7 +173,7 @@ public static void terminate(final int afterSeconds) {
new Thread() {
@Override
public void run() {
UTILS.sleep(afterSeconds * 1000);
U.sleep(afterSeconds * 1000);
terminate();
}
}.start();
Expand All @@ -187,7 +186,7 @@ public static void terminateIfIdleFor(final int idleSeconds) {
@Override
public void run() {
while (!Thread.interrupted()) {
UTILS.sleep(500);
U.sleep(500);
long lastUsed = Usage.getLastAppUsedOn();
long idleSec = (U.time() - lastUsed) / 1000;
if (idleSec >= idleSeconds) {
Expand Down
Expand Up @@ -106,13 +106,13 @@ public Object handle(HttpExchange x) {
protected void start() {
server.start();

UTILS.sleep(300);
U.sleep(300);
System.out.println("----------------------------------------");
}

protected void shutdown() {
server.shutdown();
UTILS.sleep(300);
U.sleep(300);
System.out.println("--- SERVER STOPPED ---");
}

Expand Down
Expand Up @@ -25,7 +25,6 @@
import org.rapidoid.annotation.Since;
import org.rapidoid.log.Log;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;

@Authors("Nikolche Mihajlovski")
@Since("2.0.0")
Expand Down Expand Up @@ -140,7 +139,7 @@ public T start() {
public void waitToStart() {
// wait for the event loop to activate
while (status == LoopStatus.INIT || status == LoopStatus.BEFORE_LOOP) {
UTILS.sleep(50);
U.sleep(50);
}
}

Expand All @@ -157,7 +156,7 @@ public T shutdown() {
public void waitToStop() {
// wait for the event loop to stop
while (status != LoopStatus.STOPPED && status != LoopStatus.FAILED) {
UTILS.sleep(50);
U.sleep(50);
}
}

Expand Down
Expand Up @@ -25,7 +25,7 @@
import org.rapidoid.annotation.Since;
import org.rapidoid.cls.Cls;
import org.rapidoid.net.Protocol;
import org.rapidoid.util.UTILS;
import org.rapidoid.util.U;
import org.rapidoid.wire.Wire;

@Authors("Nikolche Mihajlovski")
Expand Down Expand Up @@ -71,7 +71,7 @@ public void run() {

public RapidoidWorker getWorker() {
while (worker == null) {
UTILS.sleep(50);
U.sleep(50);
}

return worker;
Expand Down
6 changes: 3 additions & 3 deletions rapidoid-net/src/test/java/org/rapidoid/NetTestCommons.java
Expand Up @@ -27,7 +27,7 @@
import org.rapidoid.net.Serve;
import org.rapidoid.net.TCPServer;
import org.rapidoid.test.TestCommons;
import org.rapidoid.util.UTILS;
import org.rapidoid.util.U;

@Authors("Nikolche Mihajlovski")
@Since("2.0.0")
Expand All @@ -36,14 +36,14 @@ public abstract class NetTestCommons extends TestCommons {
protected void server(Protocol protocol, Runnable client) {
TCPServer server = Serve.listen(protocol);

UTILS.sleep(300);
U.sleep(300);
System.out.println("----------------------------------------");

try {
client.run();
} finally {
server.shutdown();
UTILS.sleep(300);
U.sleep(300);
System.out.println("--- SERVER STOPPED ---");
}

Expand Down
Expand Up @@ -20,11 +20,11 @@
* #L%
*/

import org.junit.Test;
import org.rapidoid.NetTestCommons;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.mime.MediaType;
import org.junit.Test;

@Authors("Nikolche Mihajlovski")
@Since("2.0.0")
Expand Down
16 changes: 16 additions & 0 deletions rapidoid-u/src/main/java/org/rapidoid/util/U.java
Expand Up @@ -784,4 +784,20 @@ public static void validateArg(String argumentName, boolean isValid) {
}
}

/**
* Sleeps (calling Thread.sleep) for the specified period.
*
* If the thread is interrupted while sleeping, throws {@link ThreadDeath} to stop the thread.
*
* @param millis
* the length of time to sleep in milliseconds.
*/
public static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
throw new ThreadDeath();
}
}

}
Expand Up @@ -23,7 +23,6 @@
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;

@Authors("Nikolche Mihajlovski")
@Since("3.0.0")
Expand Down Expand Up @@ -56,7 +55,7 @@ public void onDone(T result, Throwable error) {

public T get() {
while (!done) {
UTILS.sleep(10);
U.sleep(10);
}

if (error != null) {
Expand Down
Expand Up @@ -39,7 +39,7 @@ public void run() {
Log.info("Starting stats thread...");

while (!Thread.interrupted()) {
UTILS.sleep(1000);
U.sleep(1000);
String stats = UTILS.getCpuMemStats();
if (!stats.equals(lastStats)) {
System.out.println(stats);
Expand Down
8 changes: 0 additions & 8 deletions rapidoid-utils/src/main/java/org/rapidoid/util/UTILS.java
Expand Up @@ -627,14 +627,6 @@ public static void endMeasure(String info) {
D.print(info + ": " + delta + " ms");
}

public static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
throw new ThreadDeath();
}
}

public static Throwable rootCause(Throwable e) {
while (e.getCause() != null) {
e = e.getCause();
Expand Down
Expand Up @@ -20,10 +20,10 @@
* #L%
*/

import org.junit.Test;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.test.TestCommons;
import org.junit.Test;

@Authors("Nikolche Mihajlovski")
@Since("2.0.0")
Expand Down
Expand Up @@ -21,6 +21,7 @@
* #L%
*/

import org.junit.Test;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.config.Conf;
Expand All @@ -30,7 +31,7 @@
import org.rapidoid.http.HttpExchange;
import org.rapidoid.test.TestCommons;
import org.rapidoid.util.D;
import org.rapidoid.util.UTILS;
import org.rapidoid.util.U;
import org.rapidoid.wrap.BoolWrap;
import org.rapidoid.wrap.IntWrap;
import org.rapidoidx.bytes.BytesUtil;
Expand All @@ -39,7 +40,6 @@
import org.rapidoidx.net.TCP;
import org.rapidoidx.net.abstracts.Channel;
import org.rapidoidx.net.impl.FiniteStateProtocol;
import org.junit.Test;

@Authors("Nikolche Mihajlovski")
@Since("3.0.0")
Expand Down Expand Up @@ -106,7 +106,7 @@ protected int state1(Channel ctx) {
}).build().start();

int sec = 5;
UTILS.sleep(sec * 1000);
U.sleep(sec * 1000);

server.shutdown();

Expand Down
Expand Up @@ -26,7 +26,6 @@
import org.rapidoid.annotation.Since;
import org.rapidoid.log.Log;
import org.rapidoid.util.U;
import org.rapidoid.util.UTILS;

@Authors("Nikolche Mihajlovski")
@Since("3.0.0")
Expand Down Expand Up @@ -141,7 +140,7 @@ public T start() {
public void waitToStart() {
// wait for the event loop to activate
while (status == LoopStatus.INIT || status == LoopStatus.BEFORE_LOOP) {
UTILS.sleep(50);
U.sleep(50);
}
}

Expand All @@ -158,7 +157,7 @@ public T shutdown() {
public void waitToStop() {
// wait for the event loop to stop
while (status != LoopStatus.STOPPED && status != LoopStatus.FAILED) {
UTILS.sleep(50);
U.sleep(50);
}
}

Expand Down
Expand Up @@ -25,7 +25,7 @@
import org.rapidoid.annotation.Since;
import org.rapidoid.log.Log;
import org.rapidoid.test.TestCommons;
import org.rapidoid.util.UTILS;
import org.rapidoid.util.U;
import org.rapidoidx.net.Protocol;
import org.rapidoidx.net.TCP;
import org.rapidoidx.net.TCPServer;
Expand All @@ -37,14 +37,14 @@ public abstract class NetTestCommons extends TestCommons {
protected void server(Protocol protocol, Runnable client) {
TCPServer server = TCP.listen(protocol);

UTILS.sleep(300);
U.sleep(300);
System.out.println("----------------------------------------");

try {
client.run();
} finally {
server.shutdown();
UTILS.sleep(300);
U.sleep(300);
System.out.println("--- SERVER STOPPED ---");
}

Expand Down
16 changes: 8 additions & 8 deletions rapidoid-x-net/src/test/java/org/rapidoidx/TcpClientTest.java
Expand Up @@ -21,17 +21,17 @@
* #L%
*/

import org.junit.Test;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.log.Log;
import org.rapidoid.log.LogLevel;
import org.rapidoid.util.UTILS;
import org.rapidoid.util.U;
import org.rapidoidx.net.Protocol;
import org.rapidoidx.net.TCP;
import org.rapidoidx.net.TCPClient;
import org.rapidoidx.net.TCPServer;
import org.rapidoidx.net.abstracts.Channel;
import org.junit.Test;

@Authors("Nikolche Mihajlovski")
@Since("3.0.0")
Expand Down Expand Up @@ -66,12 +66,12 @@ public void testTCPClientWithDefaultConnections() {
TCPClient client = TCP.client().host("localhost").port(8080).connections(5).protocol(HI_CLIENT).build().start();

// let the clients wait
UTILS.sleep(3000);
U.sleep(3000);

TCPServer server = TCP.server().port(8080).protocol(UPPERCASE_SERVER).build().start();

// let the server serve the clients
UTILS.sleep(3000);
U.sleep(3000);

eq(client.info().messagesProcessed(), 5);
eq(server.info().messagesProcessed(), 5);
Expand All @@ -88,12 +88,12 @@ public void testTCPClientWithCustomConnections() {
client.connect("localhost", 8080, HI_CLIENT, 10, false, null);

// let the clients wait
UTILS.sleep(3000);
U.sleep(3000);

TCPServer server = TCP.server().port(8080).protocol(UPPERCASE_SERVER).build().start();

// let the server serve the clients
UTILS.sleep(3000);
U.sleep(3000);

eq(client.info().messagesProcessed(), 10);
eq(server.info().messagesProcessed(), 10);
Expand All @@ -110,13 +110,13 @@ public void testTCPClientWithDefaultAndCustomConnections() {
client.connect("localhost", 9090, HI_CLIENT, 2, false, null);

// let the clients wait
UTILS.sleep(3000);
U.sleep(3000);

TCPServer server1 = TCP.server().port(8080).protocol(UPPERCASE_SERVER).build().start();
TCPServer server2 = TCP.server().port(9090).protocol(UPPERCASE_SERVER).build().start();

// let the servers serve the clients
UTILS.sleep(3000);
U.sleep(3000);

eq(client.info().messagesProcessed(), 5);
eq(server1.info().messagesProcessed(), 3);
Expand Down
Expand Up @@ -21,11 +21,11 @@
* #L%
*/

import org.junit.Test;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.mime.MediaType;
import org.rapidoidx.NetTestCommons;
import org.junit.Test;

@Authors("Nikolche Mihajlovski")
@Since("3.0.0")
Expand Down
Expand Up @@ -25,7 +25,7 @@

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.util.UTILS;
import org.rapidoid.util.U;

@Authors("Nikolche Mihajlovski")
@Since("3.0.0")
Expand All @@ -52,15 +52,15 @@ public T take() {
T item;

while ((item = queue.poll()) == null) {
UTILS.sleep(100);
U.sleep(100);
}

return item;
}

public void put(T item) {
while (!queue.offer(item)) {
UTILS.sleep(100);
U.sleep(100);
}
}

Expand Down

0 comments on commit 653292c

Please sign in to comment.