github.com/cloudwego/hertz@v0.9.3/pkg/app/client/discovery/discovery_test.go (about) 1 /* 2 * Copyright 2022 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package discovery 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/cloudwego/hertz/pkg/app/server/registry" 24 "github.com/cloudwego/hertz/pkg/common/test/assert" 25 ) 26 27 func TestInstance(t *testing.T) { 28 network := "192.168.1.1" 29 address := "/hello" 30 weight := 1 31 instance := NewInstance(network, address, weight, nil) 32 33 assert.DeepEqual(t, network, instance.Address().Network()) 34 assert.DeepEqual(t, address, instance.Address().String()) 35 assert.DeepEqual(t, weight, instance.Weight()) 36 val, ok := instance.Tag("name") 37 assert.DeepEqual(t, "", val) 38 assert.False(t, ok) 39 40 instance2 := NewInstance("", "", 0, nil) 41 assert.DeepEqual(t, registry.DefaultWeight, instance2.Weight()) 42 } 43 44 func TestSynthesizedResolver(t *testing.T) { 45 targetFunc := func(ctx context.Context, target *TargetInfo) string { 46 return "hello" 47 } 48 resolveFunc := func(ctx context.Context, key string) (Result, error) { 49 return Result{CacheKey: "name"}, nil 50 } 51 nameFunc := func() string { 52 return "raymonder" 53 } 54 resolver := SynthesizedResolver{ 55 TargetFunc: targetFunc, 56 ResolveFunc: resolveFunc, 57 NameFunc: nameFunc, 58 } 59 60 assert.DeepEqual(t, "hello", resolver.Target(context.Background(), &TargetInfo{})) 61 res, err := resolver.Resolve(context.Background(), "") 62 assert.DeepEqual(t, "name", res.CacheKey) 63 assert.Nil(t, err) 64 assert.DeepEqual(t, "raymonder", resolver.Name()) 65 66 resolver2 := SynthesizedResolver{ 67 TargetFunc: nil, 68 ResolveFunc: nil, 69 NameFunc: nil, 70 } 71 assert.DeepEqual(t, "", resolver2.Target(context.Background(), &TargetInfo{})) 72 assert.DeepEqual(t, "", resolver2.Name()) 73 }