forked from apache/incubator-seata
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix apache#195 add message unit test
- Loading branch information
1 parent
c580a2c
commit 57c5247
Showing
2 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
core/src/test/java/com/alibaba/fescar/core/protocol/MessageFutureTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.fescar.core.protocol; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.concurrent.*; | ||
|
||
/** | ||
* Created by guoyao on 2019/3/1. | ||
*/ | ||
public class MessageFutureTest { | ||
|
||
private final boolean ASYNC_FIELD=false; | ||
private final String BODY_FIELD="test_body"; | ||
private final boolean HEART_BEAT_FIELD=true; | ||
private final boolean REQUEST_FIELD=false; | ||
private final long ID_FIELD = 100L; | ||
private final long TIME_OUT_FIELD = 100L; | ||
|
||
@Test | ||
public void testFieldSetGet() { | ||
String fromJson="{\n" + | ||
"\t\"requestMessage\":{\n" + | ||
"\t\t\"async\":"+ASYNC_FIELD+",\n" + | ||
"\t\t\"body\":\""+BODY_FIELD+"\",\n" + | ||
"\t\t\"heartbeat\":"+HEART_BEAT_FIELD+",\n" + | ||
"\t\t\"id\":"+ID_FIELD+",\n" + | ||
"\t\t\"request\":"+REQUEST_FIELD+"\n" + | ||
"\t},\n" + | ||
"\t\"timeout\":"+TIME_OUT_FIELD+"\n" + | ||
"}"; | ||
MessageFuture fromJsonFuture=JSON.parseObject(fromJson, MessageFuture.class); | ||
Assert.assertEquals(TIME_OUT_FIELD, fromJsonFuture.getTimeout()); | ||
MessageFuture toJsonFuture=new MessageFuture(); | ||
toJsonFuture.setRequestMessage(buildRepcMessage()); | ||
toJsonFuture.setTimeout(TIME_OUT_FIELD); | ||
String toJson=JSON.toJSONString(toJsonFuture, true); | ||
Assert.assertEquals(toJson, fromJson); | ||
} | ||
|
||
@Test | ||
public void testIsTimeOut() throws Exception { | ||
MessageFuture messageFuture=new MessageFuture(); | ||
messageFuture.setTimeout(TIME_OUT_FIELD); | ||
Assert.assertTrue(!messageFuture.isTimeout()); | ||
Thread.sleep(TIME_OUT_FIELD); | ||
Assert.assertTrue(messageFuture.isTimeout()); | ||
|
||
} | ||
|
||
@Test(expected =TimeoutException.class) | ||
public void testGetNoResultWithTimeOutException() throws Exception { | ||
MessageFuture messageFuture=new MessageFuture(); | ||
messageFuture.setRequestMessage(buildRepcMessage()); | ||
messageFuture.setTimeout(TIME_OUT_FIELD); | ||
messageFuture.get(TIME_OUT_FIELD, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
@Test(expected =TimeoutException.class) | ||
public void testGetHasResultWithTimeOutException() throws Exception { | ||
MessageFuture messageFuture=new MessageFuture(); | ||
messageFuture.setRequestMessage(buildRepcMessage()); | ||
messageFuture.setTimeout(TIME_OUT_FIELD); | ||
ExecutorService executorService=Executors.newSingleThreadExecutor(); | ||
CountDownLatch downLatch=new CountDownLatch(1); | ||
executorService.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
downLatch.await(); | ||
messageFuture.setResultMessage("has_result"); | ||
} catch (InterruptedException e) { | ||
|
||
} | ||
} | ||
}); | ||
messageFuture.get(TIME_OUT_FIELD, TimeUnit.MILLISECONDS); | ||
downLatch.countDown(); | ||
} | ||
|
||
@Test(expected =RuntimeException.class) | ||
public void testGetHasResultWithRunTimeException() throws Exception { | ||
MessageFuture messageFuture=new MessageFuture(); | ||
messageFuture.setRequestMessage(buildRepcMessage()); | ||
messageFuture.setTimeout(TIME_OUT_FIELD); | ||
messageFuture.setResultMessage(new RuntimeException()); | ||
messageFuture.get(TIME_OUT_FIELD, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
@Test(expected =RuntimeException.class) | ||
public void testGetHasResultWithThrowable() throws Exception { | ||
MessageFuture messageFuture=new MessageFuture(); | ||
messageFuture.setRequestMessage(buildRepcMessage()); | ||
messageFuture.setTimeout(TIME_OUT_FIELD); | ||
messageFuture.setResultMessage(new Throwable("test_throwable")); | ||
messageFuture.get(TIME_OUT_FIELD, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
private RpcMessage buildRepcMessage() { | ||
RpcMessage rpcMessage=new RpcMessage(); | ||
rpcMessage.setId(ID_FIELD); | ||
rpcMessage.setAsync(ASYNC_FIELD); | ||
rpcMessage.setRequest(REQUEST_FIELD); | ||
rpcMessage.setHeartbeat(HEART_BEAT_FIELD); | ||
rpcMessage.setBody(BODY_FIELD); | ||
return rpcMessage; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
core/src/test/java/com/alibaba/fescar/core/protocol/RpcMessageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.fescar.core.protocol; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by guoyao on 2019/3/1. | ||
*/ | ||
public class RpcMessageTest { | ||
|
||
private final boolean ASYNC_FIELD=false; | ||
private final String BODY_FIELD="test_body"; | ||
private final boolean HEART_BEAT_FIELD=true; | ||
private final boolean REQUEST_FIELD=false; | ||
private final long ID_FIELD = 100L; | ||
|
||
|
||
@Test | ||
public void testFieldGetSetFromJson() { | ||
String fromJson="{\n" + | ||
"\t\"async\":" + ASYNC_FIELD + ",\n" + | ||
"\t\"body\":\"" + BODY_FIELD + "\",\n" + | ||
"\t\"heartbeat\":" + HEART_BEAT_FIELD + ",\n" + | ||
"\t\"id\":" + ID_FIELD + ",\n" + | ||
"\t\"request\":" + REQUEST_FIELD + "\n" + | ||
"}"; | ||
RpcMessage fromJsonMessage=JSON.parseObject(fromJson, RpcMessage.class); | ||
Assert.assertEquals(false,fromJsonMessage.isAsync()); | ||
Assert.assertEquals(true,fromJsonMessage.isHeartbeat()); | ||
Assert.assertEquals(false, fromJsonMessage.isRequest()); | ||
Assert.assertEquals("test_body", fromJsonMessage.getBody()); | ||
Assert.assertEquals(100, fromJsonMessage.getId()); | ||
|
||
RpcMessage toJsonMessage=new RpcMessage(); | ||
toJsonMessage.setAsync(ASYNC_FIELD); | ||
toJsonMessage.setBody(BODY_FIELD); | ||
toJsonMessage.setRequest(REQUEST_FIELD); | ||
toJsonMessage.setHeartbeat(HEART_BEAT_FIELD); | ||
toJsonMessage.setId(ID_FIELD); | ||
String toJson=JSON.toJSONString(toJsonMessage, true); | ||
Assert.assertEquals(fromJson, toJson); | ||
} | ||
|
||
@Test | ||
public void testGetNextMessageId() { | ||
Assert.assertEquals(1, RpcMessage.getNextMessageId()); | ||
Assert.assertEquals(2, RpcMessage.getNextMessageId()); | ||
Assert.assertEquals(3, RpcMessage.getNextMessageId()); | ||
Assert.assertEquals(4, RpcMessage.getNextMessageId()); | ||
Assert.assertEquals(5, RpcMessage.getNextMessageId()); | ||
} | ||
} |