1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// +build go1.13
package topology
import (
"context"
"errors"
"net"
"testing"
"go.mongodb.org/mongo-driver/internal/testutil/assert"
"go.mongodb.org/mongo-driver/mongo/address"
"go.mongodb.org/mongo-driver/x/mongo/driver/auth"
)
func TestConnectionErrors(t *testing.T) {
t.Run("errors are wrapped", func(t *testing.T) {
t.Run("dial error", func(t *testing.T) {
dialError := errors.New("foo")
conn, err := newConnection(address.Address(""), WithDialer(func(Dialer) Dialer {
return DialerFunc(func(context.Context, string, string) (net.Conn, error) { return nil, dialError })
}))
assert.Nil(t, err, "newConnection error: %v", err)
conn.connect(context.Background())
err = conn.wait()
assert.True(t, errors.Is(err, dialError), "expected error %v, got %v", dialError, err)
})
t.Run("handshake error", func(t *testing.T) {
conn, err := newConnection(address.Address(""),
WithHandshaker(func(Handshaker) Handshaker {
return auth.Handshaker(nil, &auth.HandshakeOptions{})
}),
WithDialer(func(Dialer) Dialer {
return DialerFunc(func(context.Context, string, string) (net.Conn, error) {
return &net.TCPConn{}, nil
})
}),
)
assert.Nil(t, err, "newConnection error: %v", err)
defer conn.close()
ctx, cancel := context.WithCancel(context.Background())
cancel()
conn.connect(ctx)
err = conn.wait()
assert.True(t, errors.Is(err, context.Canceled), "expected error %v, got %v", context.Canceled, err)
})
t.Run("write error", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
conn := &connection{id: "foobar", nc: &net.TCPConn{}, connected: connected}
err := conn.writeWireMessage(ctx, []byte{})
assert.True(t, errors.Is(err, context.Canceled), "expected error %v, got %v", context.Canceled, err)
})
t.Run("read error", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
conn := &connection{id: "foobar", nc: &net.TCPConn{}, connected: connected}
_, err := conn.readWireMessage(ctx, []byte{})
assert.True(t, errors.Is(err, context.Canceled), "expected error %v, got %v", context.Canceled, err)
})
})
}
|