Skip to content

Commit

Permalink
test: precedence of flag vs toml for abci client type
Browse files Browse the repository at this point in the history
Also more documentation for TOML template.
  • Loading branch information
JimLarson committed Apr 19, 2024
1 parent 5733916 commit 820bc69
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
3 changes: 3 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ type BaseConfig struct {
IAVLLazyLoading bool `mapstructure:"iavl-lazy-loading"`

// ABCIClientType selects the type of ABCI client.
// Valid settings are "committing" (default) or "local".
// The committing client allows greater query parallelism,
// but the local client is more defensive.
ABCIClientType string `mapstructure:"abci-client-type"`

// AppDBBackend defines the type of Database to use for the application and snapshots databases.
Expand Down
4 changes: 3 additions & 1 deletion server/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ iavl-disable-fastnode = {{ .BaseConfig.IAVLDisableFastNode }}
iavl-lazy-loading = {{ .BaseConfig.IAVLLazyLoading }}
# ABCIClientType selects the type of ABCI client.
# Default is "committing".
# Valid settings are "committing" (default) or "local".
# The committing client allows greater query parallelism,
# but the local client is more defensive.
abci-client-type = "{{ .BaseConfig.ABCIClientType }}"
# AppDBBackend defines the database backend type to use for the application and snapshots DBs.
Expand Down
88 changes: 88 additions & 0 deletions server/start_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package server

import (
"context"
"errors"
"fmt"
"os"
"path"
"reflect"
"runtime"
"testing"

"github.com/spf13/cobra"
tmcfg "github.com/tendermint/tendermint/config"
)

func TestAbciClientType(t *testing.T) {
Expand Down Expand Up @@ -44,3 +52,83 @@ func TestAbciClientType(t *testing.T) {
})
}
}

var errCancelledInPreRun = errors.New("cancelled in prerun")

func TestAbciClientPrecedence(t *testing.T) {
for i, tt := range []struct {
flag, toml, want string
}{
{
want: "committing",
},
{
flag: "foo",
want: "foo",
},
{
toml: "foo",
want: "foo",
},
{
flag: "foo",
toml: "bar",
want: "foo",
},
} {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
tempDir := t.TempDir()
err := os.Mkdir(path.Join(tempDir, "config"), os.ModePerm)
if err != nil {
t.Fatalf("creating config dir failed: %v", err)
}
appTomlPath := path.Join(tempDir, "config", "app.toml")

cmd := StartCmd(nil, tempDir)

if tt.flag != "" {
err = cmd.Flags().Set(FlagAbciClientType, tt.flag)
if err != nil {
t.Fatalf(`failed setting flag to "%s": %v`, tt.flag, err)
}
}

if tt.toml != "" {
writer, err := os.Create(appTomlPath)
if err != nil {
t.Fatalf(`failed creating "%s": %v`, appTomlPath, err)
}
_, err = writer.WriteString(fmt.Sprintf("%s = \"%s\"\n", FlagAbciClientType, tt.toml))
if err != nil {
t.Fatalf(`failed writing to app.toml: %v`, err)
}
err = writer.Close()
if err != nil {
t.Fatalf(`failed closing app.toml: %v`, err)
}
}

// Compare to tests in util_test.go
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
err := InterceptConfigsPreRunHandler(cmd, "", nil, tmcfg.DefaultConfig())
if err != nil {
return err
}
return errCancelledInPreRun
}

serverCtx := NewDefaultContext()
ctx := context.WithValue(context.Background(), ServerContextKey, serverCtx)
err = cmd.ExecuteContext(ctx)
if err != errCancelledInPreRun {
t.Fatal(err)
}

gotClientType := serverCtx.Viper.GetString(FlagAbciClientType)

if gotClientType != tt.want {
t.Errorf(`want client type "%s", got "%s"`, tt.want, gotClientType)
}
})
}
}

0 comments on commit 820bc69

Please sign in to comment.