-
Notifications
You must be signed in to change notification settings - Fork 89
Additional tests for conn.go #1320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: devel
Are you sure you want to change the base?
Conversation
Kept in same package netceptor
Codecov ReportAll modified and coverable lines are covered by tests ✅
@@ Coverage Diff @@
## devel #1320 +/- ##
==========================================
+ Coverage 43.72% 46.06% +2.34%
==========================================
Files 57 57
Lines 9782 9843 +61
==========================================
+ Hits 4277 4534 +257
+ Misses 5193 4984 -209
- Partials 312 325 +13 see 4 files with indirect coverage changes
🚀 New features to boost your workflow:
|
|
// mockNetceptorForListen is a mock for Netceptor used in listen tests. | ||
type mockNetceptorForListen struct { | ||
listenerRegistry map[string]*PacketConn | ||
reservedServices map[string]func(*MessageData) error | ||
nodeID string | ||
maxForwardingHops byte | ||
logger *logger.ReceptorLogger | ||
context context.Context | ||
cancelFunc context.CancelFunc | ||
listenerLock sync.RWMutex | ||
getEphemeralFunc func() string | ||
} | ||
|
||
// listen is a simplified implementation of Netceptor.listen for testing. | ||
func (m *mockNetceptorForListen) listen(ctx context.Context, service string, tlscfg *tls.Config, advertise bool, adTags map[string]string) (*Listener, error) { | ||
if len(service) > 8 { | ||
return nil, fmt.Errorf("service name %s too long", service) | ||
} | ||
if service == "" { | ||
service = m.GetEphemeralService() | ||
} | ||
m.listenerLock.Lock() | ||
defer m.listenerLock.Unlock() | ||
_, isReserved := m.reservedServices[service] | ||
_, isListening := m.listenerRegistry[service] | ||
if isReserved || isListening { | ||
return nil, fmt.Errorf("service %s is already listening", service) | ||
} | ||
|
||
// In a real implementation, we would create a PacketConn, set up QUIC, etc. | ||
// For testing, we'll just return a minimal Listener | ||
|
||
if advertise { | ||
m.AddLocalServiceAdvertisement(service, 0, adTags) | ||
} | ||
|
||
doneChan := make(chan struct{}) | ||
li := &Listener{ | ||
s: nil, // We don't need this for the test | ||
pc: nil, // We don't need this for the test | ||
ql: nil, // We don't need this for the test | ||
AcceptChan: make(chan *AcceptResult), | ||
DoneChan: doneChan, | ||
doneOnce: &sync.Once{}, | ||
} | ||
|
||
return li, nil | ||
} | ||
|
||
8000 | func newMockNetceptorForListen() *mockNetceptorForListen { | |
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
return &mockNetceptorForListen{ | ||
listenerRegistry: make(map[string]*PacketConn), | ||
reservedServices: make(map[string]func(*MessageData) error), | ||
nodeID: "test-node", | ||
maxForwardingHops: 5, | ||
logger: logger.NewReceptorLogger("test"), | ||
context: ctx, | ||
cancelFunc: cancel, | ||
listenerLock: sync.RWMutex{}, | ||
getEphemeralFunc: func() string { return "ephemeral-service" }, | ||
} | ||
} | ||
|
||
func (m *mockNetceptorForListen) GetEphemeralService() string { | ||
return m.getEphemeralFunc() | ||
} | ||
|
||
func (m *mockNetceptorForListen) AddNameHash(name string) uint64 { | ||
return 0 | ||
} | ||
|
||
func (m *mockNetceptorForListen) AddLocalServiceAdvertisement(service string, connType byte, tags map[string]string) { | ||
// Mock implementation - do nothing | ||
} | ||
|
||
func (m *mockNetceptorForListen) GetLogger() *logger.ReceptorLogger { | ||
return m.logger | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pattern for these mocks is to generate them into mock_netceptor
rather than writing them in-line in the test file.
// listen is a simplified implementation of Netceptor.listen for testing. | ||
func (m *mockNetceptorForListen) listen(ctx context.Context, service string, tlscfg *tls.Config, advertise bool, adTags map[string]string) (*Listener, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But if we mock the listen
method, we won't be running the real one from TestListen
, right?
Kept in same package netceptor
Replaces #1317