Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
Fix null result in echo request
Browse files Browse the repository at this point in the history
Issue: #20

Signed-off-by: Hechao Li <hechaol@outlook.com>
  • Loading branch information
hechaoli committed May 8, 2018
1 parent e80a988 commit a2e587b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private Object[] toMethodParams(ArrayNode paramsNode, Method method) {

Object[] actualParams = new Object[methodParamSize];
if (methodParamSize != actualParamSize) {
// This is not an error only if the last arg is an vararg
// This is not an error only if the last arg is a vararg
// And the actual params number must be >= methods params number - 1
// For e.g. if a methods has n params, the last one is an vararg,
// then the number of actual params can be [n-1, ...)
Expand All @@ -149,10 +149,8 @@ private Object[] toMethodParams(ArrayNode paramsNode, Method method) {
&& parameters[lastIndex].isVarArgs()
&& actualParamSize >= lastIndex) {

if (actualParamSize > lastIndex) {
Class<?> type = parameters[lastIndex].getType().getComponentType();
actualParams[lastIndex] = buildVarargParam(paramsNode, lastIndex, type);
}
Class<?> type = parameters[lastIndex].getType().getComponentType();
actualParams[lastIndex] = buildVarargParam(paramsNode, lastIndex, type);
// We have handled the last param, no need to handle it later
--methodParamSize;
} else {
Expand All @@ -169,11 +167,9 @@ private Object[] toMethodParams(ArrayNode paramsNode, Method method) {
actualParams[i] = JsonUtil.treeToValue(paramNode, type);
} catch (JsonProcessingException ex) {
throw new IllegalArgumentException(
"Failed to convert param " + paramNode + " to type " + type
);
"Failed to convert param " + paramNode + " to type " + type);
}
}

return actualParams;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.vmware.ovsdb.protocol.util.OvsdbConstant.STEAL;
import static com.vmware.ovsdb.protocol.util.OvsdbConstant.UNLOCK;
import static com.vmware.ovsdb.testutils.SslUtil.newSelfSignedSslContextPair;
import static com.vmware.ovsdb.testutils.TestConstants.VERIFY_TIMEOUT_MILLIS;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.fail;
import static org.junit.Assert.assertArrayEquals;
Expand Down Expand Up @@ -74,8 +75,11 @@
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -124,6 +128,7 @@ private void testAll() throws IOException, OvsdbClientException {
testConnectionInfo();
testErrorOperation();
testLock();
testEchoResponse();
}

void testTcpConnection()
Expand Down Expand Up @@ -507,8 +512,6 @@ private void testErrorOperation() throws OvsdbClientException {
}

private void testLock() throws OvsdbClientException {
int VERIFY_TIMEOUT_MILLIS = 5000;

// Get lock-1
String lockId1 = "lock-1";
LockCallback lockCallback1 = mock(LockCallback.class);
Expand Down Expand Up @@ -558,6 +561,38 @@ private void testLock() throws OvsdbClientException {
verify(lockCallback3, timeout(VERIFY_TIMEOUT_MILLIS)).stolen();
}

private void testEchoResponse() {
CompletableFuture<Void> successFuture1 = new CompletableFuture<>();
final String expectedResponse1 = "{\"result\":[]," + "\"error\":null,\"id\":\"echo\"}";
ovsdbServerEmulator.registerReadCallback(msg -> {
if (msg.equals(expectedResponse1)) {
successFuture1.complete(null);
}
});
ovsdbServerEmulator.write("{\"method\":\"echo\",\"params\":[],\"id\":\"echo\"}");
try {
successFuture1.get(VERIFY_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
fail(e.getMessage());
}

CompletableFuture<Void> successFuture2 = new CompletableFuture<>();
final String expectedResponse2 = "{\"result\":[123,\"456\",true]," + "\"error\":null,"
+ "\"id\":\"echo\"}";
ovsdbServerEmulator.registerReadCallback(msg -> {
if (msg.equals(expectedResponse2)) {
successFuture2.complete(null);
}
});
ovsdbServerEmulator.write("{\"method\":\"echo\",\"params\":[123,\"456\",true],"
+ "\"id\":\"echo\"}");
try {
successFuture2.get(VERIFY_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
fail(e.getMessage());
}
}

private void setupOvsdbEmulator(
String request, String result, String error
) {
Expand Down

0 comments on commit a2e587b

Please sign in to comment.