Skip to content

Commit

Permalink
Add unittest rpc netty 20190125 (#305)
Browse files Browse the repository at this point in the history
* release 0.1.1 (#178)

* Remove redundant semicolons and modify non-standard names

* remove duplicate RM init

* issue #86 length of applicationData should be int

* fix: fixed qualifier

* Modify method description of DemoCode

* Update ConfigurationKeys.java

* config instance obtained by the factory class

* fix typo of `retryable` as the annotation of Spring Retryable. add static code to init BranchStatus mapper to speed up get(int ordinal)

* add template

* add template

* add template

* fixes #117

* fixes #114

* fix issue #122

* update template

* fix file.RenameTo->Files.move

* #73 add travis config

* fix #121

* #73 add travis config

* fix #135 delete BranchSession main

* fix #137 param position err

* fix #142 delete defaultEventExecutorGroup

* fix #142 delete defaultEventExecutorGroup (#144)

* fix #139 netty heartbeat configurable

* fixes #139 heartbeat configurable (#146)

* fix #142 delete defaultEventExecutorGroup

* fix #139 netty heartbeat configurable

* FileTransactionStoreManager#closeFile() method remove redundant fileChannel.close() invoke (#140)

* fix: When local branch change nothing, the RC will rise an NullpointerException (#155)

* bugfix: when no record changed register localBranch will occur exception
in TC

* clean the environment

* enhancement: when no changes in local branch let RC not throw an
NullpointerException

* change tabs to spaces

* fix #150 use ServerBootstrap#childOption() to set SO_KEEPALIVE (#151)

* FileTransactionStoreManager#closeFile() method remove redundant fileChannel.close() invoke

* Netty SO_KEEPALIVE option not works on ServerBootstrap#option(), should replace with ServerBootstrap#childOption()

* fix #149 (long)->Number.longValue

*  fix #149 (long)->Number.longValue (#156)

* fix #142 delete defaultEventExecutorGroup

* fix #139 netty heartbeat configurable

* fix #149 (long)->Number.longValue

* remove mistake twitter icon (#165)

* Update README.md

* Update README.md

* issue #110 bug fix for RM channel management (#169)

* issue #110 fix RM channel management

* fix merge mistake

* issue #110 enhance: if no channel found on my application set, try other application on the same resource.

* bug fix and enhance

* issue #110 enhance the original fix

* fix: Unify `undo_log` table name variable (#174)

* ShowSql might be better when debugging to execute the demo to observe the data. (#164)

issues #158

* revert OrderServiceImpl

* add toString method

* add toString method

* add toString method

* add toString method

* fix

* 小优化-类型转换

* 类型转换小优化

* add unit test to TmRpcClient

* fix conflicts

* fix conflicts

* add unit test to TmRpcClient

* add unit test to TmRpcClient

* add unit test to TmRpcClient

* add unit test to TmRpcClient

* add unit test to TmRpcClient

* add unit test to TmRpcClient

* add unit test to TmRpcClient

* add unit test to TmRpcClient

* add unit test to TmRpcClient reconnect method

* add unit test to RegisterTMResponse

* add unit test

* add unit test to TmRpcClient

* add copyright

* remove printf
  • Loading branch information
summerjava authored and slievrly committed Jan 30, 2019
1 parent 40ea4eb commit f817d35
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,26 @@ public boolean decode(ByteBuf in) {

return true;
}

@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("version=");
result.append(String.valueOf(version));
result.append(",");
result.append("extraData=");
result.append(String.valueOf(extraData));
result.append(",");
result.append("identified=");
result.append(String.valueOf(identified));
result.append(",");
result.append("resultCode=");
result.append(String.valueOf(getResultCode()));
result.append(",");
result.append("msg=");
result.append(String.valueOf(getMsg()));


return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.alibaba.fescar.core.protocol.transaction;

import com.alibaba.fescar.core.protocol.AbstractMessage;

import java.io.Serializable;
import java.nio.ByteBuffer;

Expand Down Expand Up @@ -87,4 +89,22 @@ public void decode(ByteBuffer byteBuffer) {
this.transactionId = byteBuffer.getLong();
this.branchId = byteBuffer.getLong();
}

@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("BranchRegisterResponse: transactionId=");
result.append(transactionId);
result.append(",");
result.append("branchId=");
result.append(branchId);
result.append(",");
result.append("result code =");
result.append(getResultCode());
result.append(",");
result.append("getMsg =");
result.append(getMsg());

return result.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.message;

import com.alibaba.fescar.core.protocol.ResultCode;
import com.alibaba.fescar.core.protocol.transaction.BranchRegisterResponse;

import org.junit.Assert;
import org.junit.Test;

public class BranchRegisterResponseTest {

@Test
public void toStringTest() throws Exception{
BranchRegisterResponse branchRegisterResponse = new BranchRegisterResponse();

branchRegisterResponse.setTransactionId(123456L);
branchRegisterResponse.setBranchId(123457L);
branchRegisterResponse.setResultCode(ResultCode.Success);
branchRegisterResponse.setMsg("");
Assert.assertEquals("BranchRegisterResponse: transactionId=123456,branchId=123457,result code =Success,getMsg =", branchRegisterResponse.toString());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.message;

import com.alibaba.fescar.core.protocol.RegisterTMResponse;
import com.alibaba.fescar.core.protocol.ResultCode;

import org.junit.Assert;
import org.junit.Test;

public class RegisterTMResponseTest {

@Test
public void testToString() throws Exception{
RegisterTMResponse registerTMResponse = new RegisterTMResponse();

registerTMResponse.setVersion("1");
registerTMResponse.setIdentified(true);
registerTMResponse.setResultCode(ResultCode.Success);

Assert.assertEquals("version=1,extraData=null,identified=true,resultCode=Success,msg=null", registerTMResponse.toString());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,87 @@

package com.alibaba.fescar.core.rpc.netty;

import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ChannelFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
* The type Tm rpc client test.
*
* @Author: jimin.jm @alibaba-inc.com
* @Project: fescar -all
* @DateTime: 2018 /12/26 14:32
* @Author: jimin.jm@alibaba-inc.com
* @Author: xiajun.0706@163.com
* @Project: fescar-all
* @DateTime: 2019/01/25 08:32
* @FileName: TmRpcClientTest
* @Description:
*/
public class TmRpcClientTest {

private static final ThreadPoolExecutor
workingThreads = new ThreadPoolExecutor(100, 500, 500, TimeUnit.SECONDS,
new LinkedBlockingQueue(20000), new ThreadPoolExecutor.CallerRunsPolicy());

@Test
public void testGetInstance() throws Exception {
String applicationId = "app 1";
String transactionServiceGroup = "group A";
TmRpcClient tmRpcClient = TmRpcClient.getInstance(applicationId, transactionServiceGroup);

NettyClientConfig defaultNettyClientConfig = new NettyClientConfig();
GenericKeyedObjectPool.Config config = tmRpcClient.getNettyPoolConfig();
Assert.assertEquals(defaultNettyClientConfig.getMaxPoolActive(), config.maxActive);
Assert.assertEquals(defaultNettyClientConfig.getMinPoolIdle(), config.minIdle);
Assert.assertEquals(defaultNettyClientConfig.getMaxAcquireConnMills(), config.maxWait);
Assert.assertEquals(defaultNettyClientConfig.isPoolTestBorrow(), config.testOnBorrow);
Assert.assertEquals(defaultNettyClientConfig.isPoolTestReturn(), config.testOnReturn);
Assert.assertEquals(defaultNettyClientConfig.isPoolFifo(), config.lifo);
}

/**
* Do connect.
*
* @throws Exception the exception
*/
@Test
public void doConnect() throws Exception {
public void testInit() throws Exception {
String applicationId = "app 1";
String transactionServiceGroup = "group A";
TmRpcClient tmRpcClient = TmRpcClient.getInstance(applicationId, transactionServiceGroup);

tmRpcClient.init();

//check if attr of tmRpcClient object has been set success
Field bootstrapField = getDeclaredField(tmRpcClient, "bootstrap");
bootstrapField.setAccessible(true);
Bootstrap bootstrap = (Bootstrap) bootstrapField.get(tmRpcClient);

Assert.assertNotNull(bootstrap);
Field optionsField = getDeclaredField(bootstrap, "options");
optionsField.setAccessible(true);
Map<ChannelOption<?>, Object> options = (Map<ChannelOption<?>, Object>) optionsField.get(bootstrap);
Assert.assertTrue(Boolean.TRUE.equals(options.get(ChannelOption.TCP_NODELAY)));
Assert.assertTrue(Boolean.TRUE.equals(options.get(ChannelOption.SO_KEEPALIVE)));
Assert.assertEquals(10000, options.get(ChannelOption.CONNECT_TIMEOUT_MILLIS));
Assert.assertTrue(Boolean.TRUE.equals(options.get(ChannelOption.SO_KEEPALIVE)));
Assert.assertEquals(153600, options.get(ChannelOption.SO_RCVBUF));

Field channelFactoryField = getDeclaredField(bootstrap, "channelFactory");
channelFactoryField.setAccessible(true);
ChannelFactory<? extends Channel>
channelFactory = (ChannelFactory<? extends Channel>) channelFactoryField.get(bootstrap);
Assert.assertNotNull(channelFactory);
Assert.assertTrue(channelFactory.newChannel() instanceof NioSocketChannel);

}

Expand All @@ -58,4 +120,25 @@ public void setApplicationId() throws Exception {

}

/**
* get private field in parent class
*
* @param object
* @param fieldName
* @return
*/
public static Field getDeclaredField(Object object, String fieldName){
Field field = null ;
Class<?> clazz = object.getClass() ;
for(; clazz != Object.class ; clazz = clazz.getSuperclass()) {
try {
field = clazz.getDeclaredField(fieldName) ;
return field ;
} catch (Exception e) {

}
}

return null;
}
}
4 changes: 4 additions & 0 deletions test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
<groupId>${project.groupId}</groupId>
<artifactId>fescar-tm</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>fescar-server</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>fescar-rm-datasource</artifactId>
Expand Down
Loading

0 comments on commit f817d35

Please sign in to comment.