I’ve created a comprehensive test suite for the bridge service located at cmd/bridge/main.go. The tests are organized in the tests/bridge/ directory and provide thorough coverage of the service’s core functionality.
mqtthandler() function and kafkaWriter interfacetests/bridge/mqtt_handler_test.go
tests/bridge/event_loop_test.go
tests/bridge/integration_test.go
-short flag)All tests pass successfully:
=== RUN TestEventLoop_ApiUpdate_POST
--- PASS: TestEventLoop_ApiUpdate_POST (0.00s)
=== RUN TestEventLoop_ApiUpdate_DELETE
--- PASS: TestEventLoop_ApiUpdate_DELETE (0.00s)
=== RUN TestEventLoop_ApiUpdate_DELETE_All
--- PASS: TestEventLoop_ApiUpdate_DELETE_All (0.00s)
=== RUN TestEventLoop_AlertMessage
--- PASS: TestEventLoop_AlertMessage (0.10s)
=== RUN TestEventLoop_TrackerMessage
--- PASS: TestEventLoop_TrackerMessage (0.10s)
=== RUN TestEventLoop_ContextCancellation
--- PASS: TestEventLoop_ContextCancellation (0.00s)
=== RUN TestIntegration_AppStateSequentialOperations
--- PASS: TestIntegration_AppStateSequentialOperations (0.00s)
=== RUN TestIntegration_CleanLookup
--- PASS: TestIntegration_CleanLookup (0.00s)
=== RUN TestMQTTHandler_SingleReading
--- PASS: TestMQTTHandler_SingleReading (0.00s)
=== RUN TestMQTTHandler_MultipleReadings
--- PASS: TestMQTTHandler_MultipleReadings (0.00s)
=== RUN TestMQTTHandler_GatewayTypeSkipped
--- PASS: TestMQTTHandler_GatewayTypeSkipped (0.00s)
=== RUN TestMQTTHandler_UnknownBeaconSkipped
--- PASS: TestMQTTHandler_UnknownBeaconSkipped (0.00s)
=== RUN TestMQTTHandler_InvalidJSON
--- PASS: TestMQTTHandler_InvalidJSON (0.00s)
=== RUN TestMQTTHandler_HostnameExtraction
--- PASS: TestMQTTHandler_HostnameExtraction (0.00s)
=== RUN TestMQTTHandler_PreservesRawData
--- PASS: TestMQTTHandler_PreservesRawData (0.00s)
PASS
ok github.com/AFASystems/presence/tests/bridge 0.209s
go test ./tests/bridge/... -short
go test ./tests/bridge/...
go test ./tests/bridge/... -short -v
go test ./tests/bridge/... -run TestMQTTHandler_SingleReading -v
main() to bridge_test.go to make it testablekafkaWriter interface allows easy mockingThe tests exercise and verify interactions with:
internal/pkg/common/appcontext - AppState managementinternal/pkg/model - Data models (RawReading, BeaconAdvertisement, Alert, Tracker)internal/pkg/kafkaclient - Kafka consumption (via integration tests)github.com/segmentio/kafka-go - Kafka operationsgithub.com/eclipse/paho.mqtt.golang - MQTT client operationsIf you want to improve the test suite further:
-short flagbridge_test.go mirror the logic in main.go