From 62aa6bf0b6580005f38484ff804e373833f6523a Mon Sep 17 00:00:00 2001 From: Makoto YUI Date: Fri, 7 Nov 2014 17:43:20 +0900 Subject: [PATCH] Fixed to use waitForMixed() instead of Thread.sleep() --- .../hivemall/mix/server/MixServerTest.java | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/test/java/hivemall/mix/server/MixServerTest.java b/src/test/java/hivemall/mix/server/MixServerTest.java index e5610697..3409e58f 100644 --- a/src/test/java/hivemall/mix/server/MixServerTest.java +++ b/src/test/java/hivemall/mix/server/MixServerTest.java @@ -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 { @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); + } + } + } + }