...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/topology/connection_errors_test.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/topology

     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  //go:build go1.13
     8  // +build go1.13
     9  
    10  package topology
    11  
    12  import (
    13  	"context"
    14  	"errors"
    15  	"net"
    16  	"testing"
    17  
    18  	"go.mongodb.org/mongo-driver/internal/assert"
    19  	"go.mongodb.org/mongo-driver/mongo/address"
    20  	"go.mongodb.org/mongo-driver/x/mongo/driver/auth"
    21  )
    22  
    23  func TestConnectionErrors(t *testing.T) {
    24  	t.Run("errors are wrapped", func(t *testing.T) {
    25  		t.Run("dial error", func(t *testing.T) {
    26  			dialError := errors.New("foo")
    27  
    28  			conn := newConnection(address.Address(""), WithDialer(func(Dialer) Dialer {
    29  				return DialerFunc(func(context.Context, string, string) (net.Conn, error) { return nil, dialError })
    30  			}))
    31  
    32  			err := conn.connect(context.Background())
    33  			assert.True(t, errors.Is(err, dialError), "expected error %v, got %v", dialError, err)
    34  		})
    35  		t.Run("handshake error", func(t *testing.T) {
    36  			conn := newConnection(address.Address(""),
    37  				WithHandshaker(func(Handshaker) Handshaker {
    38  					return auth.Handshaker(nil, &auth.HandshakeOptions{})
    39  				}),
    40  				WithDialer(func(Dialer) Dialer {
    41  					return DialerFunc(func(context.Context, string, string) (net.Conn, error) {
    42  						return &net.TCPConn{}, nil
    43  					})
    44  				}),
    45  			)
    46  			defer conn.close()
    47  
    48  			ctx, cancel := context.WithCancel(context.Background())
    49  			cancel()
    50  			err := conn.connect(ctx)
    51  			assert.True(t, errors.Is(err, context.Canceled), "expected error %v, got %v", context.Canceled, err)
    52  		})
    53  	})
    54  }
    55  

View as plain text