You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.1 KiB
Go

package testutil
import (
"errors"
"time"
"github.com/noahlann/nnet/internal/connection"
)
// MockConnection is a mock implementation of ConnectionInterface for testing
type MockConnection struct {
IDValue string
RemoteAddrValue string
LocalAddrValue string
WriteFunc func(data []byte) error
CloseFunc func() error
LastActiveValue time.Time
WriteError bool
}
// NewMockConnection creates a new mock connection
func NewMockConnection(id string) connection.ConnectionInterface {
return &MockConnection{
IDValue: id,
RemoteAddrValue: "127.0.0.1:6995",
LocalAddrValue: "127.0.0.1:6995",
LastActiveValue: time.Now(),
WriteFunc: func(data []byte) error { return nil },
CloseFunc: func() error { return nil },
}
}
// ID returns the connection ID
func (m *MockConnection) ID() string {
return m.IDValue
}
// RemoteAddr returns the remote address
func (m *MockConnection) RemoteAddr() string {
return m.RemoteAddrValue
}
// LocalAddr returns the local address
func (m *MockConnection) LocalAddr() string {
return m.LocalAddrValue
}
// Write writes data to the connection
func (m *MockConnection) Write(data []byte) error {
if m.WriteError {
return errors.New("write error")
}
if m.WriteFunc != nil {
return m.WriteFunc(data)
}
return nil
}
// Close closes the connection
func (m *MockConnection) Close() error {
if m.CloseFunc != nil {
return m.CloseFunc()
}
return nil
}
// LastActive returns the last active time (if implemented)
func (m *MockConnection) LastActive() time.Time {
return m.LastActiveValue
}
// SetLastActive sets the last active time
func (m *MockConnection) SetLastActive(t time.Time) {
m.LastActiveValue = t
}
// SetWriteError sets whether Write should return an error
func (m *MockConnection) SetWriteError(err bool) {
m.WriteError = err
}
// SetWriteFunc sets a custom write function
func (m *MockConnection) SetWriteFunc(fn func(data []byte) error) {
m.WriteFunc = fn
}
// SetCloseFunc sets a custom close function
func (m *MockConnection) SetCloseFunc(fn func() error) {
m.CloseFunc = fn
}