github.com/iotexproject/iotex-core@v1.14.1-rc1/server/itx/server_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 TestStop(t *testing.T) { 21 require := require.New(t) 22 cfg, cleanupPath := newConfig(t) 23 defer cleanupPath() 24 svr, err := NewServer(cfg) 25 require.NoError(err) 26 ctx := context.Background() 27 err = svr.Start(ctx) 28 require.NoError(err) 29 err = testutil.WaitUntil(100*time.Millisecond, 3*time.Second, func() (bool, error) { 30 err = svr.Stop(ctx) 31 return err == nil, err 32 }) 33 require.NoError(err) 34 } 35 36 func TestNewSubChainService(t *testing.T) { 37 require := require.New(t) 38 cfg, cleanupPath := newConfig(t) 39 defer cleanupPath() 40 svr, err := NewServer(cfg) 41 require.NoError(err) 42 err = svr.NewSubChainService(cfg) 43 require.NoError(err) 44 cs := svr.ChainService(1) 45 require.NotNil(cs) 46 ctx := context.Background() 47 require.NoError(cs.Start(ctx)) 48 err = testutil.WaitUntil(100*time.Millisecond, 3*time.Second, func() (bool, error) { 49 err = svr.StopChainService(ctx, 1) 50 return err == nil, err 51 }) 52 require.NoError(err) 53 } 54 55 func TestStartServer(t *testing.T) { 56 require := require.New(t) 57 cfg, cleanupPath := newConfig(t) 58 defer cleanupPath() 59 svr, err := NewServer(cfg) 60 require.NoError(err) 61 probeSvr := probe.New(cfg.System.HTTPStatsPort) 62 ctx, cancel := context.WithCancel(context.Background()) 63 require.NoError(probeSvr.Start(ctx)) 64 go func() { 65 testutil.WaitUntil(100*time.Millisecond, 3*time.Second, func() (bool, error) { 66 cancel() 67 return true, nil 68 }) 69 }() 70 StartServer(ctx, svr, probeSvr, cfg) 71 } 72 73 func newConfig(t *testing.T) (config.Config, func()) { 74 require := require.New(t) 75 dbPath, err := testutil.PathOfTempFile("chain.db") 76 require.NoError(err) 77 triePath, err := testutil.PathOfTempFile("trie.db") 78 require.NoError(err) 79 indexPath, err := testutil.PathOfTempFile("indxer.db") 80 require.NoError(err) 81 contractIndexPath, err := testutil.PathOfTempFile("contractindxer.db") 82 require.NoError(err) 83 sgdPath, err := testutil.PathOfTempFile("sgdindex.db") 84 require.NoError(err) 85 cfg := config.Default 86 cfg.API.GRPCPort = testutil.RandomPort() 87 cfg.API.HTTPPort = testutil.RandomPort() 88 cfg.API.WebSocketPort = testutil.RandomPort() 89 cfg.Chain.ChainDBPath = dbPath 90 cfg.Chain.TrieDBPath = triePath 91 cfg.Chain.SGDIndexDBPath = sgdPath 92 cfg.Chain.TrieDBPatchFile = "" 93 cfg.Chain.ContractStakingIndexDBPath = contractIndexPath 94 return cfg, func() { 95 testutil.CleanupPath(dbPath) 96 testutil.CleanupPath(triePath) 97 testutil.CleanupPath(indexPath) 98 testutil.CleanupPath(contractIndexPath) 99 testutil.CleanupPath(sgdPath) 100 } 101 }