diff --git a/server/start_test.go b/server/start_test.go index 9756e59324..de66b8203c 100644 --- a/server/start_test.go +++ b/server/start_test.go @@ -1,9 +1,12 @@ package server import ( + "fmt" "os" "testing" + "time" + "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/stretchr/testify/require" @@ -11,7 +14,29 @@ import ( "github.com/tendermint/tmlibs/log" ) -func TestStart(t *testing.T) { +func TestStartStandAlone(t *testing.T) { + defer setupViper()() + + logger := log.NewNopLogger() + initCmd := InitCmd(mock.GenInitOptions, logger) + err := initCmd.RunE(nil, nil) + require.NoError(t, err) + + rootDir := viper.GetString("home") + app, err := mock.NewApp(logger, rootDir) + require.NoError(t, err) + + // set up app and start up + viper.Set(flagWithTendermint, false) + viper.Set(flagAddress, "localhost:11122") + startCmd := StartCmd(app, logger) + timeout := time.Duration(3) * time.Second + + err = runOrTimeout(startCmd, timeout) + require.NoError(t, err) +} + +func TestStartWithTendermint(t *testing.T) { defer setupViper()() logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)). @@ -21,16 +46,35 @@ func TestStart(t *testing.T) { err := initCmd.RunE(nil, nil) require.NoError(t, err) - // try to start up - // this should hang forever on success.... how to close??? - rootDir := viper.GetString("home") app, err := mock.NewApp(logger, rootDir) require.NoError(t, err) - _ = StartCmd(app, logger) - // startCmd := StartCmd(app, logger) - // // TODO: test with tendermint - // err = startCmd.RunE(nil, nil) - // require.NoError(t, err) + // set up app and start up + viper.Set(flagWithTendermint, true) + startCmd := StartCmd(app, logger) + timeout := time.Duration(3) * time.Second + + err = runOrTimeout(startCmd, timeout) + require.NoError(t, err) +} + +func runOrTimeout(cmd *cobra.Command, timeout time.Duration) error { + done := make(chan error) + go func(out chan<- error) { + // this should NOT exit + err := cmd.RunE(nil, nil) + if err != nil { + out <- err + } + out <- fmt.Errorf("start died for unknown reasons") + }(done) + timer := time.NewTimer(timeout) + + select { + case err := <-done: + return err + case <-timer.C: + return nil + } }