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