Skip to content

Commit 1167931

Browse files
committed
add tests using inmemory listener
1 parent b554c27 commit 1167931

File tree

4 files changed

+184
-83
lines changed

4 files changed

+184
-83
lines changed

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ module github.com/bio-routing/bio-rd
22

33
replace github.com/valyala/fasthttp => github.com/tobikris/fasthttp v0.0.0-20221130175052-07711a7959b1
44

5+
replace github.com/stretchr/testify => github.com/tobikris/testify v0.0.0-20221024223348-988ad3449f13
6+
57
require (
68
github.com/bio-routing/tflow2 v0.0.0-20181230153523-2e308a4a3c3a
79
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0
810
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
911
github.com/prometheus/client_golang v1.0.0
1012
github.com/sirupsen/logrus v1.3.0
11-
github.com/stretchr/testify v1.7.0
13+
github.com/stretchr/testify v1.7.1
1214
github.com/urfave/cli v1.21.0
1315
github.com/valyala/fasthttp v1.41.0
1416
github.com/vishvananda/netlink v1.0.0
@@ -34,7 +36,7 @@ require (
3436
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
3537
golang.org/x/text v0.3.7 // indirect
3638
google.golang.org/genproto v0.0.0-20211129164237-f09f9a12af12 // indirect
37-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
39+
gopkg.in/yaml.v3 v3.0.1 // indirect
3840
)
3941

4042
go 1.18

go.sum

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWH
2323
github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
2424
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
2525
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
26-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2726
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2827
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2928
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@@ -103,15 +102,12 @@ github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx
103102
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
104103
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
105104
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
106-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
107105
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
108-
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
109-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
110-
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
111-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
112-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
106+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
113107
github.com/tobikris/fasthttp v0.0.0-20221130175052-07711a7959b1 h1:qQBkpMAB9K4jfdsuwg9L3UE3gnYoE3KXwe5/f5zYEGo=
114108
github.com/tobikris/fasthttp v0.0.0-20221130175052-07711a7959b1/go.mod h1:f6VbjjoI3z1NDOZOv17o6RvtRSWxC77seBFc2uWtgiY=
109+
github.com/tobikris/testify v0.0.0-20221024223348-988ad3449f13 h1:Blvp02EGsZuEvLqCInVNM/hi7Va2dx7qItAmdCGsT+M=
110+
github.com/tobikris/testify v0.0.0-20221024223348-988ad3449f13/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
115111
github.com/urfave/cli v1.21.0 h1:wYSSj06510qPIzGSua9ZqsncMmWE3Zr55KBERygyrxE=
116112
github.com/urfave/cli v1.21.0/go.mod h1:lxDj6qX9Q6lWQxIrbrT0nwecwUtRnhVZAJjJZrVUZZQ=
117113
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
@@ -218,7 +214,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
218214
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
219215
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
220216
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
221-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
222-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
217+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
218+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
223219
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
224220
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

protocols/bgp/server/server.go

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ const (
2020
)
2121

2222
type bgpServer struct {
23-
listeners []listener
24-
acceptCh chan net.Conn
25-
peers *peerManager
26-
routerID uint32
27-
metrics *metricsService
28-
logger log.LoggerInterface
23+
addListeners chan listener
24+
acceptedListeners []listener
25+
acceptCh chan net.Conn
26+
peers *peerManager
27+
routerID uint32
28+
metrics *metricsService
29+
logger log.LoggerInterface
2930
}
3031

3132
type BGPServer interface {
@@ -37,6 +38,7 @@ type BGPServer interface {
3738
GetPeerConfig(*bnet.IP) *PeerConfig
3839
DisposePeer(*bnet.IP)
3940
GetPeers() []*bnet.IP
41+
GetPeerStatus(*bnet.IP) string
4042
Metrics() (*metrics.BGPMetrics, error)
4143
GetRIBIn(peerIP *bnet.IP, afi uint16, safi uint8) *adjRIBIn.AdjRIBIn
4244
GetRIBOut(peerIP *bnet.IP, afi uint16, safi uint8) *adjRIBOut.AdjRIBOut
@@ -52,9 +54,10 @@ func NewBGPServer(routerID uint32) BGPServer {
5254

5355
func newBGPServer(routerID uint32) *bgpServer {
5456
server := &bgpServer{
55-
peers: newPeerManager(),
56-
routerID: routerID,
57-
listeners: make([]listener, 0),
57+
peers: newPeerManager(),
58+
routerID: routerID,
59+
addListeners: make(chan listener, 256),
60+
acceptedListeners: make([]listener, 0),
5861
logger: log.GetLogger().WithFields(log.Fields{
5962
"router_id": bnet.IPv4(routerID).String(),
6063
}),
@@ -99,12 +102,12 @@ func (d *dummyListener) setTCPMD5(net.IP, string) error {
99102
func (b *bgpServer) AddListener(l ...net.Listener) error {
100103
for _, l := range l {
101104
if ll, ok := l.(listener); ok {
102-
b.listeners = append(b.listeners, ll)
105+
b.addListeners <- ll
103106
} else {
104107
d := &dummyListener{
105108
Listener: l,
106109
logger: b.logger}
107-
b.listeners = append(b.listeners, d)
110+
b.addListeners <- d
108111
}
109112
}
110113

@@ -126,28 +129,30 @@ func (b *bgpServer) AddListenerFromAddrString(addrs ...string) error {
126129
return nil
127130
}
128131

129-
func (b *bgpServer) Start() error {
130-
if len(b.listeners) > 0 {
131-
acceptCh := make(chan net.Conn, 4096)
132-
133-
for _, addr := range b.listeners {
134-
go func(addr listener) {
135-
for {
136-
conn, err := addr.Accept()
137-
138-
if err != nil {
139-
b.logger.Errorf("failed to accept connection: %v", err)
140-
continue
141-
}
142-
143-
acceptCh <- conn
144-
}
145-
}(addr)
132+
func (b *bgpServer) accept(addr listener, acceptCh chan net.Conn) {
133+
for {
134+
conn, err := addr.Accept()
135+
136+
if err != nil {
137+
b.logger.Errorf("failed to accept connection: %v", err)
138+
continue
146139
}
147-
b.acceptCh = acceptCh
148140

149-
go b.incomingConnectionWorker()
141+
acceptCh <- conn
150142
}
143+
}
144+
145+
func (b *bgpServer) Start() error {
146+
b.acceptCh = make(chan net.Conn, 4096)
147+
148+
go b.incomingConnectionWorker()
149+
150+
go func() {
151+
for addr := range b.addListeners {
152+
go b.accept(addr, b.acceptCh)
153+
b.acceptedListeners = append(b.acceptedListeners, addr)
154+
}
155+
}()
151156

152157
return nil
153158
}
@@ -264,7 +269,7 @@ func (b *bgpServer) AddPeer(c PeerConfig) error {
264269
}
265270

266271
if c.AuthenticationKey != "" {
267-
for _, l := range b.listeners {
272+
for _, l := range b.acceptedListeners {
268273
err = l.setTCPMD5(c.PeerAddress.ToNetIP(), c.AuthenticationKey)
269274
if err != nil {
270275
return fmt.Errorf("unable to set TCP MD5 secret: %w", err)
@@ -298,6 +303,19 @@ func (b *bgpServer) GetPeerConfig(addr *bnet.IP) *PeerConfig {
298303
return nil
299304
}
300305

306+
// GetPeerStatus gets the status of a BGP peer by its address
307+
func (b *bgpServer) GetPeerStatus(addr *bnet.IP) string {
308+
p := b.peers.get(addr)
309+
if p != nil {
310+
p.fsms[0].stateMu.RLock()
311+
defer p.fsms[0].stateMu.RUnlock()
312+
313+
return stateName(p.fsms[0].state)
314+
}
315+
316+
return ""
317+
}
318+
301319
func (b *bgpServer) DisposePeer(addr *bnet.IP) {
302320
p := b.peers.get(addr)
303321
if p == nil {

0 commit comments

Comments
 (0)