Skip to content

Commit b930b19

Browse files
committed
fixes for test coverage
1 parent 764adf5 commit b930b19

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

batchconn_generic.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ func toBatchConn(c net.PacketConn) batchConn {
1414
}
1515

1616
func readBatchUnavailable(xconn batchConn, err error) bool {
17+
ret := false
1718
if detector, ok := xconn.(batchErrDetector); ok {
18-
return detector.ReadBatchUnavailable(err)
19+
ret = detector.ReadBatchUnavailable(err)
1920
}
20-
return false
21+
return ret
2122
}
2223

2324
func writeBatchUnavailable(xconn batchConn, err error) bool {
25+
ret := false
2426
if detector, ok := xconn.(batchErrDetector); ok {
25-
return detector.WriteBatchUnavailable(err)
27+
ret = detector.WriteBatchUnavailable(err)
2628
}
27-
return false
29+
return ret
2830
}

batchconn_linux.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ func toBatchConn(c net.PacketConn) batchConn {
1414
if xconn, ok := c.(batchConn); ok {
1515
return xconn
1616
}
17+
var xconn batchConn
1718
if _, ok := c.(*net.UDPConn); ok {
18-
var xconn batchConn
1919
addr, err := net.ResolveUDPAddr("udp", c.LocalAddr().String())
2020
if err == nil {
2121
if addr.IP.To4() != nil {
@@ -24,9 +24,8 @@ func toBatchConn(c net.PacketConn) batchConn {
2424
xconn = ipv6.NewPacketConn(c)
2525
}
2626
}
27-
return xconn
2827
}
29-
return nil
28+
return xconn
3029
}
3130

3231
func isPacketConn(xconn batchConn) bool {
@@ -53,10 +52,11 @@ func readBatchUnavailable(xconn batchConn, err error) bool {
5352
}
5453
return false
5554
}
55+
ret := false
5656
if detector, ok := xconn.(batchErrDetector); ok {
57-
return detector.ReadBatchUnavailable(err)
57+
ret = detector.ReadBatchUnavailable(err)
5858
}
59-
return false
59+
return ret
6060
}
6161

6262
func writeBatchUnavailable(xconn batchConn, err error) bool {
@@ -73,8 +73,9 @@ func writeBatchUnavailable(xconn batchConn, err error) bool {
7373
}
7474
return false
7575
}
76+
ret := false
7677
if detector, ok := xconn.(batchErrDetector); ok {
77-
return detector.WriteBatchUnavailable(err)
78+
ret = detector.WriteBatchUnavailable(err)
7879
}
79-
return false
80+
return ret
8081
}

readloop.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ func (s *UDPSession) defaultReadLoop() {
3333
for {
3434
if n, addr, err := s.conn.ReadFrom(buf); err == nil {
3535
// make sure the packet is from the same source
36-
if addr.String() != src {
37-
if len(src) == 0 { // set source address
38-
src = addr.String()
39-
} else {
40-
atomic.AddUint64(&DefaultSnmp.InErrs, 1)
41-
continue
42-
}
36+
if src == "" { // set source address
37+
src = addr.String()
38+
} else if addr.String() != src {
39+
atomic.AddUint64(&DefaultSnmp.InErrs, 1)
40+
continue
4341
}
4442
s.packetInput(buf[:n])
4543
} else {

sess_test.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ func dialTinyBufferEcho(port int) (*UDPSession, error) {
9090
}
9191

9292
//////////////////////////
93+
type listenFn func(port int) (net.Listener, error)
94+
9395
func listenEcho(port int) (net.Listener, error) {
9496
//block, _ := NewNoneBlockCrypt(pass)
9597
//block, _ := NewSimpleXORBlockCrypt(pass)
@@ -113,7 +115,7 @@ func listenNoEncryption(port int) (net.Listener, error) {
113115

114116
func server(
115117
port int,
116-
listen func(port int) (net.Listener, error),
118+
listen listenFn,
117119
handle func(*UDPSession),
118120
) net.Listener {
119121
l, err := listen(port)
@@ -748,8 +750,25 @@ func (c *customBatchConn) WriteBatchUnavailable(err error) bool {
748750
return err.Error() == "unsupported"
749751
}
750752

753+
func batchListenFn(opt func(pconn *customBatchConn)) listenFn {
754+
return func(port int) (net.Listener, error) {
755+
udpaddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("127.0.0.1:%v", port))
756+
if err != nil {
757+
return nil, err
758+
}
759+
conn, err := net.ListenUDP("udp", udpaddr)
760+
if err != nil {
761+
return nil, err
762+
}
763+
pconn := &customBatchConn{UDPConn: conn}
764+
opt(pconn)
765+
return serveConn(nil, 0, 0, pconn, true)
766+
}
767+
}
768+
751769
func TestCustomBatchConn(t *testing.T) {
752-
l := server(0, listenNoEncryption, handleEcho)
770+
listen := batchListenFn(func(pconn *customBatchConn) {})
771+
l := server(0, listen, handleEcho)
753772
defer l.Close()
754773

755774
// Create a net.PacketConn not owned by the UDPSession.
@@ -794,7 +813,12 @@ func TestCustomBatchConn(t *testing.T) {
794813
}
795814

796815
func TestCustomBatchConnFallback(t *testing.T) {
797-
l := server(0, listenNoEncryption, handleEcho)
816+
// should fallback to defaultMonitor()
817+
listen := batchListenFn(func(pconn *customBatchConn) {
818+
pconn.disableReadBatch = true
819+
pconn.disableWriteBatch = true
820+
})
821+
l := server(0, listen, handleEcho)
798822
defer l.Close()
799823

800824
// Create a net.PacketConn not owned by the UDPSession.

0 commit comments

Comments
 (0)