Skip to content

Commit

Permalink
Unit tests for Ping.go inside netceptor (#810)
Browse files Browse the repository at this point in the history
Create interface for Ping & Packetconn and add unit tests to cover netceptor/ping.go

Increase ping.go coverage from 0% to 92.5
  • Loading branch information
matoval authored Aug 28, 2023
1 parent f9bc1b1 commit 54ad74b
Show file tree
Hide file tree
Showing 8 changed files with 855 additions and 34 deletions.
12 changes: 6 additions & 6 deletions pkg/netceptor/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type acceptResult struct {
// Listener implements the net.Listener interface via the Receptor network.
type Listener struct {
s *Netceptor
pc *PacketConn
pc PacketConner
ql quic.Listener
acceptChan chan *acceptResult
doneChan chan struct{}
Expand Down Expand Up @@ -261,7 +261,7 @@ func (li *Listener) Addr() net.Addr {
// Conn implements the net.Conn interface via the Receptor network.
type Conn struct {
s *Netceptor
pc *PacketConn
pc PacketConner
qc quic.Connection
qs quic.Stream
doneChan chan struct{}
Expand Down Expand Up @@ -380,7 +380,7 @@ func (s *Netceptor) DialContext(ctx context.Context, node string, service string

// monitorUnreachable receives unreachable messages from the underlying PacketConn, and ends the connection
// if the remote service has gone away.
func monitorUnreachable(pc *PacketConn, doneChan chan struct{}, remoteAddr Addr, cancel context.CancelFunc) {
func monitorUnreachable(pc PacketConner, doneChan chan struct{}, remoteAddr Addr, cancel context.CancelFunc) {
msgCh := pc.SubscribeUnreachable(doneChan)
if msgCh == nil {
cancel()
Expand All @@ -390,7 +390,7 @@ func monitorUnreachable(pc *PacketConn, doneChan chan struct{}, remoteAddr Addr,
// read from channel until closed
for msg := range msgCh {
if msg.Problem == ProblemServiceUnknown && msg.ToNode == remoteAddr.node && msg.ToService == remoteAddr.service {
pc.s.Logger.Warning("remote service %s to node %s is unreachable", msg.ToService, msg.ToNode)
pc.GetLogger().Warning("remote service %s to node %s is unreachable", msg.ToService, msg.ToNode)
cancel()
}
}
Expand Down Expand Up @@ -421,11 +421,11 @@ func (c *Conn) Close() error {
}

func (c *Conn) CloseConnection() error {
c.pc.cancel()
c.pc.Cancel()
c.doneOnce.Do(func() {
close(c.doneChan)
})
c.s.Logger.Debug("closing connection from service %s to %s", c.pc.localService, c.RemoteAddr().String())
c.s.Logger.Debug("closing connection from service %s to %s", c.pc.GetLocalService(), c.RemoteAddr().String())

return c.qc.CloseWithError(0, "normal close")
}
Expand Down
208 changes: 208 additions & 0 deletions pkg/netceptor/mock_netceptor/packetconn.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 54ad74b

Please sign in to comment.