google.golang.org/grpc@v1.72.2/balancer/pickfirst/pickfirstleaf/pickfirstleaf_test.go (about)

     1  /*
     2   *
     3   * Copyright 2024 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package pickfirstleaf
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"testing"
    25  	"time"
    26  
    27  	"google.golang.org/grpc/attributes"
    28  	"google.golang.org/grpc/balancer"
    29  	"google.golang.org/grpc/connectivity"
    30  	"google.golang.org/grpc/internal/grpctest"
    31  	"google.golang.org/grpc/internal/testutils"
    32  	"google.golang.org/grpc/resolver"
    33  )
    34  
    35  const (
    36  	// Default timeout for tests in this package.
    37  	defaultTestTimeout = 10 * time.Second
    38  	// Default short timeout, to be used when waiting for events which are not
    39  	// expected to happen.
    40  	defaultTestShortTimeout = 100 * time.Millisecond
    41  )
    42  
    43  type s struct {
    44  	grpctest.Tester
    45  }
    46  
    47  func Test(t *testing.T) {
    48  	grpctest.RunSubTests(t, s{})
    49  }
    50  
    51  // TestAddressList_Iteration verifies the behaviour of the addressList while
    52  // iterating through the entries.
    53  func (s) TestAddressList_Iteration(t *testing.T) {
    54  	addrs := []resolver.Address{
    55  		{
    56  			Addr:               "192.168.1.1",
    57  			ServerName:         "test-host-1",
    58  			Attributes:         attributes.New("key-1", "val-1"),
    59  			BalancerAttributes: attributes.New("bal-key-1", "bal-val-1"),
    60  		},
    61  		{
    62  			Addr:               "192.168.1.2",
    63  			ServerName:         "test-host-2",
    64  			Attributes:         attributes.New("key-2", "val-2"),
    65  			BalancerAttributes: attributes.New("bal-key-2", "bal-val-2"),
    66  		},
    67  		{
    68  			Addr:               "192.168.1.3",
    69  			ServerName:         "test-host-3",
    70  			Attributes:         attributes.New("key-3", "val-3"),
    71  			BalancerAttributes: attributes.New("bal-key-3", "bal-val-3"),
    72  		},
    73  	}
    74  
    75  	addressList := addressList{}
    76  	addressList.updateAddrs(addrs)
    77  
    78  	for i := 0; i < len(addrs); i++ {
    79  		if got, want := addressList.isValid(), true; got != want {
    80  			t.Fatalf("addressList.isValid() = %t, want %t", got, want)
    81  		}
    82  		if got, want := addressList.currentAddress(), addrs[i]; !want.Equal(got) {
    83  			t.Errorf("addressList.currentAddress() = %v, want %v", got, want)
    84  		}
    85  		if got, want := addressList.increment(), i+1 < len(addrs); got != want {
    86  			t.Fatalf("addressList.increment() = %t, want %t", got, want)
    87  		}
    88  	}
    89  
    90  	if got, want := addressList.isValid(), false; got != want {
    91  		t.Fatalf("addressList.isValid() = %t, want %t", got, want)
    92  	}
    93  
    94  	// increment an invalid address list.
    95  	if got, want := addressList.increment(), false; got != want {
    96  		t.Errorf("addressList.increment() = %t, want %t", got, want)
    97  	}
    98  
    99  	if got, want := addressList.isValid(), false; got != want {
   100  		t.Errorf("addressList.isValid() = %t, want %t", got, want)
   101  	}
   102  
   103  	addressList.reset()
   104  	for i := 0; i < len(addrs); i++ {
   105  		if got, want := addressList.isValid(), true; got != want {
   106  			t.Fatalf("addressList.isValid() = %t, want %t", got, want)
   107  		}
   108  		if got, want := addressList.currentAddress(), addrs[i]; !want.Equal(got) {
   109  			t.Errorf("addressList.currentAddress() = %v, want %v", got, want)
   110  		}
   111  		if got, want := addressList.increment(), i+1 < len(addrs); got != want {
   112  			t.Fatalf("addressList.increment() = %t, want %t", got, want)
   113  		}
   114  	}
   115  }
   116  
   117  // TestAddressList_SeekTo verifies the behaviour of addressList.seekTo.
   118  func (s) TestAddressList_SeekTo(t *testing.T) {
   119  	addrs := []resolver.Address{
   120  		{
   121  			Addr:               "192.168.1.1",
   122  			ServerName:         "test-host-1",
   123  			Attributes:         attributes.New("key-1", "val-1"),
   124  			BalancerAttributes: attributes.New("bal-key-1", "bal-val-1"),
   125  		},
   126  		{
   127  			Addr:               "192.168.1.2",
   128  			ServerName:         "test-host-2",
   129  			Attributes:         attributes.New("key-2", "val-2"),
   130  			BalancerAttributes: attributes.New("bal-key-2", "bal-val-2"),
   131  		},
   132  		{
   133  			Addr:               "192.168.1.3",
   134  			ServerName:         "test-host-3",
   135  			Attributes:         attributes.New("key-3", "val-3"),
   136  			BalancerAttributes: attributes.New("bal-key-3", "bal-val-3"),
   137  		},
   138  	}
   139  
   140  	addressList := addressList{}
   141  	addressList.updateAddrs(addrs)
   142  
   143  	// Try finding an address in the list.
   144  	key := resolver.Address{
   145  		Addr:               "192.168.1.2",
   146  		ServerName:         "test-host-2",
   147  		Attributes:         attributes.New("key-2", "val-2"),
   148  		BalancerAttributes: attributes.New("ignored", "bal-val-2"),
   149  	}
   150  
   151  	if got, want := addressList.seekTo(key), true; got != want {
   152  		t.Errorf("addressList.seekTo(%v) = %t, want %t", key, got, want)
   153  	}
   154  
   155  	// It should be possible to increment once more now that the pointer has advanced.
   156  	if got, want := addressList.increment(), true; got != want {
   157  		t.Errorf("addressList.increment() = %t, want %t", got, want)
   158  	}
   159  
   160  	if got, want := addressList.increment(), false; got != want {
   161  		t.Errorf("addressList.increment() = %t, want %t", got, want)
   162  	}
   163  
   164  	// Seek to the key again, it is behind the pointer now.
   165  	if got, want := addressList.seekTo(key), true; got != want {
   166  		t.Errorf("addressList.seekTo(%v) = %t, want %t", key, got, want)
   167  	}
   168  
   169  	// Seek to a key not in the list.
   170  	key = resolver.Address{
   171  		Addr:               "192.168.1.5",
   172  		ServerName:         "test-host-5",
   173  		Attributes:         attributes.New("key-5", "val-5"),
   174  		BalancerAttributes: attributes.New("ignored", "bal-val-5"),
   175  	}
   176  
   177  	if got, want := addressList.seekTo(key), false; got != want {
   178  		t.Errorf("addressList.seekTo(%v) = %t, want %t", key, got, want)
   179  	}
   180  
   181  	// It should be possible to increment once more since the pointer has not advanced.
   182  	if got, want := addressList.increment(), true; got != want {
   183  		t.Errorf("addressList.increment() = %t, want %t", got, want)
   184  	}
   185  
   186  	if got, want := addressList.increment(), false; got != want {
   187  		t.Errorf("addressList.increment() = %t, want %t", got, want)
   188  	}
   189  }
   190  
   191  // TestPickFirstLeaf_TFPickerUpdate sends TRANSIENT_FAILURE SubConn state updates
   192  // for each SubConn managed by a pickfirst balancer. It verifies that the picker
   193  // is updated with the expected frequency.
   194  func (s) TestPickFirstLeaf_TFPickerUpdate(t *testing.T) {
   195  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
   196  	defer cancel()
   197  	cc := testutils.NewBalancerClientConn(t)
   198  	bal := pickfirstBuilder{}.Build(cc, balancer.BuildOptions{})
   199  	defer bal.Close()
   200  	ccState := balancer.ClientConnState{
   201  		ResolverState: resolver.State{
   202  			Endpoints: []resolver.Endpoint{
   203  				{Addresses: []resolver.Address{{Addr: "1.1.1.1:1"}}},
   204  				{Addresses: []resolver.Address{{Addr: "2.2.2.2:2"}}},
   205  			},
   206  		},
   207  	}
   208  	if err := bal.UpdateClientConnState(ccState); err != nil {
   209  		t.Fatalf("UpdateClientConnState(%v) returned error: %v", ccState, err)
   210  	}
   211  
   212  	// PF should report TRANSIENT_FAILURE only once all the sunbconns have failed
   213  	// once.
   214  	tfErr := fmt.Errorf("test err: connection refused")
   215  	sc1 := <-cc.NewSubConnCh
   216  	sc1.UpdateState(balancer.SubConnState{ConnectivityState: connectivity.Connecting})
   217  	sc1.UpdateState(balancer.SubConnState{ConnectivityState: connectivity.TransientFailure, ConnectionError: tfErr})
   218  
   219  	if err := cc.WaitForPickerWithErr(ctx, balancer.ErrNoSubConnAvailable); err != nil {
   220  		t.Fatalf("cc.WaitForPickerWithErr(%v) returned error: %v", balancer.ErrNoSubConnAvailable, err)
   221  	}
   222  
   223  	sc2 := <-cc.NewSubConnCh
   224  	sc2.UpdateState(balancer.SubConnState{ConnectivityState: connectivity.Connecting})
   225  	sc2.UpdateState(balancer.SubConnState{ConnectivityState: connectivity.TransientFailure, ConnectionError: tfErr})
   226  
   227  	if err := cc.WaitForPickerWithErr(ctx, tfErr); err != nil {
   228  		t.Fatalf("cc.WaitForPickerWithErr(%v) returned error: %v", tfErr, err)
   229  	}
   230  
   231  	// Subsequent TRANSIENT_FAILUREs should be reported only after seeing "# of SubConns"
   232  	// TRANSIENT_FAILUREs.
   233  	newTfErr := fmt.Errorf("test err: unreachable")
   234  	sc2.UpdateState(balancer.SubConnState{ConnectivityState: connectivity.TransientFailure, ConnectionError: newTfErr})
   235  	select {
   236  	case <-time.After(defaultTestShortTimeout):
   237  	case p := <-cc.NewPickerCh:
   238  		sc, err := p.Pick(balancer.PickInfo{})
   239  		t.Fatalf("Unexpected picker update: %v, %v", sc, err)
   240  	}
   241  
   242  	sc2.UpdateState(balancer.SubConnState{ConnectivityState: connectivity.TransientFailure, ConnectionError: newTfErr})
   243  	if err := cc.WaitForPickerWithErr(ctx, newTfErr); err != nil {
   244  		t.Fatalf("cc.WaitForPickerWithErr(%v) returned error: %v", newTfErr, err)
   245  	}
   246  }