|
| 1 | +package org.tron.core.consensus; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.mock; |
| 4 | + |
| 5 | +import java.lang.reflect.Field; |
| 6 | +import java.lang.reflect.Method; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Test; |
| 9 | +import org.mockito.Mockito; |
| 10 | +import org.tron.consensus.base.BlockHandle; |
| 11 | +import org.tron.consensus.base.State; |
| 12 | +import org.tron.consensus.dpos.DposService; |
| 13 | +import org.tron.consensus.dpos.DposSlot; |
| 14 | +import org.tron.consensus.dpos.DposTask; |
| 15 | +import org.tron.consensus.dpos.StateManager; |
| 16 | + |
| 17 | +public class DposTaskTest { |
| 18 | + private DposTask dposTask = new DposTask(); |
| 19 | + |
| 20 | + @Test |
| 21 | + public void tet() throws Exception { |
| 22 | + StateManager stateManager = mock(StateManager.class); |
| 23 | + Mockito.when(stateManager.getState()).thenReturn(State.BACKUP_IS_NOT_MASTER); |
| 24 | + |
| 25 | + Field field = dposTask.getClass().getDeclaredField("stateManager"); |
| 26 | + field.setAccessible(true); |
| 27 | + field.set(dposTask, stateManager); |
| 28 | + |
| 29 | + Method method = dposTask.getClass().getDeclaredMethod("produceBlock"); |
| 30 | + method.setAccessible(true); |
| 31 | + State state = (State) method.invoke(dposTask); |
| 32 | + |
| 33 | + Assert.assertEquals(State.BACKUP_IS_NOT_MASTER, state); |
| 34 | + |
| 35 | + |
| 36 | + Mockito.when(stateManager.getState()).thenReturn(State.OK); |
| 37 | + |
| 38 | + DposSlot dposSlot = mock(DposSlot.class); |
| 39 | + Mockito.when(dposSlot.getTime(1)).thenReturn(Long.MAX_VALUE); |
| 40 | + |
| 41 | + field = dposTask.getClass().getDeclaredField("dposSlot"); |
| 42 | + field.setAccessible(true); |
| 43 | + field.set(dposTask, dposSlot); |
| 44 | + |
| 45 | + |
| 46 | + Mockito.when(stateManager.getState()).thenReturn(State.OK); |
| 47 | + |
| 48 | + BlockHandle blockHandle = mock(BlockHandle.class); |
| 49 | + Mockito.when(blockHandle.getLock()).thenReturn(new Object()); |
| 50 | + |
| 51 | + |
| 52 | + DposService dposService = mock(DposService.class); |
| 53 | + Mockito.when(dposService.getBlockHandle()).thenReturn(blockHandle); |
| 54 | + |
| 55 | + field = dposTask.getClass().getDeclaredField("dposService"); |
| 56 | + field.setAccessible(true); |
| 57 | + field.set(dposTask, dposService); |
| 58 | + |
| 59 | + state = (State) method.invoke(dposTask); |
| 60 | + |
| 61 | + Assert.assertEquals(State.NOT_TIME_YET, state); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments