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") log.Println("[app] [Monitor] subscribed to tele/# channels")
for !mms.IsTerminated() { for !mms.IsTerminated() {
if !mqttClient.IsConnected() { if !mqttClient.IsConnected() || !mqttClient.IsConnectionOpen() {
log.Println("[app] [Monitor] retry establishing a connection") log.Println("[app] [Monitor] retry establishing a connection")
break // Exit inner loop on disconnect break // Exit inner loop on disconnect
} }

View File

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