Skip to content

Commit c3e2c56

Browse files
committed
fix(net): refine TransactionsMsgHandler shutdown and test
1 parent 6d5b17a commit c3e2c56

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

framework/src/main/java/org/tron/core/net/messagehandler/TransactionsMsgHandler.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ public void init() {
6666
*/
6767
public void close() {
6868
isClosed = true;
69-
// Shutdown the producer (scheduler) first to stop generating new tasks
69+
// Shutdown the producer (scheduler) first.
70+
// Pending tasks in smartContractQueue will be discarded.
7071
ExecutorServiceManager.shutdownAndAwaitTermination(smartContractExecutor, smartEsName);
71-
// Then shutdown the consumer (worker pool) to finish existing tasks
72+
// Then shutdown the consumer (worker pool) to finish tasks already submitted to trxHandlePool.
7273
ExecutorServiceManager.shutdownAndAwaitTermination(trxHandlePool, trxEsName);
7374
}
7475

@@ -141,9 +142,7 @@ private void check(PeerConnection peer, TransactionsMessage msg) throws P2pExcep
141142
private void handleSmartContract() {
142143
ExecutorServiceManager.scheduleWithFixedDelay(smartContractExecutor, () -> {
143144
try {
144-
// Stop fetching from queue if the handler is closed
145-
while (!isClosed
146-
&& queue.size() < MAX_SMART_CONTRACT_SUBMIT_SIZE
145+
while (queue.size() < MAX_SMART_CONTRACT_SUBMIT_SIZE
147146
&& smartContractQueue.size() > 0) {
148147
TrxEvent event = smartContractQueue.take();
149148
ExecutorServiceManager.submit(

framework/src/test/java/org/tron/core/net/messagehandler/TransactionsMsgHandlerTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,18 @@ public void testProcessMessage() {
133133
}
134134

135135
@Test
136-
public void testProcessMessageAfterClose() {
136+
public void testProcessMessageAfterClose() throws Exception {
137137
TransactionsMsgHandler handler = new TransactionsMsgHandler();
138138
handler.init();
139139
handler.close();
140140

141141
PeerConnection peer = Mockito.mock(PeerConnection.class);
142142
TransactionsMessage msg = Mockito.mock(TransactionsMessage.class);
143143

144-
try {
145-
handler.processMessage(peer, msg);
146-
} catch (Exception e) {
147-
Assert.fail("Should not throw any exception when closed");
148-
}
144+
handler.processMessage(peer, msg);
145+
146+
// Verify that the handler immediately returned and did not process the message
147+
Mockito.verify(msg, Mockito.never()).getTransactions();
149148
}
150149

151150
class TrxEvent {

0 commit comments

Comments
 (0)