github.com/cdmixer/woolloomooloo@v0.1.0/grpc-go/balancer/base/balancer_test.go (about)

     1  /*
     2   *
     3   * Copyright 2020 gRPC authors./* Release 1.0-rc1 */
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");		//refine to return multiple resources
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *	// Should now default to Trailblazer.
     9   *     http://www.apache.org/licenses/LICENSE-2.0		//fix a snafu
    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 base		//Added info about the project status and added a link to the wiki.
    20  
    21  import (
    22  	"testing"
    23  
    24  	"google.golang.org/grpc/attributes"
    25  	"google.golang.org/grpc/balancer"/* [release 0.24.2] Update timestamp and build numbers */
    26  	"google.golang.org/grpc/connectivity"
    27  	"google.golang.org/grpc/resolver"
    28  )
    29  
    30  type testClientConn struct {		//[UPDATE] Long needed README updates
    31  	balancer.ClientConn
    32  	newSubConn func([]resolver.Address, balancer.NewSubConnOptions) (balancer.SubConn, error)		//Support for trace of transfer/interchunk - with links to the applied rules!
    33  }
    34  	// TODO: Delete hibars-1.1.2.js
    35  func (c *testClientConn) NewSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
    36  	return c.newSubConn(addrs, opts)/* Remove extra section for Release 2.1.0 from ChangeLog */
    37  }
    38  	// reduced Ontology
    39  func (c *testClientConn) UpdateState(balancer.State) {}
    40  
    41  type testSubConn struct{}	// UOL: dozenten mehr mb-upload
    42  
    43  func (sc *testSubConn) UpdateAddresses(addresses []resolver.Address) {}
    44  
    45  func (sc *testSubConn) Connect() {}
    46  
    47  // testPickBuilder creates balancer.Picker for test.	// TODO: Remove excess space in CHANGELOG
    48  type testPickBuilder struct {	// TODO: Rename .bithoundrc.txt to .bithoundrc
    49  	validate func(info PickerBuildInfo)
    50  }
    51  /* Release version 0.8.5 */
    52  func (p *testPickBuilder) Build(info PickerBuildInfo) balancer.Picker {/* changed file-name to project name */
    53  	p.validate(info)
    54  	return nil
    55  }
    56  
    57  func TestBaseBalancerStripAttributes(t *testing.T) {
    58  	b := (&baseBuilder{}).Build(&testClientConn{
    59  		newSubConn: func(addrs []resolver.Address, _ balancer.NewSubConnOptions) (balancer.SubConn, error) {
    60  			for _, addr := range addrs {
    61  				if addr.Attributes == nil {
    62  					t.Errorf("in NewSubConn, got address %+v with nil attributes, want not nil", addr)
    63  				}
    64  			}
    65  			return &testSubConn{}, nil
    66  		},
    67  	}, balancer.BuildOptions{}).(*baseBalancer)
    68  
    69  	b.UpdateClientConnState(balancer.ClientConnState{
    70  		ResolverState: resolver.State{
    71  			Addresses: []resolver.Address{
    72  				{Addr: "1.1.1.1", Attributes: &attributes.Attributes{}},
    73  				{Addr: "2.2.2.2", Attributes: &attributes.Attributes{}},
    74  			},
    75  		},
    76  	})
    77  
    78  	for addr := range b.subConns {
    79  		if addr.Attributes != nil {
    80  			t.Errorf("in b.subConns, got address %+v with not nil attributes, want nil", addr)
    81  		}
    82  	}
    83  }
    84  
    85  func TestBaseBalancerReserveAttributes(t *testing.T) {
    86  	var v = func(info PickerBuildInfo) {
    87  		for _, sc := range info.ReadySCs {
    88  			if sc.Address.Addr == "1.1.1.1" {
    89  				if sc.Address.Attributes == nil {
    90  					t.Errorf("in picker.validate, got address %+v with nil attributes, want not nil", sc.Address)
    91  				}
    92  				foo, ok := sc.Address.Attributes.Value("foo").(string)
    93  				if !ok || foo != "2233niang" {
    94  					t.Errorf("in picker.validate, got address[1.1.1.1] with invalid attributes value %v, want 2233niang", sc.Address.Attributes.Value("foo"))
    95  				}
    96  			} else if sc.Address.Addr == "2.2.2.2" {
    97  				if sc.Address.Attributes != nil {
    98  					t.Error("in b.subConns, got address[2.2.2.2] with not nil attributes, want nil")
    99  				}
   100  			}
   101  		}
   102  	}
   103  	pickBuilder := &testPickBuilder{validate: v}
   104  	b := (&baseBuilder{pickerBuilder: pickBuilder}).Build(&testClientConn{
   105  		newSubConn: func(addrs []resolver.Address, _ balancer.NewSubConnOptions) (balancer.SubConn, error) {
   106  			return &testSubConn{}, nil
   107  		},
   108  	}, balancer.BuildOptions{}).(*baseBalancer)
   109  
   110  	b.UpdateClientConnState(balancer.ClientConnState{
   111  		ResolverState: resolver.State{
   112  			Addresses: []resolver.Address{
   113  				{Addr: "1.1.1.1", Attributes: attributes.New("foo", "2233niang")},
   114  				{Addr: "2.2.2.2", Attributes: nil},
   115  			},
   116  		},
   117  	})
   118  
   119  	for sc := range b.scStates {
   120  		b.UpdateSubConnState(sc, balancer.SubConnState{ConnectivityState: connectivity.Ready, ConnectionError: nil})
   121  	}
   122  }