Skip to content

Commit

Permalink
Clear future ops
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Jul 16, 2024
1 parent c351f4f commit 02ff2d5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
12 changes: 5 additions & 7 deletions x/simulation/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func SimulateFromSeedX(
eventStats.Tally, config.Lean, config.ChainID,
)
numQueuedTimeOpsRan, timeFutureOps := runQueuedTimeOperations(tb,
timeOperationQueue, int(blockHeight), blockTime,
&timeOperationQueue, int(blockHeight), blockTime,
r, app, ctx, accs, logWriter, eventStats.Tally,
config.Lean, config.ChainID,
)
Expand Down Expand Up @@ -390,19 +390,17 @@ func runQueuedOperations(tb testing.TB, queueOps map[int][]simulation.Operation,
return numOpsRan, allFutureOps
}

func runQueuedTimeOperations(tb testing.TB, queueOps []simulation.FutureOperation,
func runQueuedTimeOperations(tb testing.TB, queueOps *[]simulation.FutureOperation,
height int, currentTime time.Time, r *rand.Rand,
app *baseapp.BaseApp, ctx sdk.Context, accounts []simulation.Account,
logWriter LogWriter, event func(route, op, evResult string),
lean bool, chainID string,
) (numOpsRan int, allFutureOps []simulation.FutureOperation) {
tb.Helper()
// Keep all future operations
allFutureOps = make([]simulation.FutureOperation, 0)

numOpsRan = 0
for len(queueOps) > 0 && currentTime.After(queueOps[0].BlockTime) {
opMsg, futureOps, err := queueOps[0].Op(r, app, ctx, accounts, chainID)
for len(*queueOps) > 0 && currentTime.After((*queueOps)[0].BlockTime) {
opMsg, futureOps, err := (*queueOps)[0].Op(r, app, ctx, accounts, chainID)

opMsg.LogEvent(event)

Expand All @@ -419,7 +417,7 @@ func runQueuedTimeOperations(tb testing.TB, queueOps []simulation.FutureOperatio
allFutureOps = append(allFutureOps, futureOps...)
}

queueOps = queueOps[1:]
*queueOps = slices.Delete(*queueOps, 0, 1)
numOpsRan++
}

Expand Down
56 changes: 56 additions & 0 deletions x/simulation/simulate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package simulation

import (
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"math/rand"
"testing"
"time"
)

func TestRunQueuedTimeOperations(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
ctx := sdk.Context{}
lw := NewLogWriter(true)
noopEvent := func(route, op, evResult string) {}
var acc []simulation.Account
noOp := simulation.FutureOperation{
Op: func(gotR *rand.Rand, gotApp *baseapp.BaseApp, ctx sdk.Context, accounts []simulation.Account, chainID string) (OperationMsg simulation.OperationMsg, futureOps []simulation.FutureOperation, err error) {
return simulation.OperationMsg{}, nil, nil
},
}
futureOp := simulation.FutureOperation{
Op: func(gotR *rand.Rand, gotApp *baseapp.BaseApp, ctx sdk.Context, accounts []simulation.Account, chainID string) (OperationMsg simulation.OperationMsg, futureOps []simulation.FutureOperation, err error) {
return simulation.OperationMsg{}, []simulation.FutureOperation{noOp}, nil
},
}

specs := map[string]struct {
queueOps []simulation.FutureOperation
expOps []simulation.FutureOperation
}{
"empty": {},
"single": {
queueOps: []simulation.FutureOperation{noOp},
},
"multi": {
queueOps: []simulation.FutureOperation{noOp, noOp},
},
"future op": {
queueOps: []simulation.FutureOperation{futureOp},
expOps: []simulation.FutureOperation{noOp},
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
expOps := len(spec.queueOps)
n, fOps := runQueuedTimeOperations(t, &spec.queueOps, 0, time.Now(), r, nil, ctx, acc, lw, noopEvent, false, "testing")
require.Equal(t, expOps, n)
assert.Empty(t, spec.queueOps)
assert.Equal(t, spec.expOps, fOps)
})
}
}

0 comments on commit 02ff2d5

Please sign in to comment.