8000 Additional tests for conn.go by dleehr · Pull Request #1320 · ansible/receptor · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Draft
wants to merge 3 commits into
base: devel
Choose a base branch
from

Conversation

dleehr
Copy link
Contributor
@dleehr dleehr commented May 9, 2025

Kept in same package netceptor

Replaces #1317

Kept in same package netceptor
@dleehr dleehr marked this pull request as draft May 9, 2025 20:00
Copy link
codecov bot commented May 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 46.06%. Comparing base (06eef5e) to head (a851b03).
Report is 1 commits behind head on devel.

@@            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

Components Coverage Δ
Go 46.06% <82.81%> (+2.34%) ⬆️
Receptorctl ∅ <ø> (∅)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines +142 to +221
8000
// 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
}

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
}
Copy link
Contributor Author

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.

Comment on lines +155 to +156
// 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) {
Copy link
Contributor Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0