use of io.seata.core.protocol.transaction.GlobalBeginResponse in project seata by seata.
the class GlobalBeginResponseSerializerTest method test_codec.
/**
* Test codec.
*/
@Test
public void test_codec() {
GlobalBeginResponse globalBeginResponse = new GlobalBeginResponse();
globalBeginResponse.setTransactionExceptionCode(TransactionExceptionCode.GlobalTransactionNotActive);
globalBeginResponse.setExtraData("absd");
globalBeginResponse.setXid("2454");
globalBeginResponse.setResultCode(ResultCode.Failed);
globalBeginResponse.setMsg("abcs");
byte[] bytes = seataSerializer.serialize(globalBeginResponse);
GlobalBeginResponse globalBeginResponse2 = seataSerializer.deserialize(bytes);
assertThat(globalBeginResponse2.getTransactionExceptionCode()).isEqualTo(globalBeginResponse.getTransactionExceptionCode());
assertThat(globalBeginResponse2.getResultCode()).isEqualTo(globalBeginResponse.getResultCode());
assertThat(globalBeginResponse2.getXid()).isEqualTo(globalBeginResponse.getXid());
assertThat(globalBeginResponse2.getExtraData()).isEqualTo(globalBeginResponse.getExtraData());
assertThat(globalBeginResponse2.getMsg()).isEqualTo(globalBeginResponse.getMsg());
}
use of io.seata.core.protocol.transaction.GlobalBeginResponse in project seata by seata.
the class DefaultCoordinatorMetricsTest method test.
@Test
public void test() throws IOException, TransactionException, InterruptedException {
SessionHolder.init(null);
DefaultCoordinator coordinator = new DefaultCoordinator(new MockServerMessageSender());
coordinator.init();
try {
MetricsManager.get().init();
// start a transaction
GlobalBeginRequest request = new GlobalBeginRequest();
request.setTransactionName("test_transaction");
GlobalBeginResponse response = new GlobalBeginResponse();
coordinator.doGlobalBegin(request, response, new RpcContext());
Map<String, Measurement> measurements = new HashMap<>();
MetricsManager.get().getRegistry().measure().forEach(measurement -> measurements.put(measurement.getId().toString(), measurement));
Assertions.assertEquals(1, measurements.size());
Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=counter,role=tc,status=active)").getValue(), 0);
// commit this transaction
GlobalCommitRequest commitRequest = new GlobalCommitRequest();
commitRequest.setXid(response.getXid());
coordinator.doGlobalCommit(commitRequest, new GlobalCommitResponse(), new RpcContext());
// we need sleep for a short while because default canBeCommittedAsync() is true
Thread.sleep(200);
measurements.clear();
MetricsManager.get().getRegistry().measure().forEach(measurement -> measurements.put(measurement.getId().toString(), measurement));
Assertions.assertEquals(9, measurements.size());
Assertions.assertEquals(0, measurements.get("seata.transaction(applicationId=null,group=null,meter=counter,role=tc,status=active)").getValue(), 0);
Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=counter,role=tc,status=committed)").getValue(), 0);
Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=summary,role=tc,statistic=count,status=committed)").getValue(), 0);
Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=summary,role=tc,statistic=total,status=committed)").getValue(), 0);
Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=timer,role=tc,statistic=count,status=committed)").getValue(), 0);
// start another new transaction
request = new GlobalBeginRequest();
request.setTransactionName("test_transaction_2");
response = new GlobalBeginResponse();
coordinator.doGlobalBegin(request, response, new RpcContext());
// rollback this transaction
GlobalRollbackRequest rollbackRequest = new GlobalRollbackRequest();
rollbackRequest.setXid(response.getXid());
coordinator.doGlobalRollback(rollbackRequest, new GlobalRollbackResponse(), new RpcContext());
Thread.sleep(200);
measurements.clear();
MetricsManager.get().getRegistry().measure().forEach(measurement -> measurements.put(measurement.getId().toString(), measurement));
Assertions.assertEquals(17, measurements.size());
Assertions.assertEquals(0, measurements.get("seata.transaction(applicationId=null,group=null,meter=counter,role=tc,status=active)").getValue(), 0);
Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=counter,role=tc,status=committed)").getValue(), 0);
Assertions.assertEquals(0, measurements.get("seata.transaction(applicationId=null,group=null,meter=summary,role=tc,statistic=count,status=committed)").getValue(), 0);
Assertions.assertEquals(0, measurements.get("seata.transaction(applicationId=null,group=null,meter=summary,role=tc,statistic=total,status=committed)").getValue(), 0);
Assertions.assertEquals(0, measurements.get("seata.transaction(applicationId=null,group=null,meter=timer,role=tc,statistic=count,status=committed)").getValue(), 0);
Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=counter,role=tc,status=rollbacked)").getValue(), 0);
Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=summary,role=tc,statistic=count,status=rollbacked)").getValue(), 0);
Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=summary,role=tc,statistic=total,status=rollbacked)").getValue(), 0);
Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=timer,role=tc,statistic=count,status=rollbacked)").getValue(), 0);
} finally {
coordinator.destroy();
SessionHolder.destroy();
}
}
use of io.seata.core.protocol.transaction.GlobalBeginResponse in project seata by seata.
the class GlobalBeginResponseConvertorTest method convert2Proto.
@Test
public void convert2Proto() {
GlobalBeginResponse globalBeginResponse = new GlobalBeginResponse();
globalBeginResponse.setResultCode(ResultCode.Failed);
globalBeginResponse.setMsg("msg");
globalBeginResponse.setExtraData("extraData");
globalBeginResponse.setXid("xid");
globalBeginResponse.setTransactionExceptionCode(TransactionExceptionCode.BranchRollbackFailed_Retriable);
GlobalBeginResponseConvertor convertor = new GlobalBeginResponseConvertor();
GlobalBeginResponseProto proto = convertor.convert2Proto(globalBeginResponse);
GlobalBeginResponse real = convertor.convert2Model(proto);
assertThat((real.getTypeCode())).isEqualTo(globalBeginResponse.getTypeCode());
assertThat((real.getMsg())).isEqualTo(globalBeginResponse.getMsg());
assertThat((real.getResultCode())).isEqualTo(globalBeginResponse.getResultCode());
assertThat((real.getTransactionExceptionCode())).isEqualTo(globalBeginResponse.getTransactionExceptionCode());
}
use of io.seata.core.protocol.transaction.GlobalBeginResponse in project seata by seata.
the class GlobalBeginResponseCodec method encode.
@Override
public <T> void encode(T t, ByteBuf out) {
super.encode(t, out);
GlobalBeginResponse globalBeginResponse = (GlobalBeginResponse) t;
String xid = globalBeginResponse.getXid();
String extraData = globalBeginResponse.getExtraData();
if (xid != null) {
byte[] bs = xid.getBytes(UTF8);
out.writeShort((short) bs.length);
if (bs.length > 0) {
out.writeBytes(bs);
}
} else {
out.writeShort((short) 0);
}
if (extraData != null) {
byte[] bs = extraData.getBytes(UTF8);
out.writeShort((short) bs.length);
if (bs.length > 0) {
out.writeBytes(bs);
}
} else {
out.writeShort((short) 0);
}
}
use of io.seata.core.protocol.transaction.GlobalBeginResponse in project seata by seata.
the class GlobalBeginResponseCodec method decode.
@Override
public <T> void decode(T t, ByteBuffer in) {
super.decode(t, in);
GlobalBeginResponse globalBeginResponse = (GlobalBeginResponse) t;
short len = in.getShort();
if (len > 0) {
byte[] bs = new byte[len];
in.get(bs);
globalBeginResponse.setXid(new String(bs, UTF8));
}
len = in.getShort();
if (len > 0) {
byte[] bs = new byte[len];
in.get(bs);
globalBeginResponse.setExtraData(new String(bs, UTF8));
}
}
Aggregations