|
| 1 | +package org.tron.core.net.services; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.lang.reflect.Field; |
| 5 | +import java.net.InetSocketAddress; |
| 6 | +import org.junit.After; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Before; |
| 9 | +import org.junit.Test; |
| 10 | +import org.tron.common.application.TronApplicationContext; |
| 11 | +import org.tron.common.utils.FileUtil; |
| 12 | +import org.tron.common.utils.ReflectUtils; |
| 13 | +import org.tron.core.Constant; |
| 14 | +import org.tron.core.config.DefaultConfig; |
| 15 | +import org.tron.core.config.args.Args; |
| 16 | +import org.tron.core.net.P2pEventHandlerImpl; |
| 17 | +import org.tron.core.net.peer.PeerConnection; |
| 18 | +import org.tron.core.net.peer.TronState; |
| 19 | +import org.tron.core.net.service.sync.SyncService; |
| 20 | +import org.tron.p2p.connection.Channel; |
| 21 | + |
| 22 | + |
| 23 | +public class SyncServiceTest { |
| 24 | + protected TronApplicationContext context; |
| 25 | + private SyncService service; |
| 26 | + private PeerConnection peer; |
| 27 | + private P2pEventHandlerImpl p2pEventHandler; |
| 28 | + private String dbPath = "output-sync-service-test"; |
| 29 | + |
| 30 | + /** |
| 31 | + * init context. |
| 32 | + */ |
| 33 | + @Before |
| 34 | + public void init() { |
| 35 | + Args.setParam(new String[]{"--output-directory", dbPath, "--debug"}, |
| 36 | + Constant.TEST_CONF); |
| 37 | + context = new TronApplicationContext(DefaultConfig.class); |
| 38 | + service = context.getBean(SyncService.class); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * destroy. |
| 43 | + */ |
| 44 | + @After |
| 45 | + public void destroy() { |
| 46 | + Args.clearParam(); |
| 47 | + FileUtil.deleteDir(new File(dbPath)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void test() { |
| 52 | + try { |
| 53 | + ReflectUtils.setFieldValue(service, "fetchFlag", true); |
| 54 | + ReflectUtils.setFieldValue(service, "handleFlag", true); |
| 55 | + service.init(); |
| 56 | + Assert.assertTrue((boolean) ReflectUtils.getFieldObject(service, "fetchFlag")); |
| 57 | + Assert.assertTrue((boolean) ReflectUtils.getFieldObject(service, "handleFlag")); |
| 58 | + peer = context.getBean(PeerConnection.class); |
| 59 | + Assert.assertNull(peer.getSyncChainRequested()); |
| 60 | + Channel c1 = new Channel(); |
| 61 | + InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001); |
| 62 | + Field field = c1.getClass().getDeclaredField("inetSocketAddress"); |
| 63 | + field.setAccessible(true); |
| 64 | + field.set(c1, a1.getAddress()); |
| 65 | + peer.setChannel(c1); |
| 66 | + service.startSync(peer); |
| 67 | + ReflectUtils.setFieldValue(peer, "tronState", TronState.SYNCING); |
| 68 | + service.startSync(peer); |
| 69 | + } catch (Exception e) { |
| 70 | + // no need to deal with |
| 71 | + } |
| 72 | + service.close(); |
| 73 | + } |
| 74 | +} |
0 commit comments