|
| 1 | +package org.tron.core; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertSame; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | + |
| 8 | +import java.lang.reflect.Field; |
| 9 | +import java.lang.reflect.Method; |
| 10 | +import java.util.concurrent.Semaphore; |
| 11 | + |
| 12 | +import org.junit.After; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | +import org.tron.common.parameter.CommonParameter; |
| 16 | + |
| 17 | +/** |
| 18 | + * Verifies the lazy-init contract for {@code Wallet.constantCallSemaphore}: |
| 19 | + * permit count is taken from {@code CommonParameter.constantCallMaxConcurrency} |
| 20 | + * at first call, and subsequent calls return the same instance. Together with |
| 21 | + * {@link org.tron.core.vm.VmTimeoutPolicyTest} (which covers |
| 22 | + * {@code isCapEngaged}), this is the integration-level coverage for the |
| 23 | + * constant-call throttle wired up in {@code Wallet.callConstantContract}. |
| 24 | + */ |
| 25 | +public class WalletConstantCallThrottleTest { |
| 26 | + |
| 27 | + private static final Field SEMAPHORE_FIELD; |
| 28 | + private static final Method GET_SEMAPHORE_METHOD; |
| 29 | + |
| 30 | + static { |
| 31 | + try { |
| 32 | + SEMAPHORE_FIELD = Wallet.class.getDeclaredField("constantCallSemaphore"); |
| 33 | + SEMAPHORE_FIELD.setAccessible(true); |
| 34 | + GET_SEMAPHORE_METHOD = Wallet.class.getDeclaredMethod("getConstantCallSemaphore"); |
| 35 | + GET_SEMAPHORE_METHOD.setAccessible(true); |
| 36 | + } catch (NoSuchFieldException | NoSuchMethodException e) { |
| 37 | + throw new ExceptionInInitializerError(e); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private int savedMaxConcurrency; |
| 42 | + |
| 43 | + @Before |
| 44 | + public void setUp() throws Exception { |
| 45 | + savedMaxConcurrency = CommonParameter.getInstance().getConstantCallMaxConcurrency(); |
| 46 | + SEMAPHORE_FIELD.set(null, null); |
| 47 | + } |
| 48 | + |
| 49 | + @After |
| 50 | + public void tearDown() throws Exception { |
| 51 | + CommonParameter.getInstance().setConstantCallMaxConcurrency(savedMaxConcurrency); |
| 52 | + SEMAPHORE_FIELD.set(null, null); |
| 53 | + } |
| 54 | + |
| 55 | + private static Semaphore invokeFactory() throws Exception { |
| 56 | + return (Semaphore) GET_SEMAPHORE_METHOD.invoke(null); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void permitCountTracksConfiguredMaxConcurrency() throws Exception { |
| 61 | + CommonParameter.getInstance().setConstantCallMaxConcurrency(3); |
| 62 | + |
| 63 | + Semaphore sem = invokeFactory(); |
| 64 | + |
| 65 | + assertEquals(3, sem.availablePermits()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void factoryIsIdempotent() throws Exception { |
| 70 | + CommonParameter.getInstance().setConstantCallMaxConcurrency(2); |
| 71 | + |
| 72 | + Semaphore first = invokeFactory(); |
| 73 | + Semaphore second = invokeFactory(); |
| 74 | + |
| 75 | + assertSame(first, second); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void permitCountFrozenAtFirstCall() throws Exception { |
| 80 | + CommonParameter.getInstance().setConstantCallMaxConcurrency(1); |
| 81 | + Semaphore sem = invokeFactory(); |
| 82 | + |
| 83 | + // Mutating the parameter after the semaphore has been created must not |
| 84 | + // resize it — confirms that operators cannot widen the cap on a live node. |
| 85 | + CommonParameter.getInstance().setConstantCallMaxConcurrency(99); |
| 86 | + |
| 87 | + assertSame(sem, invokeFactory()); |
| 88 | + assertEquals(1, sem.availablePermits()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void semaphoreSaturatesAtConfiguredCap() throws Exception { |
| 93 | + CommonParameter.getInstance().setConstantCallMaxConcurrency(2); |
| 94 | + Semaphore sem = invokeFactory(); |
| 95 | + |
| 96 | + assertTrue(sem.tryAcquire()); |
| 97 | + assertTrue(sem.tryAcquire()); |
| 98 | + assertFalse("3rd acquire must fail when cap=2", sem.tryAcquire()); |
| 99 | + |
| 100 | + sem.release(); |
| 101 | + assertTrue("permit must replenish after release", sem.tryAcquire()); |
| 102 | + |
| 103 | + sem.release(); |
| 104 | + sem.release(); |
| 105 | + } |
| 106 | +} |
0 commit comments