From 51d580d52311c762b8114773b96b7886389cb967 Mon Sep 17 00:00:00 2001 From: "Tomi P. Hakala" Date: Sat, 21 Dec 2024 09:40:57 +0200 Subject: [PATCH] feat: add availability check for Mosquitto test server in MQTT client tests - Introduced a helper function to check the availability of the Mosquitto test server before running MQTT tests. - Updated the TestMQTTClient function to skip tests if the Mosquitto server is not reachable, improving test reliability and feedback. - This change ensures that tests are only executed when the necessary external service is available, preventing false negatives in test results. --- internal/mqtt/client_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/internal/mqtt/client_test.go b/internal/mqtt/client_test.go index b0a0e984..698a3413 100644 --- a/internal/mqtt/client_test.go +++ b/internal/mqtt/client_test.go @@ -16,9 +16,24 @@ import ( "github.com/tphakala/birdnet-go/internal/telemetry" ) +// Add this helper function at the top of the file +func isMosquittoTestServerAvailable() bool { + conn, err := net.DialTimeout("tcp", "test.mosquitto.org:1883", 5*time.Second) + if err != nil { + return false + } + conn.Close() + return true +} + // TestMQTTClient runs a suite of tests for the MQTT client implementation. // It covers basic functionality, error handling, reconnection scenarios, and metrics collection. func TestMQTTClient(t *testing.T) { + mosquittoAvailable := isMosquittoTestServerAvailable() + if !mosquittoAvailable { + t.Skip("Skipping MQTT tests: test.mosquitto.org is not available") + } + t.Run("Basic Functionality", testBasicFunctionality) t.Run("Incorrect Broker Address", testIncorrectBrokerAddress) t.Run("Connection Loss Before Publish", testConnectionLossBeforePublish)