Files
Bose-SoundTouch/pkg/discovery/mdns_test.go
T
Tobias Gesellchen c5a3911104 Fix SSDP discovery and enhance device discovery consistency
Major improvements to device discovery system:

🔧 **SSDP Discovery Fixed**:
- Fixed networking issue where SSDP used connected UDP socket instead of UDP listener
- SSDP now properly receives unicast responses from multicast requests
- UPnP discovery now works reliably and finds all MediaRenderer devices

 **Enhanced DiscoveredDevice Model**:
- Added consistent URL fields (APIBaseURL, InfoURL) for all discovery methods
- Added protocol-specific fields (UPnPLocation, UPnPUSN, MDNSHostname, etc.)
- Added DiscoveryMethod tracking to show how devices were found
- Added device merging support for same device found via multiple protocols

🚀 **Unified Discovery Improvements**:
- Fixed device merging logic to properly combine protocol-specific data
- Discovery methods now correctly show combinations like 'Configuration+SSDP/UPnP+mDNS/Bonjour'
- Removed duplicate configuration device loading in individual services
- All three discovery methods (SSDP, mDNS, Configuration) work together seamlessly

🛠 **Updated Tools & Examples**:
- Updated soundtouch-cli to display new consistent field structure
- Enhanced all example programs with better device information display
- Added new unified discovery example demonstrating all three methods
- Fixed context timeout issues in example programs

📋 **Comprehensive Testing**:
- All tests updated and passing
- Real-world validation with actual Bose SoundTouch devices
- Confirmed discovery methods properly merge device data

Every discovered device now has consistent http://host:port/info URLs regardless
of discovery method, while preserving valuable protocol-specific metadata.
2026-01-10 23:01:22 +01:00

113 lines
3.3 KiB
Go

package discovery
import (
"context"
"testing"
"time"
)
func TestNewMDNSDiscoveryService(t *testing.T) {
// Test with custom timeout
service := NewMDNSDiscoveryService(10 * time.Second)
if service.timeout != 10*time.Second {
t.Errorf("Expected timeout 10s, got %v", service.timeout)
}
// Test with zero timeout (should use default)
service = NewMDNSDiscoveryService(0)
if service.timeout != defaultTimeout {
t.Errorf("Expected default timeout %v, got %v", defaultTimeout, service.timeout)
}
}
func TestMDNSDiscoverDevices(t *testing.T) {
service := NewMDNSDiscoveryService(2 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
// Note: This test will attempt actual mDNS discovery
// In a real network environment, this might find actual SoundTouch devices
// In a test environment without devices, it should return an empty slice
devices, _ := service.DiscoverDevices(ctx)
// devices slice should be initialized (but might be empty)
// We don't fail on errors as they may be due to network conditions in test environment
if devices == nil {
t.Error("Expected devices slice to be initialized, got nil")
}
// If devices are found, verify they have the required fields
for _, device := range devices {
if device.Host == "" {
t.Error("Device host should not be empty")
}
if device.Port == 0 {
t.Error("Device port should not be zero")
}
if device.Name == "" {
t.Error("Device name should not be empty")
}
if device.InfoURL == "" {
t.Error("Device info URL should not be empty")
}
}
}
func TestMDNSServiceEntryToDevice(t *testing.T) {
service := NewMDNSDiscoveryService(5 * time.Second)
// Test with nil entry
device := service.serviceEntryToDevice(nil)
if device != nil {
t.Error("Expected nil device for nil entry")
}
// Note: Testing with actual zeroconf.ServiceEntry would require
// creating mock objects or using a testing framework that can
// create proper ServiceEntry instances. For now, we test the nil case.
}
func TestMDNSDiscoveryTimeout(t *testing.T) {
service := NewMDNSDiscoveryService(100 * time.Millisecond)
start := time.Now()
ctx := context.Background()
_, err := service.DiscoverDevices(ctx)
duration := time.Since(start)
// The discovery should not take significantly longer than the timeout
// Allow some buffer for processing time
maxExpected := 200 * time.Millisecond
if duration > maxExpected {
t.Errorf("Discovery took too long: %v, expected less than %v", duration, maxExpected)
}
// We don't check for error here because mDNS discovery might succeed quickly
// or fail due to network conditions, both are acceptable in tests
_ = err
}
func TestMDNSDiscoveryWithCancelledContext(t *testing.T) {
service := NewMDNSDiscoveryService(5 * time.Second)
ctx, cancel := context.WithCancel(context.Background())
cancel() // Cancel immediately
devices, err := service.DiscoverDevices(ctx)
// Should handle cancelled context gracefully
// devices should never be nil, even if cancelled
if devices == nil {
t.Error("Expected devices slice to be initialized, got nil")
}
// Error might or might not occur depending on timing and network conditions
// This is acceptable for testing - we just ensure no panic and proper slice initialization
_ = err
}