Skip to content

Commit

Permalink
Unit test initialization bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey authored and rigelrozanski committed Mar 1, 2018
1 parent d694dbe commit 34772f8
Showing 1 changed file with 53 additions and 9 deletions.
62 changes: 53 additions & 9 deletions server/start_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
package server

import (
"fmt"
"os"
"testing"
"time"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/mock"
"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)).
Expand All @@ -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
}
}

0 comments on commit 34772f8

Please sign in to comment.