refactor(tests): simplify MockInterface by embedding BaseInterface and removing redundant fields
This commit is contained in:
@@ -4,39 +4,28 @@ package transport
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"git.quad4.io/Networks/Reticulum-Go/pkg/common"
|
||||
)
|
||||
|
||||
type MockInterface struct {
|
||||
name string
|
||||
enabled bool
|
||||
common.BaseInterface
|
||||
sentData [][]byte
|
||||
dropRate float64 // 0.0 to 1.0
|
||||
mu sync.Mutex
|
||||
onReceive func([]byte)
|
||||
}
|
||||
|
||||
func (m *MockInterface) GetName() string { return m.name }
|
||||
func (m *MockInterface) IsEnabled() bool { return m.enabled }
|
||||
func (m *MockInterface) IsConnected() bool { return true }
|
||||
func (m *MockInterface) GetMTU() int { return 500 }
|
||||
func (m *MockInterface) GetBandwidthAvailable() bool { return true }
|
||||
func (m *MockInterface) SetEnabled(enabled bool) { m.enabled = enabled }
|
||||
func (m *MockInterface) Detach() {}
|
||||
|
||||
func (m *MockInterface) Send(data []byte, destination string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
m.Mutex.Lock()
|
||||
defer m.Mutex.Unlock()
|
||||
|
||||
// Simulate packet loss
|
||||
if m.dropRate > 0 {
|
||||
// In a real test we'd use rand.Float64()
|
||||
// For deterministic testing, let's just record everything for now
|
||||
}
|
||||
|
||||
|
||||
m.sentData = append(m.sentData, data)
|
||||
return nil
|
||||
}
|
||||
@@ -52,11 +41,14 @@ func TestTransportNetworkSimulation(t *testing.T) {
|
||||
tr := NewTransport(cfg)
|
||||
defer tr.Close()
|
||||
|
||||
iface1 := &MockInterface{name: "iface1", enabled: true}
|
||||
iface2 := &MockInterface{name: "iface2", enabled: true}
|
||||
iface1 := &MockInterface{BaseInterface: common.NewBaseInterface("iface1", common.IF_TYPE_UDP, true)}
|
||||
iface1.Enable()
|
||||
|
||||
tr.RegisterInterface(iface1.name, iface1)
|
||||
tr.RegisterInterface(iface2.name, iface2)
|
||||
iface2 := &MockInterface{BaseInterface: common.NewBaseInterface("iface2", common.IF_TYPE_UDP, true)}
|
||||
iface2.Enable()
|
||||
|
||||
tr.RegisterInterface(iface1.GetName(), iface1)
|
||||
tr.RegisterInterface(iface2.GetName(), iface2)
|
||||
|
||||
// Simulate receiving an announce on iface1
|
||||
// [header][hops][dest_hash(16)][payload...]
|
||||
@@ -64,7 +56,7 @@ func TestTransportNetworkSimulation(t *testing.T) {
|
||||
announcePacket[0] = PACKET_TYPE_ANNOUNCE
|
||||
announcePacket[1] = 0 // 0 hops
|
||||
copy(announcePacket[2:18], []byte("destination_hash"))
|
||||
|
||||
|
||||
// Mock the handler to avoid complex identity logic in this basic test
|
||||
tr.HandlePacket(announcePacket, iface1)
|
||||
|
||||
@@ -72,4 +64,3 @@ func TestTransportNetworkSimulation(t *testing.T) {
|
||||
// But HandlePacket runs in a goroutine, so we'd need to wait or use a better mock
|
||||
fmt.Println("Network simulation test initialized")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user