Skip to content

Commit

Permalink
bugfix: clean up resources in time to avoid mutual influence between …
Browse files Browse the repository at this point in the history
…unit tests (apache#1778)
  • Loading branch information
ggndnn authored and zjinlei committed Oct 17, 2019
1 parent 2ded19c commit 1020433
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 283 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,6 @@ public void destroy() {
((RpcServer)messageSender).destroy();
}
// 3. last destroy SessionHolder
SessionHolder.destory();
SessionHolder.destroy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public static GlobalSession findGlobalSession(String xid) {
return getRootSessionManager().findGlobalSession(xid);
}

public static void destory() {
public static void destroy() {
ROOT_SESSION_MANAGER.destroy();
ASYNC_COMMITTING_SESSION_MANAGER.destroy();
RETRY_COMMITTING_SESSION_MANAGER.destroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,91 +44,95 @@ public void test() throws IOException, TransactionException, InterruptedExceptio
SessionHolder.init(null);
DefaultCoordinator coordinator = new DefaultCoordinator(null);
coordinator.init();

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(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(meter=counter,role=tc,status=active)").getValue(), 0);
Assertions
.assertEquals(1, measurements.get("seata.transaction(meter=counter,role=tc,status=committed)").getValue(),
0);
Assertions.assertEquals(1,
measurements.get("seata.transaction(meter=summary,role=tc,statistic=count,status=committed)").getValue(),
0);
Assertions.assertEquals(1,
measurements.get("seata.transaction(meter=summary,role=tc,statistic=total,status=committed)").getValue(),
0);
Assertions.assertEquals(1,
measurements.get("seata.transaction(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(meter=counter,role=tc,status=active)").getValue(), 0);

Assertions
.assertEquals(1, measurements.get("seata.transaction(meter=counter,role=tc,status=committed)").getValue(),
0);
Assertions.assertEquals(0,
measurements.get("seata.transaction(meter=summary,role=tc,statistic=count,status=committed)").getValue(),
0);
Assertions.assertEquals(0,
measurements.get("seata.transaction(meter=summary,role=tc,statistic=total,status=committed)").getValue(),
0);
Assertions.assertEquals(0,
measurements.get("seata.transaction(meter=timer,role=tc,statistic=count,status=committed)").getValue(), 0);

Assertions.assertEquals(1,
measurements.get("seata.transaction(meter=counter,role=tc,status=rollbacked)").getValue(), 0);
Assertions.assertEquals(1,
measurements.get("seata.transaction(meter=summary,role=tc,statistic=count,status=rollbacked)").getValue(),
0);
Assertions.assertEquals(1,
measurements.get("seata.transaction(meter=summary,role=tc,statistic=total,status=rollbacked)").getValue(),
0);
Assertions.assertEquals(1,
measurements.get("seata.transaction(meter=timer,role=tc,statistic=count,status=rollbacked)").getValue(), 0);
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(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(meter=counter,role=tc,status=active)").getValue(), 0);
Assertions
.assertEquals(1, measurements.get("seata.transaction(meter=counter,role=tc,status=committed)").getValue(),
0);
Assertions.assertEquals(1,
measurements.get("seata.transaction(meter=summary,role=tc,statistic=count,status=committed)").getValue(),
0);
Assertions.assertEquals(1,
measurements.get("seata.transaction(meter=summary,role=tc,statistic=total,status=committed)").getValue(),
0);
Assertions.assertEquals(1,
measurements.get("seata.transaction(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(meter=counter,role=tc,status=active)").getValue(), 0);

Assertions
.assertEquals(1, measurements.get("seata.transaction(meter=counter,role=tc,status=committed)").getValue(),
0);
Assertions.assertEquals(0,
measurements.get("seata.transaction(meter=summary,role=tc,statistic=count,status=committed)").getValue(),
0);
Assertions.assertEquals(0,
measurements.get("seata.transaction(meter=summary,role=tc,statistic=total,status=committed)").getValue(),
0);
Assertions.assertEquals(0,
measurements.get("seata.transaction(meter=timer,role=tc,statistic=count,status=committed)").getValue(), 0);

Assertions.assertEquals(1,
measurements.get("seata.transaction(meter=counter,role=tc,status=rollbacked)").getValue(), 0);
Assertions.assertEquals(1,
measurements.get("seata.transaction(meter=summary,role=tc,statistic=count,status=rollbacked)").getValue(),
0);
Assertions.assertEquals(1,
measurements.get("seata.transaction(meter=summary,role=tc,statistic=total,status=rollbacked)").getValue(),
0);
Assertions.assertEquals(1,
measurements.get("seata.transaction(meter=timer,role=tc,statistic=count,status=rollbacked)").getValue(), 0);
} finally {
coordinator.destroy();
SessionHolder.destroy();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public static void afterClass() throws Exception {
for (GlobalSession globalSession : globalSessions) {
globalSession.closeAndClean();
}

SessionHolder.destroy();
}

static Stream<Arguments> xidAndBranchIdProviderForCommit() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
import io.seata.server.session.GlobalSession;
import io.seata.server.session.SessionHelper;
import io.seata.server.session.SessionHolder;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -70,11 +71,19 @@ public class DefaultCoreTest {
*
* @throws Exception the exception
*/
@BeforeEach
public void initSessionManager() throws Exception {
@BeforeAll
public static void initSessionManager() throws Exception {
SessionHolder.init(null);
}

/**
* Destroy session manager.
*/
@AfterAll
public static void destroySessionManager() {
SessionHolder.destroy();
}

/**
* Clean.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,49 +57,52 @@ public void processGlobalTransactionEvent(GlobalTransactionEvent event) {
counter.addAndGet(1);
}
}

SessionHolder.init(null);
DefaultCoordinator coordinator = new DefaultCoordinator(null);
coordinator.init();
try {
Core core = CoreFactory.get();

Core core = CoreFactory.get();

GlobalTransactionEventSubscriber subscriber = new GlobalTransactionEventSubscriber();
EventBusManager.get().register(subscriber);
GlobalTransactionEventSubscriber subscriber = new GlobalTransactionEventSubscriber();
EventBusManager.get().register(subscriber);

//start a transaction
String xid = core.begin("test_app_id", "default_group", "test_tran_name", 30000);
//start a transaction
String xid = core.begin("test_app_id", "default_group", "test_tran_name", 30000);

Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Begin).get());
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Begin).get());

//commit this transaction
core.commit(xid);
//commit this transaction
core.commit(xid);

//we need sleep for a short while because default canBeCommittedAsync() is true
Thread.sleep(1000);
//we need sleep for a short while because default canBeCommittedAsync() is true
Thread.sleep(1000);

//check
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.AsyncCommitting).get());
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Committed).get());
//check
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.AsyncCommitting).get());
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Committed).get());

//start another new transaction
xid = core.begin("test_app_id", "default_group", "test_tran_name2", 30000);
//start another new transaction
xid = core.begin("test_app_id", "default_group", "test_tran_name2", 30000);

Assertions.assertEquals(2, subscriber.getEventCounters().get(GlobalStatus.Begin).get());
Assertions.assertEquals(2, subscriber.getEventCounters().get(GlobalStatus.Begin).get());

core.rollback(xid);
core.rollback(xid);

//check
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Rollbacking).get());
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Rollbacked).get());
//check
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Rollbacking).get());
Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Rollbacked).get());

//start more one new transaction for test timeout and let this transaction immediately timeout
xid = core.begin("test_app_id", "default_group", "test_tran_name3", 0);
//start more one new transaction for test timeout and let this transaction immediately timeout
xid = core.begin("test_app_id", "default_group", "test_tran_name3", 0);

//sleep for check -> DefaultCoordinator.timeoutCheck
Thread.sleep(1000);
//sleep for check -> DefaultCoordinator.timeoutCheck
Thread.sleep(1000);

//at lease retry once because DefaultCoordinator.timeoutCheck is 1 second
Assertions.assertTrue(subscriber.getEventCounters().get(GlobalStatus.TimeoutRollbacking).get() >= 1);
//at lease retry once because DefaultCoordinator.timeoutCheck is 1 second
Assertions.assertTrue(subscriber.getEventCounters().get(GlobalStatus.TimeoutRollbacking).get() >= 1);
} finally {
coordinator.destroy();
SessionHolder.destroy();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ public void testInit() throws IOException {
}
final String mode = StoreMode.FILE.toString();
SessionHolder.init(mode);
final File actual = new File(pathname);
Assertions.assertTrue(actual.exists());
Assertions.assertTrue(actual.isFile());
try {
final File actual = new File(pathname);
Assertions.assertTrue(actual.exists());
Assertions.assertTrue(actual.isFile());
} finally {
SessionHolder.destroy();
}
}

@AfterEach
Expand Down
Loading

0 comments on commit 1020433

Please sign in to comment.