Skip to content

Commit

Permalink
feat: add availability check for Mosquitto test server in MQTT client…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
tphakala committed Dec 21, 2024
1 parent a2a3a3f commit 51d580d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/mqtt/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 51d580d

Please sign in to comment.