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

Method TestPickFirst_ParseConfig_Success

balancer/pickfirst/pickfirst_ext_test.go:633–691  ·  view source on GitHub ↗

Test config parsing with the env var turned on and off for various scenarios.

(t *testing.T)

Source from the content-addressed store, hash-verified

631
632// Test config parsing with the env var turned on and off for various scenarios.
633func (s) TestPickFirst_ParseConfig_Success(t *testing.T) {
634 testutils.SetEnvConfig(t, &envconfig.PickFirstWeightedShuffling, false)
635
636 // Install a shuffler that always reverses two entries.
637 origShuf := pfinternal.RandShuffle
638 defer func() { pfinternal.RandShuffle = origShuf }()
639 pfinternal.RandShuffle = func(n int, f func(int, int)) {
640 if n != 2 {
641 t.Errorf("Shuffle called with n=%v; want 2", n)
642 return
643 }
644 f(0, 1) // reverse the two addresses
645 }
646
647 tests := []struct {
648 name string
649 serviceConfig string
650 wantFirstAddr bool
651 }{
652 {
653 name: "empty pickfirst config",
654 serviceConfig: `{"loadBalancingConfig": [{"pick_first":{}}]}`,
655 wantFirstAddr: true,
656 },
657 {
658 name: "empty good pickfirst config",
659 serviceConfig: `{"loadBalancingConfig": [{"pick_first":{ "shuffleAddressList": true }}]}`,
660 wantFirstAddr: false,
661 },
662 }
663
664 for _, test := range tests {
665 t.Run(test.name, func(t *testing.T) {
666 // Set up our backends.
667 cc, r, backends := setupPickFirst(t, 2)
668 addrs := stubBackendsToResolverAddrs(backends)
669
670 r.UpdateState(resolver.State{
671 ServiceConfig: parseServiceConfig(t, r, test.serviceConfig),
672 Addresses: addrs,
673 })
674
675 // Some tests expect address shuffling to happen, and indicate that
676 // by setting wantFirstAddr to false (since our shuffling function
677 // defined at the top of this test, simply reverses the list of
678 // addresses provided to it).
679 wantAddr := addrs[0]
680 if !test.wantFirstAddr {
681 wantAddr = addrs[1]
682 }
683
684 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
685 defer cancel()
686 if err := pickfirst.CheckRPCsToBackend(ctx, cc, wantAddr); err != nil {
687 t.Fatal(err)
688 }
689 })
690 }

Callers

nothing calls this directly

Calls 8

SetEnvConfigFunction · 0.92
CheckRPCsToBackendFunction · 0.92
setupPickFirstFunction · 0.85
parseServiceConfigFunction · 0.70
ErrorfMethod · 0.65
UpdateStateMethod · 0.65
FatalMethod · 0.65

Tested by

no test coverage detected