MCPcopy
hub / github.com/grpc/grpc-go / TestGRPCLB_Weighted

Method TestGRPCLB_Weighted

balancer/grpclb/grpclb_test.go:474–542  ·  view source on GitHub ↗

TestGRPCLB_Weighted tests weighted roundrobin. The remote balancer is configured to send a response with duplicate backend addresses (to simulate weights) to the grpclb client. The test verifies that RPCs are weighted roundrobin-ed across these backends.

(t *testing.T)

Source from the content-addressed store, hash-verified

472// weights) to the grpclb client. The test verifies that RPCs are weighted
473// roundrobin-ed across these backends.
474func (s) TestGRPCLB_Weighted(t *testing.T) {
475 tss, cleanup, err := startBackendsAndRemoteLoadBalancer(t, 2, "", nil)
476 if err != nil {
477 t.Fatalf("failed to create new load balancer: %v", err)
478 }
479 defer cleanup()
480
481 beServers := []*lbpb.Server{{
482 IpAddress: tss.beIPs[0].AsSlice(),
483 Port: int32(tss.bePorts[0]),
484 LoadBalanceToken: lbToken,
485 }, {
486 IpAddress: tss.beIPs[1].AsSlice(),
487 Port: int32(tss.bePorts[1]),
488 LoadBalanceToken: lbToken,
489 }}
490
491 // Configure the manual resolver with an initial state containing a service
492 // config with grpclb as the load balancing policy and the remote balancer
493 // address specified via attributes.
494 r := manual.NewBuilderWithScheme("whatever")
495 s := &grpclbstate.State{
496 BalancerAddresses: []resolver.Address{
497 {
498 Addr: tss.lbAddr,
499 ServerName: lbServerName,
500 },
501 },
502 }
503 rs := grpclbstate.Set(resolver.State{ServiceConfig: internal.ParseServiceConfig.(func(string) *serviceconfig.ParseResult)(grpclbConfig)}, s)
504 r.InitialState(rs)
505
506 // Connect to test backends.
507 dopts := []grpc.DialOption{
508 grpc.WithResolvers(r),
509 grpc.WithTransportCredentials(&serverNameCheckCreds{}),
510 grpc.WithContextDialer(fakeNameDialer),
511 }
512 cc, err := grpc.NewClient(r.Scheme()+":///"+beServerName, dopts...)
513 if err != nil {
514 t.Fatalf("Failed to create a client for the backend %v", err)
515 }
516 defer cc.Close()
517
518 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
519 defer cancel()
520 // Sequence represents the sequence of backends to be returned from the
521 // remote load balancer.
522 sequences := [][]int{
523 {0, 0, 1, 0, 1},
524 {0, 0, 0, 1, 1},
525 }
526 for _, seq := range sequences {
527 // Push the configured sequence of backend to the remote balancer, and
528 // compute the expected addresses to which RPCs should be routed.
529 var backends []*lbpb.Server
530 var wantAddrs []resolver.Address
531 for _, s := range seq {

Callers

nothing calls this directly

Calls 15

NewBuilderWithSchemeFunction · 0.92
WithResolversFunction · 0.92
WithTransportCredentialsFunction · 0.92
WithContextDialerFunction · 0.92
NewClientFunction · 0.92
InitialStateMethod · 0.80
FatalfMethod · 0.65
SetMethod · 0.65
SchemeMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected