|
@@ -18,6 +18,20 @@
|
|
|
|
|
|
package org.apache.hadoop.ipc;
|
|
|
|
|
|
+import static org.junit.Assert.assertEquals;
|
|
|
+import static org.junit.Assert.assertFalse;
|
|
|
+import static org.junit.Assert.assertTrue;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.InetSocketAddress;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ExecutionException;
|
|
|
+import java.util.concurrent.Future;
|
|
|
+
|
|
|
import org.apache.commons.logging.Log;
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
@@ -34,17 +48,6 @@ import org.junit.Assert;
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
-import java.net.InetSocketAddress;
|
|
|
-import java.util.*;
|
|
|
-import java.util.concurrent.ExecutionException;
|
|
|
-import java.util.concurrent.Future;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-import java.util.concurrent.TimeoutException;
|
|
|
-
|
|
|
-import static org.junit.Assert.assertEquals;
|
|
|
-import static org.junit.Assert.assertFalse;
|
|
|
-
|
|
|
public class TestAsyncIPC {
|
|
|
|
|
|
private static Configuration conf;
|
|
@@ -84,51 +87,26 @@ public class TestAsyncIPC {
|
|
|
try {
|
|
|
final long param = TestIPC.RANDOM.nextLong();
|
|
|
TestIPC.call(client, param, server, conf);
|
|
|
- Future<LongWritable> returnFuture = Client.getAsyncRpcResponse();
|
|
|
+ Future<LongWritable> returnFuture = Client.getReturnRpcResponse();
|
|
|
returnFutures.put(i, returnFuture);
|
|
|
expectedValues.put(i, param);
|
|
|
} catch (Exception e) {
|
|
|
+ LOG.fatal("Caught: " + StringUtils.stringifyException(e));
|
|
|
failed = true;
|
|
|
- throw new RuntimeException(e);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- void assertReturnValues() throws InterruptedException, ExecutionException {
|
|
|
+ public void waitForReturnValues() throws InterruptedException,
|
|
|
+ ExecutionException {
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
LongWritable value = returnFutures.get(i).get();
|
|
|
- Assert.assertEquals("call" + i + " failed.",
|
|
|
- expectedValues.get(i).longValue(), value.get());
|
|
|
- }
|
|
|
- Assert.assertFalse(failed);
|
|
|
- }
|
|
|
-
|
|
|
- void assertReturnValues(long timeout, TimeUnit unit)
|
|
|
- throws InterruptedException, ExecutionException {
|
|
|
- final boolean[] checked = new boolean[count];
|
|
|
- for(boolean done = false; !done;) {
|
|
|
- done = true;
|
|
|
- for (int i = 0; i < count; i++) {
|
|
|
- if (checked[i]) {
|
|
|
- continue;
|
|
|
- } else {
|
|
|
- done = false;
|
|
|
- }
|
|
|
-
|
|
|
- final LongWritable value;
|
|
|
- try {
|
|
|
- value = returnFutures.get(i).get(timeout, unit);
|
|
|
- } catch (TimeoutException e) {
|
|
|
- LOG.info("call" + i + " caught ", e);
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- Assert.assertEquals("call" + i + " failed.",
|
|
|
- expectedValues.get(i).longValue(), value.get());
|
|
|
- checked[i] = true;
|
|
|
+ if (expectedValues.get(i) != value.get()) {
|
|
|
+ LOG.fatal(String.format("Call-%d failed!", i));
|
|
|
+ failed = true;
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
- Assert.assertFalse(failed);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -205,7 +183,7 @@ public class TestAsyncIPC {
|
|
|
|
|
|
private void doCall(final int idx, final long param) throws IOException {
|
|
|
TestIPC.call(client, param, server, conf);
|
|
|
- Future<LongWritable> returnFuture = Client.getAsyncRpcResponse();
|
|
|
+ Future<LongWritable> returnFuture = Client.getReturnRpcResponse();
|
|
|
returnFutures.put(idx, returnFuture);
|
|
|
expectedValues.put(idx, param);
|
|
|
}
|
|
@@ -255,7 +233,10 @@ public class TestAsyncIPC {
|
|
|
}
|
|
|
for (int i = 0; i < callerCount; i++) {
|
|
|
callers[i].join();
|
|
|
- callers[i].assertReturnValues();
|
|
|
+ callers[i].waitForReturnValues();
|
|
|
+ String msg = String.format("Expected not failed for caller-%d: %s.", i,
|
|
|
+ callers[i]);
|
|
|
+ assertFalse(msg, callers[i].failed);
|
|
|
}
|
|
|
for (int i = 0; i < clientCount; i++) {
|
|
|
clients[i].stop();
|
|
@@ -277,37 +258,25 @@ public class TestAsyncIPC {
|
|
|
try {
|
|
|
AsyncCaller caller = new AsyncCaller(client, addr, callCount);
|
|
|
caller.run();
|
|
|
- caller.assertReturnValues();
|
|
|
- caller.assertReturnValues();
|
|
|
- caller.assertReturnValues();
|
|
|
- Assert.assertEquals(asyncCallCount, client.getAsyncCallCount());
|
|
|
- } finally {
|
|
|
- client.stop();
|
|
|
- server.stop();
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- @Test(timeout = 60000)
|
|
|
- public void testFutureGetWithTimeout() throws IOException,
|
|
|
- InterruptedException, ExecutionException {
|
|
|
-// GenericTestUtils.setLogLevel(AsyncGetFuture.LOG, Level.ALL);
|
|
|
- final Server server = new TestIPC.TestServer(10, true, conf);
|
|
|
- final InetSocketAddress addr = NetUtils.getConnectAddress(server);
|
|
|
- server.start();
|
|
|
+ caller.waitForReturnValues();
|
|
|
+ String msg = String.format(
|
|
|
+ "First time, expected not failed for caller: %s.", caller);
|
|
|
+ assertFalse(msg, caller.failed);
|
|
|
|
|
|
- final Client client = new Client(LongWritable.class, conf);
|
|
|
+ caller.waitForReturnValues();
|
|
|
+ assertTrue(asyncCallCount == client.getAsyncCallCount());
|
|
|
+ msg = String.format("Second time, expected not failed for caller: %s.",
|
|
|
+ caller);
|
|
|
+ assertFalse(msg, caller.failed);
|
|
|
|
|
|
- try {
|
|
|
- final AsyncCaller caller = new AsyncCaller(client, addr, 10);
|
|
|
- caller.run();
|
|
|
- caller.assertReturnValues(10, TimeUnit.MILLISECONDS);
|
|
|
+ assertTrue(asyncCallCount == client.getAsyncCallCount());
|
|
|
} finally {
|
|
|
client.stop();
|
|
|
server.stop();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
public void internalTestAsyncCallLimit(int handlerCount, boolean handlerSleep,
|
|
|
int clientCount, int callerCount, int callCount) throws IOException,
|
|
|
InterruptedException, ExecutionException {
|
|
@@ -398,7 +367,9 @@ public class TestAsyncIPC {
|
|
|
server.start();
|
|
|
final AsyncCaller caller = new AsyncCaller(client, addr, 4);
|
|
|
caller.run();
|
|
|
- caller.assertReturnValues();
|
|
|
+ caller.waitForReturnValues();
|
|
|
+ String msg = String.format("Expected not failed for caller: %s.", caller);
|
|
|
+ assertFalse(msg, caller.failed);
|
|
|
} finally {
|
|
|
client.stop();
|
|
|
server.stop();
|
|
@@ -435,7 +406,9 @@ public class TestAsyncIPC {
|
|
|
server.start();
|
|
|
final AsyncCaller caller = new AsyncCaller(client, addr, 10);
|
|
|
caller.run();
|
|
|
- caller.assertReturnValues();
|
|
|
+ caller.waitForReturnValues();
|
|
|
+ String msg = String.format("Expected not failed for caller: %s.", caller);
|
|
|
+ assertFalse(msg, caller.failed);
|
|
|
} finally {
|
|
|
client.stop();
|
|
|
server.stop();
|
|
@@ -470,7 +443,9 @@ public class TestAsyncIPC {
|
|
|
server.start();
|
|
|
final AsyncCaller caller = new AsyncCaller(client, addr, 10);
|
|
|
caller.run();
|
|
|
- caller.assertReturnValues();
|
|
|
+ caller.waitForReturnValues();
|
|
|
+ String msg = String.format("Expected not failed for caller: %s.", caller);
|
|
|
+ assertFalse(msg, caller.failed);
|
|
|
} finally {
|
|
|
client.stop();
|
|
|
server.stop();
|
|
@@ -514,7 +489,10 @@ public class TestAsyncIPC {
|
|
|
}
|
|
|
for (int i = 0; i < callerCount; ++i) {
|
|
|
callers[i].join();
|
|
|
- callers[i].assertReturnValues();
|
|
|
+ callers[i].waitForReturnValues();
|
|
|
+ String msg = String.format("Expected not failed for caller-%d: %s.", i,
|
|
|
+ callers[i]);
|
|
|
+ assertFalse(msg, callers[i].failed);
|
|
|
}
|
|
|
} finally {
|
|
|
client.stop();
|