Skip to content

Commit

Permalink
fix: fix race conditions from #14332 (backport #14402) (#14421)
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Dec 26, 2022
1 parent c727ee7 commit c30a33a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
11 changes: 4 additions & 7 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ test-integration:
test-integration-cov:
go test ./integration/... -timeout 30m -coverpkg=../... -coverprofile=integration-profile.out -covermode=atomic

test-e2e: test-e2e-server
go test ./e2e/... -mod=readonly -timeout 30m -tags='e2e'
test-e2e:
go test ./e2e/... -mod=readonly -timeout 30m -race -tags='e2e'

test-e2e-cov: test-e2e-server
go test ./e2e/... -mod=readonly -timeout 30m -race -tags='e2e' -coverpkg=../... -coverprofile=e2e-profile.out -covermode=atomic

test-e2e-server:
go test ./e2e/server -mod=readonly -timeout 30m -tags='e2e' -coverpkg=../... -coverprofile=e2e-server-profile.out -covermode=atomic
test-e2e-cov:
go test ./e2e/... -mod=readonly -timeout 30m -race -tags='e2e' -coverpkg=../... -coverprofile=e2e-profile.out -covermode=atomic
4 changes: 2 additions & 2 deletions tests/e2e/server/export_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !race
// +build !race
//go:build e2e
// +build e2e

package server_test

Expand Down
20 changes: 8 additions & 12 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,6 @@ func (m *Manager) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) map[string

// ExportGenesisForModules performs export genesis functionality for modules
func (m *Manager) ExportGenesisForModules(ctx sdk.Context, cdc codec.JSONCodec, modulesToExport []string) map[string]json.RawMessage {
genesisData := make(map[string]json.RawMessage)

if len(modulesToExport) == 0 {
modulesToExport = m.OrderExportGenesis
}
Expand All @@ -393,22 +391,20 @@ func (m *Manager) ExportGenesisForModules(ctx sdk.Context, cdc codec.JSONCodec,
panic(err)
}

channels := make([]chan json.RawMessage, len(modulesToExport))
modulesWithGenesis := make([]string, 0, len(modulesToExport))

for i, moduleName := range modulesToExport {
channels := make(map[string]chan json.RawMessage)
for _, moduleName := range modulesToExport {
if module, ok := m.Modules[moduleName].(HasGenesis); ok {
channels[i] = make(chan json.RawMessage)
modulesWithGenesis = append(modulesWithGenesis, moduleName)

channels[moduleName] = make(chan json.RawMessage)
go func(module HasGenesis, ch chan json.RawMessage) {
ctx := ctx.WithGasMeter(sdk.NewInfiniteGasMeter()) // avoid race conditions
ch <- module.ExportGenesis(ctx, cdc)
}(module, channels[i])
}(module, channels[moduleName])
}
}

for i, moduleName := range modulesWithGenesis {
genesisData[moduleName] = <-channels[i]
genesisData := make(map[string]json.RawMessage)
for moduleName := range channels {
genesisData[moduleName] = <-channels[moduleName]
}

return genesisData
Expand Down
4 changes: 3 additions & 1 deletion types/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestManager_ExportGenesis(t *testing.T) {
require.NotNil(t, mm)
require.Equal(t, 2, len(mm.Modules))

ctx := sdk.Context{}
ctx := sdk.NewContext(nil, tmproto.Header{}, false, log.NewNopLogger())
interfaceRegistry := types.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
mockAppModule1.EXPECT().ExportGenesis(gomock.Eq(ctx), gomock.Eq(cdc)).AnyTimes().Return(json.RawMessage(`{"key1": "value1"}`))
Expand All @@ -200,6 +200,8 @@ func TestManager_ExportGenesis(t *testing.T) {
}
require.Equal(t, want, mm.ExportGenesis(ctx, cdc))
require.Equal(t, want, mm.ExportGenesisForModules(ctx, cdc, []string{}))
require.Equal(t, map[string]json.RawMessage{"module1": json.RawMessage(`{"key1": "value1"}`)}, mm.ExportGenesisForModules(ctx, cdc, []string{"module1"}))
require.NotEqual(t, map[string]json.RawMessage{"module1": json.RawMessage(`{"key1": "value1"}`)}, mm.ExportGenesisForModules(ctx, cdc, []string{"module2"}))

require.Panics(t, func() {
mm.ExportGenesisForModules(ctx, cdc, []string{"module1", "modulefoo"})
Expand Down

0 comments on commit c30a33a

Please sign in to comment.