planetmint-go/util/mqtt_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

43 lines
1.5 KiB
Go

package util
import (
"testing"
"github.com/planetmint/planetmint-go/util/mocks"
"github.com/planetmint/planetmint-go/x/dao/types"
"github.com/stretchr/testify/assert"
)
func init() {
// Use MQTT mock client
MQTTClient = &mocks.MockMQTTClient{}
}
func TestSendMqttPopInitMessages(t *testing.T) {
t.Parallel()
var challenge types.Challenge
challenge.Initiator = ""
challenge.Challengee = "plmnt15gdanx0nm2lwsx30a6wft7429p32dhzaq37c06"
challenge.Challenger = "plmnt1683t0us0r85840nsepx6jrk2kjxw7zrcnkf0rp"
challenge.Height = 58
err := sendMqttPopInitMessages(challenge)
assert.NoError(t, err)
}
func TestGetMqttStatusOfParticipantMocked(t *testing.T) {
t.Parallel()
participant := "plmnt15gdanx0nm2lwsx30a6wft7429p32dhzaq37c06"
isAvailable, err := GetMqttStatusOfParticipant(participant, 200)
assert.NoError(t, err)
assert.True(t, isAvailable)
}
func TestToJSON(t *testing.T) {
t.Parallel()
payload := []byte(`{"Time":"2024-03-26T11:50:42","Uptime":"0T00:50:19","UptimeSec":3019,"Heap":97,"SleepMode":"Dynamic","Sleep":10,"LoadAvg":99,"MqttCount":2,"Berry":{"HeapUsed":27,"Objects":491},"POWER1":"ON","POWER2":"ON","Dimmer":17,"Color":"00182C","HSBColor":"207,100,17","Channel":[0,9,17],"Scheme":0,"Width":1,"Fade":"OFF","Speed":1,"LedTable":"ON","Wifi":{"AP":1,"SSId":"UPC5729E56","BSSId":"C2:14:7E:6F:BC:C5","Channel":11,"Mode":"11n","RSSI":96,"Signal":-52,"LinkCount":1,"Downtime":"0T00:00:10"}}`)
result, err := ToJSON(payload)
assert.NoError(t, err)
assert.Equal(t, 21, len(result))
}