Add unit test to verify conn is closed when start fails (#304)

* Add unit test to verify conn is closed when start fails

* add negative test

* simplify else clause
This commit is contained in:
Joel Hendrix 2023-08-28 07:00:02 -07:00 коммит произвёл GitHub
Родитель 87b9199f11
Коммит a74ebbb50a
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 15 добавлений и 5 удалений

Просмотреть файл

@ -111,6 +111,7 @@ func Dial(ctx context.Context, addr string, opts *ConnOptions) (*Conn, error) {
}
// NewConn establishes a new AMQP client connection over conn.
// NOTE: [Conn] takes ownership of the provided [net.Conn] and will close it as required.
// opts: pass nil to accept the default values.
func NewConn(ctx context.Context, conn net.Conn, opts *ConnOptions) (*Conn, error) {
c, err := newConn(conn, opts)

Просмотреть файл

@ -278,10 +278,16 @@ func TestStart(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
err = conn.start(ctx)
cancel()
if tt.fails && err == nil {
t.Error("unexpected nil error")
} else if !tt.fails && err != nil {
t.Error(err)
if tt.fails {
require.Error(t, err)
// verify that the conn was closed
err := netConn.Close()
require.ErrorIs(t, err, fake.ErrAlreadyClosed)
} else {
require.NoError(t, err)
// verify that the conn wasn't closed
err := netConn.Close()
require.NoError(t, err)
}
})
}

Просмотреть файл

@ -98,6 +98,9 @@ type Response struct {
WriteDelay time.Duration
}
// ErrAlreadyClosed is returned by Close() if [NetConn] is already closed.
var ErrAlreadyClosed = errors.New("fake already closed")
///////////////////////////////////////////////////////
// following methods are for the net.Conn interface
///////////////////////////////////////////////////////
@ -188,7 +191,7 @@ func (n *NetConn) write() {
// Close is called by conn.close.
func (n *NetConn) Close() error {
if n.closed {
return errors.New("double close")
return ErrAlreadyClosed
}
n.closed = true
close(n.close)