google.golang.org/grpc@v1.62.1/resolver/manual/manual_test.go (about) 1 /* 2 * 3 * Copyright 2023 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 manual_test 20 21 import ( 22 "errors" 23 "testing" 24 25 "google.golang.org/grpc" 26 "google.golang.org/grpc/credentials/insecure" 27 "google.golang.org/grpc/resolver" 28 "google.golang.org/grpc/resolver/manual" 29 ) 30 31 func TestResolver(t *testing.T) { 32 r := manual.NewBuilderWithScheme("whatever") 33 r.InitialState(resolver.State{ 34 Addresses: []resolver.Address{ 35 {Addr: "address"}, 36 }, 37 }) 38 39 t.Run("update_state_panics", func(t *testing.T) { 40 defer func() { 41 want := "cannot update state as grpc.Dial with resolver has not been called" 42 if r := recover(); r != want { 43 t.Errorf("expected panic %q, got %q", want, r) 44 } 45 }() 46 r.UpdateState(resolver.State{Addresses: []resolver.Address{ 47 {Addr: "address"}, 48 {Addr: "anotheraddress"}, 49 }}) 50 }) 51 t.Run("report_error_panics", func(t *testing.T) { 52 defer func() { 53 want := "cannot report error as grpc.Dial with resolver has not been called" 54 if r := recover(); r != want { 55 t.Errorf("expected panic %q, got %q", want, r) 56 } 57 }() 58 r.ReportError(errors.New("example")) 59 }) 60 61 t.Run("happy_path", func(t *testing.T) { 62 _, err := grpc.Dial("whatever://localhost", 63 grpc.WithTransportCredentials(insecure.NewCredentials()), 64 grpc.WithResolvers(r)) 65 if err != nil { 66 t.Errorf("dial setup error: %v", err) 67 } 68 r.UpdateState(resolver.State{Addresses: []resolver.Address{ 69 {Addr: "ok"}, 70 }}) 71 r.ReportError(errors.New("example")) 72 }) 73 }