...

Source file src/cloud.google.com/go/auth/grpctransport/dial_socketopt_test.go

Documentation: cloud.google.com/go/auth/grpctransport

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build linux
    16  // +build linux
    17  
    18  package grpctransport
    19  
    20  import (
    21  	"context"
    22  	"errors"
    23  	"fmt"
    24  	"net"
    25  	"syscall"
    26  	"testing"
    27  	"time"
    28  
    29  	"cloud.google.com/go/auth"
    30  	"google.golang.org/grpc"
    31  )
    32  
    33  func TestDialTCPUserTimeout(t *testing.T) {
    34  	l, err := net.Listen("tcp", ":3000")
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	defer l.Close()
    39  
    40  	acceptErrCh := make(chan error, 1)
    41  
    42  	go func() {
    43  		conn, err := l.Accept()
    44  		if err != nil {
    45  			acceptErrCh <- err
    46  			return
    47  		}
    48  		defer conn.Close()
    49  
    50  		if err := conn.Close(); err != nil {
    51  			acceptErrCh <- err
    52  		}
    53  	}()
    54  
    55  	conn, err := dialTCPUserTimeout(context.Background(), ":3000")
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	defer conn.Close()
    60  
    61  	timeout, err := getTCPUserTimeout(conn)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	if timeout != tcpUserTimeoutMilliseconds {
    66  		t.Fatalf("expected %v, got %v", tcpUserTimeoutMilliseconds, timeout)
    67  	}
    68  
    69  	select {
    70  	case err := <-acceptErrCh:
    71  		t.Fatalf("Accept failed with: %v", err)
    72  	default:
    73  	}
    74  }
    75  
    76  func getTCPUserTimeout(conn net.Conn) (int, error) {
    77  	tcpConn, ok := conn.(*net.TCPConn)
    78  	if !ok {
    79  		return 0, fmt.Errorf("conn is not *net.TCPConn. got %T", conn)
    80  	}
    81  	rawConn, err := tcpConn.SyscallConn()
    82  	if err != nil {
    83  		return 0, err
    84  	}
    85  	var timeout int
    86  	var syscallErr error
    87  	controlErr := rawConn.Control(func(fd uintptr) {
    88  		timeout, syscallErr = syscall.GetsockoptInt(int(fd), syscall.IPPROTO_TCP, tcpUserTimeoutOp)
    89  	})
    90  	if syscallErr != nil {
    91  		return 0, syscallErr
    92  	}
    93  	if controlErr != nil {
    94  		return 0, controlErr
    95  	}
    96  	return timeout, nil
    97  }
    98  
    99  // Check that tcp timeout dialer overwrites user defined dialer.
   100  func TestDialWithDirectPathEnabled(t *testing.T) {
   101  	t.Skip("https://github.com/googleapis/google-api-go-client/issues/790")
   102  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
   103  
   104  	userDialer := grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
   105  		t.Error("did not expect a call to user dialer, got one")
   106  		cancel()
   107  		return nil, errors.New("not expected")
   108  	})
   109  
   110  	pool, err := Dial(ctx, true, &Options{
   111  		Credentials: auth.NewCredentials(&auth.CredentialsOptions{
   112  			TokenProvider: &staticTP{tok: &auth.Token{Value: "hey"}},
   113  		}),
   114  		GRPCDialOpts: []grpc.DialOption{userDialer},
   115  		Endpoint:     "example.google.com:443",
   116  		InternalOptions: &InternalOptions{
   117  			EnableDirectPath: true,
   118  		},
   119  	})
   120  	if err != nil {
   121  		t.Errorf("DialGRPC: error %v, want nil", err)
   122  	}
   123  	defer pool.Close()
   124  
   125  	// gRPC doesn't connect before the first call.
   126  	grpc.Invoke(ctx, "foo", nil, nil, pool.Connection())
   127  }
   128  

View as plain text