* added mutix in mocks to protect against data races

* defined mocks for the dao tests

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel 2024-04-05 12:28:45 +02:00
parent ed9a2779d0
commit 4303c3069d
No known key found for this signature in database
2 changed files with 11 additions and 3 deletions

View File

@ -15,14 +15,16 @@ import (
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
typesparams "github.com/cosmos/cosmos-sdk/x/params/types" typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
"github.com/planetmint/planetmint-go/monitor"
monitormocks "github.com/planetmint/planetmint-go/monitor/mocks"
"github.com/planetmint/planetmint-go/x/dao/keeper" "github.com/planetmint/planetmint-go/x/dao/keeper"
daotestutil "github.com/planetmint/planetmint-go/x/dao/testutil"
"github.com/planetmint/planetmint-go/x/dao/types" "github.com/planetmint/planetmint-go/x/dao/types"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
daotestutil "github.com/planetmint/planetmint-go/x/dao/testutil"
) )
func DaoKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) { func DaoKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
monitor.MqttMonitorInstance = &monitormocks.MockMQTTMonitorClientI{}
storeKey := sdk.NewKVStoreKey(types.StoreKey) storeKey := sdk.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey) memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)
challengeStoreKey := storetypes.NewMemoryStoreKey(types.ChallengeKey) challengeStoreKey := storetypes.NewMemoryStoreKey(types.ChallengeKey)

View File

@ -16,6 +16,7 @@ type MockMQTTClient struct {
UnsubscribeFunc func(topics ...string) mqtt.Token UnsubscribeFunc func(topics ...string) mqtt.Token
IsConnectedFunc func() bool IsConnectedFunc func() bool
connected bool connected bool
connectedMutex sync.Mutex
} }
// GetConnectFunc fetches the mock client's `Connect` func // GetConnectFunc fetches the mock client's `Connect` func
@ -98,7 +99,9 @@ func GetUnsubscribeFunc(_ ...string) mqtt.Token {
// Connect is the mock client's `Disconnect` func // Connect is the mock client's `Disconnect` func
func (m *MockMQTTClient) Connect() mqtt.Token { func (m *MockMQTTClient) Connect() mqtt.Token {
m.connectedMutex.Lock()
m.connected = true m.connected = true
m.connectedMutex.Unlock()
return GetConnectFunc() return GetConnectFunc()
} }
@ -121,5 +124,8 @@ func (m *MockMQTTClient) Unsubscribe(topics ...string) mqtt.Token {
} }
func (m *MockMQTTClient) IsConnected() bool { func (m *MockMQTTClient) IsConnected() bool {
return m.connected m.connectedMutex.Lock()
connected := m.connected
m.connectedMutex.Unlock()
return connected
} }