Skip to content
This repository has been archived by the owner on Oct 8, 2019. It is now read-only.

Commit

Permalink
Merge pull request #144 from myui/hotfixes
Browse files Browse the repository at this point in the history
Fixed to use waitForMixed() instead of Thread.sleep()
  • Loading branch information
myui committed Nov 7, 2014
2 parents 64bfbbf + 62aa6bf commit efff05e
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/test/java/hivemall/mix/server/MixServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.annotation.Nonnegative;

import org.apache.commons.cli.CommandLine;
import org.junit.Test;
import org.junit.Assert;
import org.junit.Test;

public class MixServerTest {

Expand Down Expand Up @@ -67,7 +69,7 @@ public void testSimpleScenario() throws InterruptedException {
model.set(feature, new WeightValue(weight));
}

Thread.sleep(5 * 1000); // slight delay to wait for async callbacks
waitForMixed(model, 48000, 10000L);

int numMixed = model.getNumMixed();
//System.out.println("number of mix events: " + numMixed);
Expand Down Expand Up @@ -104,7 +106,7 @@ public void testSSL() throws InterruptedException {
model.set(feature, new WeightValue(weight));
}

Thread.sleep(5 * 1000); // slight delay to wait for async callbacks
waitForMixed(model, 48000, 10000L);

int numMixed = model.getNumMixed();
//System.out.println("number of mix events: " + numMixed);
Expand Down Expand Up @@ -161,7 +163,7 @@ private static void invokeClient(String groupId, int serverPort) throws Interrup
model.set(feature, new WeightValue(weight));
}

Thread.sleep(5 * 1000); // slight delay to wait for async callbacks
waitForMixed(model, 48000, 10000L);

int numMixed = model.getNumMixed();
//System.out.println("number of mix events: " + numMixed);
Expand Down Expand Up @@ -247,7 +249,7 @@ private static void invokeClient01(String groupId, int serverPort, boolean dense
model.set(feature, new WeightValue(weight));
}

Thread.sleep(5 * 1000); // slight delay to wait for async callbacks
waitForMixed(model, 100000, 10000L);

int numMixed = model.getNumMixed();
//System.out.println("number of mix events: " + numMixed);
Expand All @@ -272,4 +274,20 @@ private static void waitForState(MixServer server, ServerState expected)
Assert.assertEquals("MixServer state is not correct (timed out)", expected, server.getState());
}

private static void waitForMixed(PredictionModel model, @Nonnegative int minMixed, @Nonnegative long maxWaitInMillis)
throws InterruptedException {
long startTime = System.currentTimeMillis();
while(true) {
int numMixed = model.getNumMixed();
if(numMixed >= minMixed) {
break;
}
Thread.sleep(500L);
long elapsedTime = System.currentTimeMillis() - startTime;
if(elapsedTime > maxWaitInMillis) {
Assert.fail("Timeout. numMixed = " + numMixed);
}
}
}

}

0 comments on commit efff05e

Please sign in to comment.