added isConnectionOpen to the interface and analysis this during execution

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel 2024-05-16 12:25:53 +02:00
parent 2223abc2d4
commit 281e3d16a5
No known key found for this signature in database
3 changed files with 18 additions and 9 deletions

View File

@ -221,7 +221,7 @@ func (mms *MqttMonitor) MonitorActiveParticipants() {
log.Println("[app] [Monitor] subscribed to tele/# channels")
for !mms.IsTerminated() {
if !mqttClient.IsConnected() {
if !mqttClient.IsConnected() || !mqttClient.IsConnectionOpen() {
log.Println("[app] [Monitor] retry establishing a connection")
break // Exit inner loop on disconnect
}

View File

@ -9,14 +9,15 @@ import (
// MockMQTTClient is the mock mqtt client
type MockMQTTClient struct {
ConnectFunc func() mqtt.Token
DisconnectFunc func(quiesce uint)
PublishFunc func(topic string, qos byte, retained bool, payload interface{}) mqtt.Token
SubscribeFunc func(topic string, qos byte, callback mqtt.MessageHandler) mqtt.Token
UnsubscribeFunc func(topics ...string) mqtt.Token
IsConnectedFunc func() bool
connected bool
connectedMutex sync.Mutex
ConnectFunc func() mqtt.Token
DisconnectFunc func(quiesce uint)
PublishFunc func(topic string, qos byte, retained bool, payload interface{}) mqtt.Token
SubscribeFunc func(topic string, qos byte, callback mqtt.MessageHandler) mqtt.Token
UnsubscribeFunc func(topics ...string) mqtt.Token
IsConnectedFunc func() bool
IsConnectionOpenFunc func() bool
connected bool
connectedMutex sync.Mutex
}
// GetConnectFunc fetches the mock client's `Connect` func
@ -129,3 +130,10 @@ func (m *MockMQTTClient) IsConnected() bool {
m.connectedMutex.Unlock()
return connected
}
func (m *MockMQTTClient) IsConnectionOpen() bool {
m.connectedMutex.Lock()
connected := m.connected
m.connectedMutex.Unlock()
return connected
}

View File

@ -23,6 +23,7 @@ type MQTTClientI interface {
Subscribe(topic string, qos byte, callback mqtt.MessageHandler) mqtt.Token
Unsubscribe(topics ...string) mqtt.Token
IsConnected() bool
IsConnectionOpen() bool
}
var (