planetmint-go/monitor/mqtt_monitor_test.go
Jürgen Eckel 779b1edd48
Eckelj/mqtt monitoring (#359)
* added a MqttMonitor module with levelDB and periodic cleanup
* initialized in the app
* passed to dao keeper
* added conversion methods (string2unixtime, byte ToJSON)
* removed obsolete keeper code
* maded  RDDLToken.Factor public
* added explicit mqtt client to the monitor module
* restart mqtt connection in mqttmonitor on connection loss
* adjusted mqttmock structure to be compatible
* added some linter exclusions to let the monitor tool pass
* 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
* added mutex in mocks to protect against data races
* defined mocks for the dao tests
* clear separation between interface and mqtt-Monitor

* added another waiting block to ensure the tx went through (multi-threading issue, race condition) during tests this failed sometimes
* added memstorage to test instead of a file based DB


Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
2024-04-08 10:49:00 +02:00

107 lines
3.1 KiB
Go

package monitor_test
import (
"testing"
"time"
"github.com/planetmint/planetmint-go/config"
"github.com/planetmint/planetmint-go/monitor"
"github.com/planetmint/planetmint-go/util/mocks"
"github.com/stretchr/testify/assert"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/storage"
)
func init() {
// Use MQTT mock client
monitor.MonitorMQTTClient = &mocks.MockMQTTClient{}
}
const (
challengerInput = "plmnt1fx3x6u8k5q8kjl7pamsuwjtut8nkks8dk92dek"
challengeeInput = "plmnt1fsaljz3xqf6vchkjxfzfrd30cdp3j4vqh298pr"
)
func TestGMonitorActiveParticipants(t *testing.T) {
monitor.LazyLoadMonitorMQTTClient()
cfg := config.GetConfig()
db, err := leveldb.Open(storage.NewMemStorage(), nil)
assert.NoError(t, err)
defer db.Close()
mqttMonitor := monitor.NewMqttMonitorService(db, *cfg)
err = mqttMonitor.Start()
assert.NoError(t, err)
currentTime := time.Now()
unixTime := currentTime.Unix()
err = mqttMonitor.AddParticipant(challengerInput, unixTime)
assert.NoError(t, err)
err = mqttMonitor.AddParticipant(challengeeInput, unixTime)
assert.NoError(t, err)
mqttMonitor.CleanupDB()
challenger, challengee, err := mqttMonitor.SelectPoPParticipantsOutOfActiveActors()
assert.NoError(t, err)
assert.Contains(t, challenger, "plmnt")
assert.Contains(t, challengee, "plmnt")
mqttMonitor.Terminate()
}
func TestCleanupRemoval(t *testing.T) {
monitor.LazyLoadMonitorMQTTClient()
cfg := config.GetConfig()
db, err := leveldb.Open(storage.NewMemStorage(), nil)
assert.NoError(t, err)
defer db.Close()
mqttMonitor := monitor.NewMqttMonitorService(db, *cfg)
err = mqttMonitor.Start()
assert.NoError(t, err)
currentTime := time.Now()
CleanupPeriodicityAgo := currentTime.Add(-1 * mqttMonitor.CleanupPeriodicityInMinutes * time.Minute)
unixTimeNow := currentTime.Unix()
err = mqttMonitor.AddParticipant(challengerInput, unixTimeNow)
assert.NoError(t, err)
err = mqttMonitor.AddParticipant(challengeeInput, CleanupPeriodicityAgo.Unix()-1)
assert.NoError(t, err)
mqttMonitor.CleanupDB()
challenger, challengee, err := mqttMonitor.SelectPoPParticipantsOutOfActiveActors()
assert.NoError(t, err)
assert.Equal(t, "", challenger)
assert.Contains(t, "", challengee)
mqttMonitor.Terminate()
}
func TestCleanupPrecisionTest(t *testing.T) {
monitor.LazyLoadMonitorMQTTClient()
cfg := config.GetConfig()
db, err := leveldb.Open(storage.NewMemStorage(), nil)
assert.NoError(t, err)
defer db.Close()
mqttMonitor := monitor.NewMqttMonitorService(db, *cfg)
err = mqttMonitor.Start()
assert.NoError(t, err)
currentTime := time.Now()
CleanupThresholdAgo := currentTime.Add(-1 * mqttMonitor.CleanupPeriodicityInMinutes * time.Minute)
aboveThreshold := CleanupThresholdAgo.Unix() + 10
unixTimeNow := currentTime.Unix()
err = mqttMonitor.AddParticipant(challengerInput, unixTimeNow)
assert.NoError(t, err)
err = mqttMonitor.AddParticipant(challengeeInput, aboveThreshold)
assert.NoError(t, err)
mqttMonitor.CleanupDB()
challenger, challengee, err := mqttMonitor.SelectPoPParticipantsOutOfActiveActors()
assert.NoError(t, err)
assert.Contains(t, challenger, "plmnt")
assert.Contains(t, challengee, "plmnt")
mqttMonitor.Terminate()
}