github.com/iotexproject/iotex-core@v1.14.1-rc1/server/itx/heartbeat_test.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package itx 7 8 import ( 9 "context" 10 "testing" 11 "time" 12 13 "github.com/stretchr/testify/require" 14 15 "github.com/iotexproject/iotex-core/config" 16 "github.com/iotexproject/iotex-core/pkg/probe" 17 "github.com/iotexproject/iotex-core/testutil" 18 ) 19 20 func TestNewHeartbeatHandler(t *testing.T) { 21 require := require.New(t) 22 23 dbPath, err := testutil.PathOfTempFile("chain.db") 24 require.NoError(err) 25 testutil.CleanupPath(dbPath) 26 triePath, err := testutil.PathOfTempFile("trie.db") 27 require.NoError(err) 28 testutil.CleanupPath(triePath) 29 indexPath, err := testutil.PathOfTempFile("index.db") 30 require.NoError(err) 31 sgdIndexPath, err := testutil.PathOfTempFile("sgdindex.db") 32 require.NoError(err) 33 defer func() { 34 testutil.CleanupPath(dbPath) 35 testutil.CleanupPath(triePath) 36 testutil.CleanupPath(indexPath) 37 testutil.CleanupPath(sgdIndexPath) 38 }() 39 cfg := config.Default 40 cfg.API.GRPCPort = testutil.RandomPort() 41 cfg.API.HTTPPort = testutil.RandomPort() 42 cfg.API.WebSocketPort = testutil.RandomPort() 43 cfg.Chain.ChainDBPath = dbPath 44 cfg.Chain.TrieDBPath = triePath 45 cfg.Chain.ContractStakingIndexDBPath = indexPath 46 cfg.Chain.SGDIndexDBPath = sgdIndexPath 47 cfg.Chain.TrieDBPatchFile = "" 48 s, err := NewServer(cfg) 49 cfg.Consensus.Scheme = config.RollDPoSScheme 50 cfg.Genesis.EnableGravityChainVoting = true 51 require.NoError(err) 52 require.NotNil(s) 53 handler := NewHeartbeatHandler(s, cfg.Network) 54 require.NotNil(handler) 55 require.Panics(func() { handler.Log() }, "P2pAgent is nil") 56 57 ctx, cancel := context.WithCancel(context.Background()) 58 livenessCtx, livenessCancel := context.WithCancel(context.Background()) 59 probeSvr := probe.New(cfg.System.HTTPStatsPort) 60 require.NoError(probeSvr.Start(ctx)) 61 require.NoError(s.Start(ctx)) 62 time.Sleep(time.Second * 2) 63 handler.Log() 64 cancel() 65 require.NoError(probeSvr.Stop(livenessCtx)) 66 livenessCancel() 67 }