From 1e5a7993ed6cb73c4b903f9c010d711b47f76611 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Thu, 19 Jul 2018 08:40:46 +0200 Subject: [PATCH] Environment variables => flags --- Makefile | 10 +++---- cmd/gaia/app/sim_test.go | 65 +++++++++++----------------------------- 2 files changed, 23 insertions(+), 52 deletions(-) diff --git a/Makefile b/Makefile index 9cba9cb61..6459f08e8 100644 --- a/Makefile +++ b/Makefile @@ -132,11 +132,11 @@ test_sim: @echo "Running individual module simulations." @go test $(PACKAGES_SIMTEST) -v @echo "Running full Gaia simulation. This may take several minutes." - @echo "Set the environment variable 'GAIA_SIMULATION_SEED' to run with a constant seed." - @echo "Set the environment variable 'GAIA_SIMULATION_KEYS' to run with the specified number of keys." - @echo "Set the environment variable 'GAIA_SIMULATION_BLOCKS' to run with the specified number of blocks." - @echo "Set the environment variable 'GAIA_SIMULATION_BLOCK_SIZE' to run with the specified block size (operations per block)." - @GAIA_SIMULATION_ENABLED=1 go test ./cmd/gaia/app -run TestFullGaiaSimulation -v + @echo "Pass the flag 'SimulationSeed' to run with a constant seed." + @echo "Pass the flag 'SimulationNumKeys' to run with the specified number of keys." + @echo "Pass the flag 'SimulationNumBlocks' to run with the specified number of blocks." + @echo "Pass the flag 'SimulationBlockSize' to run with the specified block size (operations per block)." + @go test ./cmd/gaia/app -run TestFullGaiaSimulation -SimulationEnabled=true -SimulationBlockSize=200 -v test_cover: @bash tests/test_cover.sh diff --git a/cmd/gaia/app/sim_test.go b/cmd/gaia/app/sim_test.go index 89b1b54aa..f0bea1e17 100644 --- a/cmd/gaia/app/sim_test.go +++ b/cmd/gaia/app/sim_test.go @@ -2,11 +2,9 @@ package app import ( "encoding/json" + "flag" "math/rand" - "os" - "strconv" "testing" - "time" "github.com/stretchr/testify/require" @@ -20,18 +18,22 @@ import ( stakesim "github.com/cosmos/cosmos-sdk/x/stake/simulation" ) -const ( - defaultNumKeys = 10 - defaultNumBlocks = 100 - defaultBlockSize = 100 - - simulationEnvEnable = "GAIA_SIMULATION_ENABLED" - simulationEnvSeed = "GAIA_SIMULATION_SEED" - simulationEnvKeys = "GAIA_SIMULATION_KEYS" - simulationEnvBlocks = "GAIA_SIMULATION_BLOCKS" - simulationEnvBlockSize = "GAIA_SIMULATION_BLOCK_SIZE" +var ( + seed int64 + numKeys int + numBlocks int + blockSize int + enabled bool ) +func init() { + flag.Int64Var(&seed, "SimulationSeed", 42, "Simulation random seed") + flag.IntVar(&numKeys, "SimulationNumKeys", 10, "Number of keys (accounts)") + flag.IntVar(&numBlocks, "SimulationNumBlocks", 100, "Number of blocks") + flag.IntVar(&blockSize, "SimulationBlockSize", 100, "Operations per block") + flag.BoolVar(&enabled, "SimulationEnabled", false, "Enable the simulation") +} + func appStateFn(r *rand.Rand, accs []sdk.AccAddress) json.RawMessage { var genesisAccounts []GenesisAccount @@ -62,7 +64,7 @@ func appStateFn(r *rand.Rand, accs []sdk.AccAddress) json.RawMessage { } func TestFullGaiaSimulation(t *testing.T) { - if os.Getenv(simulationEnvEnable) == "" { + if !enabled { t.Skip("Skipping Gaia simulation") } @@ -72,37 +74,6 @@ func TestFullGaiaSimulation(t *testing.T) { app := NewGaiaApp(logger, db, nil) require.Equal(t, "GaiaApp", app.Name()) - var seed int64 - var err error - envSeed := os.Getenv(simulationEnvSeed) - if envSeed != "" { - seed, err = strconv.ParseInt(envSeed, 10, 64) - require.Nil(t, err) - } else { - seed = time.Now().UnixNano() - } - - keys := defaultNumKeys - envKeys := os.Getenv(simulationEnvKeys) - if envKeys != "" { - keys, err = strconv.Atoi(envKeys) - require.Nil(t, err) - } - - blocks := defaultNumBlocks - envBlocks := os.Getenv(simulationEnvBlocks) - if envBlocks != "" { - blocks, err = strconv.Atoi(envBlocks) - require.Nil(t, err) - } - - blockSize := defaultBlockSize - envBlockSize := os.Getenv(simulationEnvBlockSize) - if envBlockSize != "" { - blockSize, err = strconv.Atoi(envBlockSize) - require.Nil(t, err) - } - // Run randomized simulation simulation.SimulateFromSeed( t, app.BaseApp, appStateFn, seed, @@ -121,8 +92,8 @@ func TestFullGaiaSimulation(t *testing.T) { banksim.NonnegativeBalanceInvariant(app.accountMapper), stakesim.AllInvariants(app.coinKeeper, app.stakeKeeper, app.accountMapper), }, - keys, - blocks, + numKeys, + numBlocks, blockSize, )