Make statsmanager and interfacemanager independent. Add reconnect logic for EventMonitoring

This commit is contained in:
Pim van Pelt
2025-06-24 07:36:10 +02:00
parent 1889934a9c
commit 3401c96112
5 changed files with 196 additions and 51 deletions

View File

@@ -3,8 +3,10 @@
package vpp
import (
"go.fd.io/govpp/binapi/interface_types"
"testing"
"time"
"go.fd.io/govpp/binapi/interface_types"
)
func TestNewInterfaceManager(t *testing.T) {
@@ -22,6 +24,14 @@ func TestNewInterfaceManager(t *testing.T) {
if manager.eventCallback != nil {
t.Error("InterfaceManager should have nil callback initially")
}
if manager.running {
t.Error("InterfaceManager should not be running initially")
}
if manager.watchingEvents {
t.Error("InterfaceManager should not be watching events initially")
}
}
func TestInterfaceManagerSetEventCallback(t *testing.T) {
@@ -116,6 +126,25 @@ func TestInterfaceManagerHandleInterfaceEventWithoutCallback(t *testing.T) {
manager.handleInterfaceEvent()
}
func TestInterfaceManagerInitializeEventWatchingWithoutConnection(t *testing.T) {
client := NewVPPClient()
manager := NewInterfaceManager(client)
err := manager.InitializeEventWatching()
if err == nil {
t.Error("InitializeEventWatching() should return error when not connected")
}
vppErr, ok := err.(*VPPError)
if !ok {
t.Errorf("Expected VPPError, got %T", err)
}
if vppErr.Message != "VPP client not connected" {
t.Errorf("Expected specific error message, got: %s", vppErr.Message)
}
}
func TestInterfaceDetails(t *testing.T) {
details := InterfaceDetails{
SwIfIndex: interface_types.InterfaceIndex(42),
@@ -188,3 +217,68 @@ func TestInterfaceEventCallback(t *testing.T) {
t.Errorf("Expected second interface 'test2', got %q", callbackDetails[1].InterfaceName)
}
}
func TestInterfaceManagerStartStopEventMonitoring(t *testing.T) {
client := NewVPPClient()
manager := NewInterfaceManager(client)
if manager.running {
t.Error("InterfaceManager should not be running initially")
}
manager.StartEventMonitoring()
if !manager.running {
t.Error("InterfaceManager should be running after StartEventMonitoring()")
}
// Test starting again (should be safe)
manager.StartEventMonitoring()
if !manager.running {
t.Error("InterfaceManager should still be running after second StartEventMonitoring()")
}
manager.StopEventMonitoring()
if manager.running {
t.Error("InterfaceManager should not be running after StopEventMonitoring()")
}
}
func TestInterfaceManagerEventMonitoringWithConnectionChanges(t *testing.T) {
client := NewVPPClient()
manager := NewInterfaceManager(client)
// Set a callback to track calls
var callbackCount int
manager.SetEventCallback(func(details []InterfaceDetails) {
callbackCount++
})
manager.StartEventMonitoring()
// Let it run briefly
time.Sleep(50 * time.Millisecond)
// Simulate VPP connection and disconnection by checking state changes
initialWatchingState := manager.watchingEvents
// Stop monitoring
manager.StopEventMonitoring()
// Verify it stopped
if manager.running {
t.Error("Event monitoring should have stopped")
}
// The watching state should reflect the connection state
if !client.IsConnected() && manager.watchingEvents {
t.Error("Should not be watching events when disconnected")
}
// Initial state should be false since we're not connected to VPP in tests
if initialWatchingState {
t.Error("Should not be watching events initially when VPP is not connected")
}
}