Unit test initialization bug
This commit is contained in:
parent
d694dbe7c1
commit
34772f8b6e
|
@ -1,9 +1,12 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
@ -11,7 +14,29 @@ import (
|
||||||
"github.com/tendermint/tmlibs/log"
|
"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()()
|
defer setupViper()()
|
||||||
|
|
||||||
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).
|
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).
|
||||||
|
@ -21,16 +46,35 @@ func TestStart(t *testing.T) {
|
||||||
err := initCmd.RunE(nil, nil)
|
err := initCmd.RunE(nil, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// try to start up
|
|
||||||
// this should hang forever on success.... how to close???
|
|
||||||
|
|
||||||
rootDir := viper.GetString("home")
|
rootDir := viper.GetString("home")
|
||||||
app, err := mock.NewApp(logger, rootDir)
|
app, err := mock.NewApp(logger, rootDir)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
_ = StartCmd(app, logger)
|
|
||||||
// startCmd := StartCmd(app, logger)
|
|
||||||
|
|
||||||
// // TODO: test with tendermint
|
// set up app and start up
|
||||||
// err = startCmd.RunE(nil, nil)
|
viper.Set(flagWithTendermint, true)
|
||||||
// require.NoError(t, err)
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue