Skip to content

Commit

Permalink
fix defer functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Oct 23, 2024
1 parent 643490d commit 064e05e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
7 changes: 6 additions & 1 deletion app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ func (app *App) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []str
counter := int16(0)

for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(iter.Key()[2:])
key := iter.Key()
if len(key) <= 2 {
app.Logger().Error("unexpected key in staking store", "key", key)
continue
}
addr := sdk.ValAddress(key[2:])
validator, found := app.StakingKeeper.GetValidator(ctx, addr)
if !found {
panic("expected validator, not found")
Expand Down
10 changes: 5 additions & 5 deletions docs/development/SIMULATION_TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ make test-sim-full-app
### Import Export simulation test
The import export simulation test runs a full application simulation
and exports the application state at the end of the run.
This state is then then imported into a new simulation.
This state is then imported into a new simulation.
At the end of the run we compare the keys for the application state for both the simulations
to make sure they are the same.
```bash
Expand All @@ -32,15 +32,15 @@ make test-sim-import-export
### Import and run simulation test
This simulation test exports the application state at the end of the run and imports it into a new simulation.
```bash
make test-sim-import-export
make test-sim-after-import
```
### Multi seed long test
Multi seed long test runs a full application simulation with multiple seeds and multiple blocks.This runs the test for a longer duration compared to the multi seed short test
```bash
make test-sim-multi-seed-long
```
### Multi seed short test
Multi seed short test runs a full application simulation with multiple seeds and multiple blocks. This runs the test for a longer duration compared to the multi seed long test
Multi seed short test runs a full application simulation with multiple seeds and multiple blocks. This runs the test for a shorter duration compared to the multi seed long test
```bash
make test-sim-multi-seed-short
```
Expand All @@ -49,9 +49,9 @@ This test runs the import export simulation test for a longer duration.It uses t
```bash
make test-sim-import-export-long
```
### Import and run simulation test
### Import and run simulation test long
This test runs the import and run simulation test for a longer duration. It uses the runsim tool to run the same test in parallel threads
```bash
make test-sim-after-import-longg
make test-sim-after-import-long
```

6 changes: 3 additions & 3 deletions tests/simulation/sim/sim_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ func CheckExportSimulation(app runtime.AppI, config simtypes.Config, params simt
if config.ExportStatePath != "" {
exported, err := app.ExportAppStateAndValidators(false, nil, nil)
if err != nil {
return err
return fmt.Errorf("failed to export app state: %w", err)
}

Check warning on line 81 in tests/simulation/sim/sim_utils.go

View check run for this annotation

Codecov / codecov/patch

tests/simulation/sim/sim_utils.go#L76-L81

Added lines #L76 - L81 were not covered by tests

if err := os.WriteFile(config.ExportStatePath, []byte(exported.AppState), 0o600); err != nil {
if err := os.WriteFile(config.ExportStatePath, exported.AppState, 0o600); err != nil {
return err
}

Check warning on line 85 in tests/simulation/sim/sim_utils.go

View check run for this annotation

Codecov / codecov/patch

tests/simulation/sim/sim_utils.go#L83-L85

Added lines #L83 - L85 were not covered by tests
}

if config.ExportParamsPath != "" {
paramsBz, err := json.MarshalIndent(params, "", " ")
if err != nil {
return err
return fmt.Errorf("failed to write app state to %s: %w", config.ExportStatePath, err)
}

Check warning on line 92 in tests/simulation/sim/sim_utils.go

View check run for this annotation

Codecov / codecov/patch

tests/simulation/sim/sim_utils.go#L88-L92

Added lines #L88 - L92 were not covered by tests

if err := os.WriteFile(config.ExportParamsPath, paramsBz, 0o600); err != nil {
Expand Down
40 changes: 30 additions & 10 deletions tests/simulation/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,12 @@ func TestFullAppSimulation(t *testing.T) {
require.NoError(t, err, "simulation setup failed")

defer func() {
require.NoError(t, db.Close())
require.NoError(t, os.RemoveAll(dir))
if err := db.Close(); err != nil {
t.Errorf("Error closing new database: %v", err)
}
if err := os.RemoveAll(dir); err != nil {
t.Errorf("Error removing directory %s: %v", dir, err)
}
}()
appOptions := make(cosmossimutils.AppOptionsMap, 0)
appOptions[server.FlagInvCheckPeriod] = simutils.FlagPeriodValue
Expand Down Expand Up @@ -256,8 +260,12 @@ func TestAppImportExport(t *testing.T) {
require.NoError(t, err, "simulation setup failed")

defer func() {
require.NoError(t, db.Close())
require.NoError(t, os.RemoveAll(dir))
if err := db.Close(); err != nil {
t.Errorf("Error closing new database: %v", err)
}
if err := os.RemoveAll(dir); err != nil {
t.Errorf("Error removing directory %s: %v", dir, err)
}
}()

appOptions := make(cosmossimutils.AppOptionsMap, 0)
Expand Down Expand Up @@ -307,8 +315,12 @@ func TestAppImportExport(t *testing.T) {
require.NoError(t, err, "simulation setup failed")

defer func() {
require.NoError(t, newDB.Close())
require.NoError(t, os.RemoveAll(newDir))
if err := newDB.Close(); err != nil {
t.Errorf("Error closing new database: %v", err)
}
if err := os.RemoveAll(newDir); err != nil {
t.Errorf("Error removing directory %s: %v", newDir, err)
}
}()

newSimApp, err := simutils.NewSimApp(
Expand Down Expand Up @@ -410,8 +422,12 @@ func TestAppSimulationAfterImport(t *testing.T) {
require.NoError(t, err, "simulation setup failed")

defer func() {
require.NoError(t, db.Close())
require.NoError(t, os.RemoveAll(dir))
if err := db.Close(); err != nil {
t.Errorf("Error closing new database: %v", err)
}
if err := os.RemoveAll(dir); err != nil {
t.Errorf("Error removing directory %s: %v", dir, err)
}
}()

appOptions := make(cosmossimutils.AppOptionsMap, 0)
Expand Down Expand Up @@ -468,8 +484,12 @@ func TestAppSimulationAfterImport(t *testing.T) {
require.NoError(t, err, "simulation setup failed")

defer func() {
require.NoError(t, newDB.Close())
require.NoError(t, os.RemoveAll(newDir))
if err := newDB.Close(); err != nil {
t.Errorf("Error closing new database: %v", err)
}
if err := os.RemoveAll(newDir); err != nil {
t.Errorf("Error removing directory %s: %v", newDir, err)
}
}()

newSimApp, err := simutils.NewSimApp(
Expand Down

0 comments on commit 064e05e

Please sign in to comment.