Skip to content

Commit

Permalink
Add registry test
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Jul 24, 2024
1 parent 05e31ec commit e90a906
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
7 changes: 6 additions & 1 deletion simsx/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ type tuple struct {
}

// runWithFailFast runs the factory method on a separate goroutine to abort early when the context is canceled via reporter skip
func runWithFailFast(ctx context.Context, data *ChainDataSource, reporter SimulationReporter, f FactoryMethod) (signer []SimAccount, msg sdk.Msg) {
func runWithFailFast(
ctx context.Context,
data *ChainDataSource,
reporter SimulationReporter,
f FactoryMethod,
) (signer []SimAccount, msg sdk.Msg) {
r := make(chan tuple)
go func() {
defer recoverPanicForSkipped(reporter, r)
Expand Down
43 changes: 43 additions & 0 deletions simsx/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,46 @@ func TestSimsProposalRegistryAdapter(t *testing.T) {
})
}
}

func TestRunWithFailFast(t *testing.T) {
myTestMsg := testdata.NewTestMsg()
mySigners := []SimAccount{SimAccountFixture()}
specs := map[string]struct {
factory FactoryMethod
expSigners []SimAccount
expMsg sdk.Msg
expSkipped bool
}{
"factory completes": {
factory: func(ctx context.Context, _ *ChainDataSource, reporter SimulationReporter) ([]SimAccount, sdk.Msg) {
return mySigners, myTestMsg
},
expSigners: mySigners,
expMsg: myTestMsg,
},
"factory skips": {
factory: func(ctx context.Context, _ *ChainDataSource, reporter SimulationReporter) ([]SimAccount, sdk.Msg) {
reporter.Skip("testing")
return nil, nil
},
expSkipped: true,
},
"factory skips and panics": {
factory: func(ctx context.Context, _ *ChainDataSource, reporter SimulationReporter) ([]SimAccount, sdk.Msg) {
reporter.Skip("testing")
panic("should be handled")
},
expSkipped: true,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
ctx, done := context.WithCancel(context.Background())
reporter := NewBasicSimulationReporter().WithScope(&testdata.TestMsg{}, SkipHookFn(func(...any) { done() }))
gotSigners, gotMsg := runWithFailFast(ctx, nil, reporter, spec.factory)
assert.Equal(t, spec.expSigners, gotSigners)
assert.Equal(t, spec.expMsg, gotMsg)
assert.Equal(t, spec.expSkipped, reporter.IsSkipped())
})
}
}

0 comments on commit e90a906

Please sign in to comment.