...

Source file src/google.golang.org/api/transport/grpc/dial_socketopt_test.go

Documentation: google.golang.org/api/transport/grpc

     1  // Copyright 2019 Google LLC.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build go1.11 && linux
     6  // +build go1.11,linux
     7  
     8  package grpc
     9  
    10  import (
    11  	"context"
    12  	"errors"
    13  	"fmt"
    14  	"net"
    15  	"syscall"
    16  	"testing"
    17  	"time"
    18  
    19  	"golang.org/x/oauth2"
    20  	"google.golang.org/api/option"
    21  	"google.golang.org/api/option/internaloption"
    22  	"google.golang.org/grpc"
    23  )
    24  
    25  func TestDialTCPUserTimeout(t *testing.T) {
    26  	l, err := net.Listen("tcp", ":3000")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	defer l.Close()
    31  
    32  	acceptErrCh := make(chan error, 1)
    33  
    34  	go func() {
    35  		conn, err := l.Accept()
    36  		if err != nil {
    37  			acceptErrCh <- err
    38  			return
    39  		}
    40  		defer conn.Close()
    41  
    42  		if err := conn.Close(); err != nil {
    43  			acceptErrCh <- err
    44  		}
    45  	}()
    46  
    47  	conn, err := dialTCPUserTimeout(context.Background(), ":3000")
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	defer conn.Close()
    52  
    53  	timeout, err := getTCPUserTimeout(conn)
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	if timeout != tcpUserTimeoutMilliseconds {
    58  		t.Fatalf("expected %v, got %v", tcpUserTimeoutMilliseconds, timeout)
    59  	}
    60  
    61  	select {
    62  	case err := <-acceptErrCh:
    63  		t.Fatalf("Accept failed with: %v", err)
    64  	default:
    65  	}
    66  }
    67  
    68  func getTCPUserTimeout(conn net.Conn) (int, error) {
    69  	tcpconn, ok := conn.(*net.TCPConn)
    70  	if !ok {
    71  		return 0, fmt.Errorf("conn is not *net.TCPConn. got %T", conn)
    72  	}
    73  	rawConn, err := tcpconn.SyscallConn()
    74  	if err != nil {
    75  		return 0, err
    76  	}
    77  	var timeout int
    78  	var syscalErr error
    79  	controlErr := rawConn.Control(func(fd uintptr) {
    80  		timeout, syscalErr = syscall.GetsockoptInt(int(fd), syscall.IPPROTO_TCP, tcpUserTimeoutOp)
    81  	})
    82  	if syscalErr != nil {
    83  		return 0, syscalErr
    84  	}
    85  	if controlErr != nil {
    86  		return 0, controlErr
    87  	}
    88  	return timeout, nil
    89  }
    90  
    91  // Check that tcp timeout dialer overwrites user defined dialer.
    92  func TestDialWithDirectPathEnabled(t *testing.T) {
    93  	t.Skip("https://github.com/googleapis/google-api-go-client/issues/790")
    94  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
    95  
    96  	userDialer := grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
    97  		t.Error("did not expect a call to user dialer, got one")
    98  		cancel()
    99  		return nil, errors.New("not expected")
   100  	})
   101  
   102  	conn, err := Dial(ctx,
   103  		option.WithTokenSource(oauth2.StaticTokenSource(nil)), // No creds.
   104  		option.WithGRPCDialOption(userDialer),
   105  		option.WithEndpoint("example.google.com:443"),
   106  		internaloption.EnableDirectPath(true))
   107  	if err != nil {
   108  		t.Errorf("DialGRPC: error %v, want nil", err)
   109  	}
   110  	defer conn.Close()
   111  
   112  	// gRPC doesn't connect before the first call.
   113  	grpc.Invoke(ctx, "foo", nil, nil, conn)
   114  }
   115  

View as plain text