mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-11-24 14:35:47 +00:00
* created a MockMqttMonitor interface and mock object
* used this to pass tests * made the MockMqttMonitor a global object so that it can be easily mocked * removed MockMqttMonitor from the app/keeper initialization * adjusted test cases to register "active machines" to the mqttmonitor Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
parent
7e2684d9e0
commit
b7fdd59466
17
app/app.go
17
app/app.go
@ -108,9 +108,7 @@ import (
|
|||||||
solomachine "github.com/cosmos/ibc-go/v7/modules/light-clients/06-solomachine"
|
solomachine "github.com/cosmos/ibc-go/v7/modules/light-clients/06-solomachine"
|
||||||
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
|
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
"github.com/syndtr/goleveldb/leveldb"
|
|
||||||
|
|
||||||
"github.com/planetmint/planetmint-go/monitor"
|
|
||||||
machinemodule "github.com/planetmint/planetmint-go/x/machine"
|
machinemodule "github.com/planetmint/planetmint-go/x/machine"
|
||||||
machinemodulekeeper "github.com/planetmint/planetmint-go/x/machine/keeper"
|
machinemodulekeeper "github.com/planetmint/planetmint-go/x/machine/keeper"
|
||||||
machinemoduletypes "github.com/planetmint/planetmint-go/x/machine/types"
|
machinemoduletypes "github.com/planetmint/planetmint-go/x/machine/types"
|
||||||
@ -126,7 +124,6 @@ import (
|
|||||||
// this line is used by starport scaffolding # stargate/app/moduleImport
|
// this line is used by starport scaffolding # stargate/app/moduleImport
|
||||||
|
|
||||||
pmante "github.com/planetmint/planetmint-go/app/ante"
|
pmante "github.com/planetmint/planetmint-go/app/ante"
|
||||||
plmntconfig "github.com/planetmint/planetmint-go/config"
|
|
||||||
"github.com/planetmint/planetmint-go/docs"
|
"github.com/planetmint/planetmint-go/docs"
|
||||||
appparams "github.com/planetmint/planetmint-go/lib/params"
|
appparams "github.com/planetmint/planetmint-go/lib/params"
|
||||||
)
|
)
|
||||||
@ -272,8 +269,7 @@ type App struct {
|
|||||||
// this line is used by starport scaffolding # stargate/app/keeperDeclaration
|
// this line is used by starport scaffolding # stargate/app/keeperDeclaration
|
||||||
|
|
||||||
// mm is the module manager
|
// mm is the module manager
|
||||||
mm *module.Manager
|
mm *module.Manager
|
||||||
mqttMonitor *monitor.MqttMonitor
|
|
||||||
|
|
||||||
// sm is the simulation manager
|
// sm is the simulation manager
|
||||||
sm *module.SimulationManager
|
sm *module.SimulationManager
|
||||||
@ -336,16 +332,6 @@ func New(
|
|||||||
tkeys: tkeys,
|
tkeys: tkeys,
|
||||||
memKeys: memKeys,
|
memKeys: memKeys,
|
||||||
}
|
}
|
||||||
|
|
||||||
aciveActorsDB, err := leveldb.OpenFile(homePath+"/activeActors.db", nil)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
app.mqttMonitor = monitor.NewMqttMonitorService(aciveActorsDB, *plmntconfig.GetConfig())
|
|
||||||
err = app.mqttMonitor.Start()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
app.ParamsKeeper = initParamsKeeper(
|
app.ParamsKeeper = initParamsKeeper(
|
||||||
appCodec,
|
appCodec,
|
||||||
cdc,
|
cdc,
|
||||||
@ -591,7 +577,6 @@ func New(
|
|||||||
app.MachineKeeper,
|
app.MachineKeeper,
|
||||||
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
|
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
|
||||||
homePath,
|
homePath,
|
||||||
app.mqttMonitor,
|
|
||||||
)
|
)
|
||||||
daoModule := daomodule.NewAppModule(appCodec, app.DaoKeeper, app.AccountKeeper, app.BankKeeper)
|
daoModule := daomodule.NewAppModule(appCodec, app.DaoKeeper, app.AccountKeeper, app.BankKeeper)
|
||||||
|
|
||||||
|
|||||||
34
monitor/interface.go
Normal file
34
monitor/interface.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package monitor
|
||||||
|
|
||||||
|
import (
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/planetmint/planetmint-go/config"
|
||||||
|
"github.com/syndtr/goleveldb/leveldb"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MQTTMonitorClientI interface {
|
||||||
|
AddParticipant(address string, lastSeenTS int64) (err error)
|
||||||
|
SelectPoPParticipantsOutOfActiveActors() (challenger string, challengee string, err error)
|
||||||
|
SetContext(ctx sdk.Context)
|
||||||
|
Start() (err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
var MqttMonitorInstance MQTTMonitorClientI
|
||||||
|
|
||||||
|
func LazyMqttMonitorLoader(homeDir string) {
|
||||||
|
if MqttMonitorInstance != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if homeDir == "" {
|
||||||
|
homeDir = "./"
|
||||||
|
}
|
||||||
|
aciveActorsDB, err := leveldb.OpenFile(homeDir+"activeActors.db", nil)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
MqttMonitorInstance = NewMqttMonitorService(aciveActorsDB, *config.GetConfig())
|
||||||
|
err = MqttMonitorInstance.Start()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
38
monitor/mocks/mqtt_monitor.go
Normal file
38
monitor/mocks/mqtt_monitor.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package mocks
|
||||||
|
|
||||||
|
import (
|
||||||
|
types "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MockMQTTMonitorClientI is a mock of MQTTMonitorClientI interface.
|
||||||
|
type MockMQTTMonitorClientI struct {
|
||||||
|
myStringList []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddParticipant mocks base method.
|
||||||
|
func (m *MockMQTTMonitorClientI) AddParticipant(address string, lastSeenTS int64) error {
|
||||||
|
m.myStringList = append(m.myStringList, address)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SelectPoPParticipantsOutOfActiveActors mocks base method.
|
||||||
|
func (m *MockMQTTMonitorClientI) SelectPoPParticipantsOutOfActiveActors() (string, string, error) {
|
||||||
|
var challenger, challengee string
|
||||||
|
amount := len(m.myStringList)
|
||||||
|
if amount >= 2 {
|
||||||
|
challenger = m.myStringList[amount-2]
|
||||||
|
challengee = m.myStringList[amount-1]
|
||||||
|
}
|
||||||
|
return challenger, challengee, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetContext mocks base method.
|
||||||
|
func (m *MockMQTTMonitorClientI) SetContext(ctx types.Context) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start mocks base method.
|
||||||
|
func (m *MockMQTTMonitorClientI) Start() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@ -7,11 +7,13 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
bank "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
|
bank "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
|
||||||
"github.com/planetmint/planetmint-go/lib"
|
"github.com/planetmint/planetmint-go/lib"
|
||||||
|
"github.com/planetmint/planetmint-go/monitor"
|
||||||
"github.com/planetmint/planetmint-go/testutil"
|
"github.com/planetmint/planetmint-go/testutil"
|
||||||
clitestutil "github.com/planetmint/planetmint-go/testutil/cli"
|
clitestutil "github.com/planetmint/planetmint-go/testutil/cli"
|
||||||
e2etestutil "github.com/planetmint/planetmint-go/testutil/e2e"
|
e2etestutil "github.com/planetmint/planetmint-go/testutil/e2e"
|
||||||
@ -163,6 +165,7 @@ func (s *SelectionE2ETestSuite) TestPopSelectionNoActors() {
|
|||||||
|
|
||||||
func (s *SelectionE2ETestSuite) TestPopSelectionOneActors() {
|
func (s *SelectionE2ETestSuite) TestPopSelectionOneActors() {
|
||||||
err := e2etestutil.AttestMachine(s.network, machines[0].name, machines[0].mnemonic, 0, s.feeDenom)
|
err := e2etestutil.AttestMachine(s.network, machines[0].name, machines[0].mnemonic, 0, s.feeDenom)
|
||||||
|
monitor.MqttMonitorInstance.AddParticipant(machines[0].address, time.Now().Unix())
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
|
||||||
out := s.perpareLocalTest()
|
out := s.perpareLocalTest()
|
||||||
@ -173,6 +176,7 @@ func (s *SelectionE2ETestSuite) TestPopSelectionOneActors() {
|
|||||||
|
|
||||||
func (s *SelectionE2ETestSuite) TestPopSelectionTwoActors() {
|
func (s *SelectionE2ETestSuite) TestPopSelectionTwoActors() {
|
||||||
err := e2etestutil.AttestMachine(s.network, machines[1].name, machines[1].mnemonic, 1, s.feeDenom)
|
err := e2etestutil.AttestMachine(s.network, machines[1].name, machines[1].mnemonic, 1, s.feeDenom)
|
||||||
|
monitor.MqttMonitorInstance.AddParticipant(machines[1].address, time.Now().Unix())
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
|
|
||||||
out := s.perpareLocalTest()
|
out := s.perpareLocalTest()
|
||||||
|
|||||||
@ -15,12 +15,9 @@ 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/config"
|
|
||||||
"github.com/planetmint/planetmint-go/monitor"
|
|
||||||
"github.com/planetmint/planetmint-go/x/dao/keeper"
|
"github.com/planetmint/planetmint-go/x/dao/keeper"
|
||||||
"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"
|
||||||
"github.com/syndtr/goleveldb/leveldb"
|
|
||||||
|
|
||||||
daotestutil "github.com/planetmint/planetmint-go/x/dao/testutil"
|
daotestutil "github.com/planetmint/planetmint-go/x/dao/testutil"
|
||||||
)
|
)
|
||||||
@ -56,11 +53,6 @@ func DaoKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
|
|||||||
|
|
||||||
ctrl := gomock.NewController(t)
|
ctrl := gomock.NewController(t)
|
||||||
bk := daotestutil.NewMockBankKeeper(ctrl)
|
bk := daotestutil.NewMockBankKeeper(ctrl)
|
||||||
aciveActorsDB, err := leveldb.OpenFile("./activeActors.db", nil)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
mqttMonitor := monitor.NewMqttMonitorService(aciveActorsDB, *config.GetConfig())
|
|
||||||
|
|
||||||
bk.EXPECT().MintCoins(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
|
bk.EXPECT().MintCoins(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
|
||||||
bk.EXPECT().BurnCoins(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
|
bk.EXPECT().BurnCoins(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
|
||||||
@ -82,11 +74,10 @@ func DaoKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
|
|||||||
nil,
|
nil,
|
||||||
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
|
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
|
||||||
"",
|
"",
|
||||||
mqttMonitor,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Initialize params
|
// Initialize params
|
||||||
err = k.SetParams(ctx, types.DefaultParams())
|
err := k.SetParams(ctx, types.DefaultParams())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,8 @@ import (
|
|||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||||
"github.com/planetmint/planetmint-go/app"
|
"github.com/planetmint/planetmint-go/app"
|
||||||
|
"github.com/planetmint/planetmint-go/monitor"
|
||||||
|
monitormocks "github.com/planetmint/planetmint-go/monitor/mocks"
|
||||||
"github.com/planetmint/planetmint-go/testutil/sample"
|
"github.com/planetmint/planetmint-go/testutil/sample"
|
||||||
"github.com/planetmint/planetmint-go/util"
|
"github.com/planetmint/planetmint-go/util"
|
||||||
"github.com/planetmint/planetmint-go/util/mocks"
|
"github.com/planetmint/planetmint-go/util/mocks"
|
||||||
@ -39,6 +41,8 @@ func Load(t *testing.T, configs ...Config) *Network {
|
|||||||
|
|
||||||
// use mock client for testing
|
// use mock client for testing
|
||||||
util.MQTTClient = &mocks.MockMQTTClient{}
|
util.MQTTClient = &mocks.MockMQTTClient{}
|
||||||
|
monitor.MonitorMQTTClient = &mocks.MockMQTTClient{}
|
||||||
|
monitor.MqttMonitorInstance = &monitormocks.MockMQTTMonitorClientI{}
|
||||||
elements.Client = &elementsmocks.MockClient{}
|
elements.Client = &elementsmocks.MockClient{}
|
||||||
util.RegisterAssetServiceHTTPClient = &mocks.MockClient{}
|
util.RegisterAssetServiceHTTPClient = &mocks.MockClient{}
|
||||||
|
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package dao
|
|||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
|
||||||
|
"github.com/planetmint/planetmint-go/monitor"
|
||||||
"github.com/planetmint/planetmint-go/util"
|
"github.com/planetmint/planetmint-go/util"
|
||||||
"github.com/planetmint/planetmint-go/x/dao/keeper"
|
"github.com/planetmint/planetmint-go/x/dao/keeper"
|
||||||
|
|
||||||
@ -23,8 +24,8 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper)
|
|||||||
hexProposerAddress := hex.EncodeToString(proposerAddress)
|
hexProposerAddress := hex.EncodeToString(proposerAddress)
|
||||||
if isPopHeight(ctx, k, currentBlockHeight) {
|
if isPopHeight(ctx, k, currentBlockHeight) {
|
||||||
// select PoP participants
|
// select PoP participants
|
||||||
k.MqttMonitor.SetContext(ctx)
|
monitor.MqttMonitorInstance.SetContext(ctx)
|
||||||
challenger, challengee, err := k.MqttMonitor.SelectPoPParticipantsOutOfActiveActors()
|
challenger, challengee, err := monitor.MqttMonitorInstance.SelectPoPParticipantsOutOfActiveActors()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.GetAppLogger().Error(ctx, "error during PoP Participant selection ", err)
|
util.GetAppLogger().Error(ctx, "error during PoP Participant selection ", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,7 +27,6 @@ type (
|
|||||||
machineKeeper types.MachineKeeper
|
machineKeeper types.MachineKeeper
|
||||||
authority string
|
authority string
|
||||||
RootDir string
|
RootDir string
|
||||||
MqttMonitor *monitor.MqttMonitor
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -46,13 +45,12 @@ func NewKeeper(
|
|||||||
machineKeeper types.MachineKeeper,
|
machineKeeper types.MachineKeeper,
|
||||||
authority string,
|
authority string,
|
||||||
rootDir string,
|
rootDir string,
|
||||||
mqttMonitor *monitor.MqttMonitor,
|
|
||||||
) *Keeper {
|
) *Keeper {
|
||||||
// set KeyTable if it has not already been set
|
// set KeyTable if it has not already been set
|
||||||
if !ps.HasKeyTable() {
|
if !ps.HasKeyTable() {
|
||||||
ps = ps.WithKeyTable(types.ParamKeyTable())
|
ps = ps.WithKeyTable(types.ParamKeyTable())
|
||||||
}
|
}
|
||||||
|
monitor.LazyMqttMonitorLoader(rootDir)
|
||||||
return &Keeper{
|
return &Keeper{
|
||||||
cdc: cdc,
|
cdc: cdc,
|
||||||
storeKey: storeKey,
|
storeKey: storeKey,
|
||||||
@ -68,7 +66,6 @@ func NewKeeper(
|
|||||||
machineKeeper: machineKeeper,
|
machineKeeper: machineKeeper,
|
||||||
authority: authority,
|
authority: authority,
|
||||||
RootDir: rootDir,
|
RootDir: rootDir,
|
||||||
MqttMonitor: mqttMonitor,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user