diff --git a/protocols/bgp/server/bmp_router_test.go b/protocols/bgp/server/bmp_router_test.go index 3c74e6b82b358c964b5ed58f6a2a8d3b28944de9..f17208b141b9a286dd852c7e301f2700d329c045 100644 --- a/protocols/bgp/server/bmp_router_test.go +++ b/protocols/bgp/server/bmp_router_test.go @@ -64,7 +64,7 @@ func TestStartStopBMP(t *testing.T) { r.stop <- struct{}{} r.runMu.Lock() - assert.Equal(t, true, r.con.(*biotesting.MockConn).Closed) + assert.Equal(t, true, r.con.(*biotesting.MockConn).Closed()) } func TestConfigureBySentOpen(t *testing.T) { @@ -478,7 +478,7 @@ func TestProcessTerminationMsg(t *testing.T) { { name: "Test shutdown", r: &router{ - con: &biotesting.MockConn{}, + con: biotesting.NewMockConn(), address: net.IP{10, 20, 30, 40}, logger: log.New(), neighbors: map[[16]byte]*neighbor{ @@ -495,9 +495,7 @@ func TestProcessTerminationMsg(t *testing.T) { }, expectedLog: "level=warning msg=\"Received termination message from 10.20.30.40: Message: \\\"Foo Bar\\\"\"", expected: &router{ - con: &biotesting.MockConn{ - Closed: true, - }, + con: biotesting.NewMockConnClosed(), address: net.IP{10, 20, 30, 40}, neighbors: map[[16]byte]*neighbor{}, }, @@ -538,9 +536,7 @@ func TestProcessTerminationMsg(t *testing.T) { }, expectedLog: "level=warning msg=\"Received termination message from 10.20.30.40: Session administratively downUnespcified reasonOut of resourcesRedundant connectionSession permanently administratively closed\"", expected: &router{ - con: &biotesting.MockConn{ - Closed: true, - }, + con: biotesting.NewMockConnClosed(), address: net.IP{10, 20, 30, 40}, neighbors: map[[16]byte]*neighbor{}, }, @@ -1452,7 +1448,7 @@ func TestIntegrationIncompleteBMPMsg(t *testing.T) { r.serve(con) - if !con.Closed { + if !con.Closed() { t.Errorf("Connection not closed although failure should have happened") } } @@ -1615,6 +1611,7 @@ func TestBMPFullRunWithWithdraw(t *testing.T) { }) time.Sleep(time.Millisecond * 50) + assert.NotEmpty(t, r.neighbors) time.Sleep(time.Millisecond * 50) diff --git a/testing/conn_mock.go b/testing/conn_mock.go index 1be39e05208aa91d67fd841b2de740bfdc15d5da..1c62b88b5bb489c36430c85c960a4071e823bb8c 100644 --- a/testing/conn_mock.go +++ b/testing/conn_mock.go @@ -3,30 +3,56 @@ package testing import ( "bytes" "net" + "sync" ) // MockConn mock an connection type MockConn struct { net.Conn - Buf *bytes.Buffer - Closed bool + buf *bytes.Buffer + lock sync.Mutex + closed bool } func NewMockConn() *MockConn { return &MockConn{ - Buf: bytes.NewBuffer(nil), + buf: bytes.NewBuffer(nil), } } +func NewMockConnClosed() *MockConn { + m := NewMockConn() + m.buf = bytes.NewBuffer(nil) + m.closed = true + return m +} + func (m *MockConn) Write(b []byte) (int, error) { - return m.Buf.Write(b) + m.lock.Lock() + defer m.lock.Unlock() + + return m.buf.Write(b) } func (m *MockConn) Read(b []byte) (n int, err error) { - return m.Buf.Read(b) + m.lock.Lock() + defer m.lock.Unlock() + + return m.buf.Read(b) } func (m *MockConn) Close() error { - m.Closed = true + m.lock.Lock() + defer m.lock.Unlock() + + m.buf = bytes.NewBuffer(nil) + m.closed = true return nil } + +func (m *MockConn) Closed() bool { + m.lock.Lock() + defer m.lock.Unlock() + + return m.closed +}