|
| 1 | +package org.tron.core.utils; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertThrows; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | +import static org.mockito.Mockito.mockConstruction; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import java.lang.reflect.Field; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.HashSet; |
| 13 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 14 | +import org.junit.After; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | +import org.junit.runner.RunWith; |
| 18 | +import org.mockito.MockedConstruction; |
| 19 | +import org.mockito.junit.MockitoJUnitRunner; |
| 20 | +import org.reflections.Reflections; |
| 21 | +import org.tron.core.actuator.AbstractActuator; |
| 22 | +import org.tron.core.actuator.TransferActuator; |
| 23 | +import org.tron.core.config.args.Args; |
| 24 | +import org.tron.core.exception.TronError; |
| 25 | + |
| 26 | +@RunWith(MockitoJUnitRunner.class) |
| 27 | +public class TransactionRegisterTest { |
| 28 | + |
| 29 | + @Before |
| 30 | + public void init() throws Exception { |
| 31 | + Args.getInstance().setActuatorSet(new HashSet<>()); |
| 32 | + resetRegisteredField(); |
| 33 | + } |
| 34 | + |
| 35 | + @After |
| 36 | + public void destroy() { |
| 37 | + Args.clearParam(); |
| 38 | + } |
| 39 | + |
| 40 | + private void resetRegisteredField() throws Exception { |
| 41 | + Field registeredField = TransactionRegister.class.getDeclaredField("REGISTERED"); |
| 42 | + registeredField.setAccessible(true); |
| 43 | + AtomicBoolean registered = (AtomicBoolean) registeredField.get(null); |
| 44 | + registered.set(false); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testAlreadyRegisteredSkipRegistration() throws Exception { |
| 49 | + |
| 50 | + TransactionRegister.registerActuator(); |
| 51 | + |
| 52 | + Field registeredField = TransactionRegister.class.getDeclaredField("REGISTERED"); |
| 53 | + registeredField.setAccessible(true); |
| 54 | + AtomicBoolean registered = (AtomicBoolean) registeredField.get(null); |
| 55 | + assertTrue("First registration should be completed", registered.get()); |
| 56 | + |
| 57 | + TransactionRegister.registerActuator(); |
| 58 | + assertTrue("Registration should still be true", registered.get()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testConcurrentAccessThreadSafe() throws Exception { |
| 63 | + final int threadCount = 5; |
| 64 | + Thread[] threads = new Thread[threadCount]; |
| 65 | + final AtomicBoolean testPassed = new AtomicBoolean(true); |
| 66 | + |
| 67 | + for (int i = 0; i < threadCount; i++) { |
| 68 | + threads[i] = new Thread(() -> { |
| 69 | + try { |
| 70 | + TransactionRegister.registerActuator(); |
| 71 | + } catch (Exception e) { |
| 72 | + testPassed.set(false); |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + for (Thread thread : threads) { |
| 78 | + thread.start(); |
| 79 | + } |
| 80 | + |
| 81 | + for (Thread thread : threads) { |
| 82 | + thread.join(); |
| 83 | + } |
| 84 | + |
| 85 | + assertTrue("All threads should complete successfully", testPassed.get()); |
| 86 | + |
| 87 | + Field registeredField = TransactionRegister.class.getDeclaredField("REGISTERED"); |
| 88 | + registeredField.setAccessible(true); |
| 89 | + AtomicBoolean registered = (AtomicBoolean) registeredField.get(null); |
| 90 | + assertTrue("Registration should be completed", registered.get()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testDoubleCheckLockingAtomicBoolean() throws Exception { |
| 95 | + Field registeredField = TransactionRegister.class.getDeclaredField("REGISTERED"); |
| 96 | + registeredField.setAccessible(true); |
| 97 | + AtomicBoolean registered = (AtomicBoolean) registeredField.get(null); |
| 98 | + |
| 99 | + assertFalse("Initial registration state should be false", registered.get()); |
| 100 | + |
| 101 | + TransactionRegister.registerActuator(); |
| 102 | + assertTrue("After first call, should be registered", registered.get()); |
| 103 | + |
| 104 | + TransactionRegister.registerActuator(); |
| 105 | + assertTrue("After second call, should still be registered", registered.get()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testSynchronizationBlock() throws Exception { |
| 110 | + final AtomicBoolean completedRegistration = new AtomicBoolean(false); |
| 111 | + |
| 112 | + Thread registrationThread = new Thread(() -> { |
| 113 | + TransactionRegister.registerActuator(); |
| 114 | + completedRegistration.set(true); |
| 115 | + |
| 116 | + }); |
| 117 | + |
| 118 | + registrationThread.start(); |
| 119 | + registrationThread.join(); |
| 120 | + |
| 121 | + assertTrue("Registration should have completed", completedRegistration.get()); |
| 122 | + |
| 123 | + Field registeredField = TransactionRegister.class.getDeclaredField("REGISTERED"); |
| 124 | + registeredField.setAccessible(true); |
| 125 | + AtomicBoolean registered = (AtomicBoolean) registeredField.get(null); |
| 126 | + assertTrue("Should be registered after completion", registered.get()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testMultipleCallsConsistency() throws Exception { |
| 131 | + Field registeredField = TransactionRegister.class.getDeclaredField("REGISTERED"); |
| 132 | + registeredField.setAccessible(true); |
| 133 | + AtomicBoolean registered = (AtomicBoolean) registeredField.get(null); |
| 134 | + |
| 135 | + assertFalse("Should start unregistered", registered.get()); |
| 136 | + |
| 137 | + TransactionRegister.registerActuator(); |
| 138 | + |
| 139 | + assertTrue("Should be registered after first call", registered.get()); |
| 140 | + |
| 141 | + for (int i = 0; i < 5; i++) { |
| 142 | + TransactionRegister.registerActuator(); |
| 143 | + assertTrue("Should remain registered after call " + (i + 2), registered.get()); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void testThrowsTronError() { |
| 149 | + try (MockedConstruction<Reflections> ignored = mockConstruction(Reflections.class, |
| 150 | + (mock, context) -> when(mock.getSubTypesOf(AbstractActuator.class)) |
| 151 | + .thenReturn(Collections.singleton(TransferActuator.class))); |
| 152 | + MockedConstruction<TransferActuator> ignored1 = mockConstruction(TransferActuator.class, |
| 153 | + (mock, context) -> { |
| 154 | + throw new RuntimeException("boom"); |
| 155 | + })) { |
| 156 | + TronError error = assertThrows(TronError.class, TransactionRegister::registerActuator); |
| 157 | + assertEquals(TronError.ErrCode.ACTUATOR_REGISTER, error.getErrCode()); |
| 158 | + assertTrue(error.getMessage().contains("TransferActuator")); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments