diff options
author | FUJITA Tomonori <fujita.tomonori@gmail.com> | 2018-12-29 08:34:03 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@gmail.com> | 2018-12-29 08:34:03 +0900 |
commit | bbc85d46c345f6ee2f70f06fc80bba9c8c6e689c (patch) | |
tree | 84731f6878bdfd5b832d4cd70a30bbb369417f6b /pkg/server | |
parent | d7e5bf22a6ef7ebbed02968d5610968e3f06cfc6 (diff) |
server: kill getNeighbor()
Use ListPeer() instead.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Diffstat (limited to 'pkg/server')
-rw-r--r-- | pkg/server/server.go | 19 | ||||
-rw-r--r-- | pkg/server/server_test.go | 70 |
2 files changed, 37 insertions, 52 deletions
diff --git a/pkg/server/server.go b/pkg/server/server.go index 1f103c12..85b13c23 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -2474,25 +2474,6 @@ func (s *BgpServer) GetBgp(ctx context.Context, r *api.GetBgpRequest) (*api.GetB return rsp, nil } -func (s *BgpServer) getNeighbor(address string, getAdvertised bool) []*config.Neighbor { - var l []*config.Neighbor - s.mgmtOperation(func() error { - l = make([]*config.Neighbor, 0, len(s.neighborMap)) - for k, peer := range s.neighborMap { - peer.fsm.lock.RLock() - neighborIface := peer.fsm.pConf.Config.NeighborInterface - peer.fsm.lock.RUnlock() - if address != "" && address != k && address != neighborIface { - continue - } - // FIXME: should remove toConfig() conversion - l = append(l, s.toConfig(peer, getAdvertised)) - } - return nil - }, false) - return l -} - func (s *BgpServer) ListPeer(ctx context.Context, r *api.ListPeerRequest, fn func(*api.Peer)) error { var l []*api.Peer s.mgmtOperation(func() error { diff --git a/pkg/server/server_test.go b/pkg/server/server_test.go index 14124c79..804842bd 100644 --- a/pkg/server/server_test.go +++ b/pkg/server/server_test.go @@ -228,15 +228,17 @@ func TestMonitor(test *testing.T) { }, }, } + ch := make(chan struct{}) + go t.MonitorPeer(context.Background(), &api.MonitorPeerRequest{}, func(peer *api.Peer) { + if peer.State.SessionState == api.PeerState_ESTABLISHED { + close(ch) + } + }) + err = t.AddPeer(context.Background(), &api.AddPeerRequest{Peer: config.NewPeerFromConfigStruct(m)}) assert.Nil(err) - for { - time.Sleep(time.Second) - if t.getNeighbor("", false)[0].State.SessionState == config.SESSION_STATE_ESTABLISHED { - break - } - } + <-ch // Test WatchBestPath. w := s.watch(watchBestPath(false)) @@ -619,15 +621,15 @@ func TestPeerGroup(test *testing.T) { }, }, } + ch := make(chan struct{}) + go t.MonitorPeer(context.Background(), &api.MonitorPeerRequest{}, func(peer *api.Peer) { + if peer.State.SessionState == api.PeerState_ESTABLISHED { + close(ch) + } + }) err = t.AddPeer(context.Background(), &api.AddPeerRequest{Peer: config.NewPeerFromConfigStruct(m)}) assert.Nil(err) - - for { - time.Sleep(time.Second) - if t.getNeighbor("", false)[0].State.SessionState == config.SESSION_STATE_ESTABLISHED { - break - } - } + <-ch } func TestDynamicNeighbor(t *testing.T) { @@ -698,16 +700,15 @@ func TestDynamicNeighbor(t *testing.T) { }, }, } + ch := make(chan struct{}) + go s2.MonitorPeer(context.Background(), &api.MonitorPeerRequest{}, func(peer *api.Peer) { + if peer.State.SessionState == api.PeerState_ESTABLISHED { + close(ch) + } + }) err = s2.AddPeer(context.Background(), &api.AddPeerRequest{Peer: config.NewPeerFromConfigStruct(m)}) - assert.Nil(err) - - for { - time.Sleep(time.Second) - if s2.getNeighbor("", false)[0].State.SessionState == config.SESSION_STATE_ESTABLISHED { - break - } - } + <-ch } func TestGracefulRestartTimerExpired(t *testing.T) { @@ -780,16 +781,15 @@ func TestGracefulRestartTimerExpired(t *testing.T) { }, }, } + ch := make(chan struct{}) + go s2.MonitorPeer(context.Background(), &api.MonitorPeerRequest{}, func(peer *api.Peer) { + if peer.State.SessionState == api.PeerState_ESTABLISHED { + close(ch) + } + }) err = s2.addNeighbor(m) assert.Nil(err) - - // Waiting for BGP session established. - for { - time.Sleep(time.Second) - if s2.getNeighbor("", false)[0].State.SessionState == config.SESSION_STATE_ESTABLISHED { - break - } - } + <-ch // Force TCP session disconnected in order to cause Graceful Restart at s1 // side. @@ -812,14 +812,18 @@ func TestGracefulRestartTimerExpired(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() + done := make(chan struct{}) // Waiting for Graceful Restart timer expired and moving on to IDLE state. for { - if s1.getNeighbor("", false)[0].State.SessionState == config.SESSION_STATE_IDLE { - break - } + s1.ListPeer(context.Background(), &api.ListPeerRequest{}, func(peer *api.Peer) { + if peer.State.SessionState == api.PeerState_IDLE { + close(done) + } + }) select { - case <-time.After(time.Second): + case <-done: + return case <-ctx.Done(): t.Fatalf("failed to enter IDLE state in the deadline") return |