|
|
pirms 2 nedēļām | |
|---|---|---|
| .. | ||
| README.md | pirms 2 nedēļām | |
| bridge_test.go | pirms 2 nedēļām | |
| event_loop_test.go | pirms 2 nedēļām | |
| integration_test.go | pirms 2 nedēļām | |
| mqtt_handler_test.go | pirms 2 nedēļām | |
| testutil.go | pirms 2 nedēļām | |
This directory contains comprehensive tests for the bridge service located at cmd/bridge/main.go.
tests/bridge/
├── bridge_test.go # Core bridge functions extracted for testing
├── mqtt_handler_test.go # Unit tests for MQTT message handling
├── event_loop_test.go # Unit tests for event loop logic
├── integration_test.go # Integration tests with real Kafka
├── testutil.go # Test utilities and helper functions
└── README.md # This file
mqtt_handler_test.go)Tests the MQTT handler function that processes incoming beacon readings:
event_loop_test.go)Tests the main event loop logic:
integration_test.go)End-to-end tests that interact with real Kafka infrastructure:
go test ./tests/bridge/...
go test ./tests/bridge/... -short
go test ./tests/bridge/... -v
go test ./tests/bridge/... -run TestMQTTHandler_SingleReading
go test ./tests/bridge/... -cover
go test ./tests/bridge/... -coverprofile=coverage.out
go tool cover -html=coverage.out
Integration tests require a running Kafka instance. By default, they connect to localhost:9092.
docker run -d \
--name kafka-test \
-p 9092:9092 \
-e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092 \
-e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \
confluentinc/cp-kafka:latest
Set the KAFKA_URL environment variable:
KAFKA_URL=your-kafka-broker:9092 go test ./tests/bridge/...
The testutil.go file provides helper functions:
NewTestHelper(t): Creates a test helper instanceCreateRawReading(mac, rssi): Creates test beacon readingsGenerateTestMAC(index): Generates test MAC addressesSetupTestBeacons(appState): Sets up standard test beaconsAssertKafkaMessageCount(t, writer, expected): Asserts Kafka message countA mock implementation of the Kafka writer for unit tests that captures all messages written to it:
mockWriter := &MockKafkaWriter{Messages: []kafka.Message{}}
// ... use in tests ...
if len(mockWriter.Messages) != expected {
t.Errorf("Expected %d messages, got %d", expected, len(mockWriter.Messages))
}
A mock implementation of the MQTT client for testing event loop logic:
mockClient := NewMockMQTTClient()
// ... use in tests ...
if _, exists := mockClient.PublishedMessages["/alerts"]; !exists {
t.Error("Expected message to be published to /alerts topic")
}
docker ps | grep kafkadocker logs kafka-testtelnet localhost 9092go mod tidyWhen adding new tests:
Test<FunctionName>_<Scenario>