github.com/kaydxh/golang@v0.0.131/pkg/resolver/resolver_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package resolver_test
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  	"testing"
    28  	"time"
    29  
    30  	resolver_ "github.com/kaydxh/golang/pkg/resolver"
    31  	viper_ "github.com/kaydxh/golang/pkg/viper"
    32  )
    33  
    34  /*
    35  func TestNewResolverService(t *testing.T) {
    36  	cfgFile := "./resolver.yaml"
    37  	config := resolver_.NewConfig(resolver_.WithViper(viper_.GetViper(cfgFile, "resolver")))
    38  	s, err := config.Complete().New(context.Background())
    39  	if err != nil {
    40  		t.Errorf("failed to new config err: %v", err)
    41  	}
    42  	s.Run(context.Background())
    43  	//	net.DefaultResolver
    44  
    45  	type args struct {
    46  		consistkey       string
    47  		resolverInterval time.Duration
    48  		services         []resolver_.ResolverQuery
    49  	}
    50  	tests := []struct {
    51  		name string
    52  		args args
    53  		want *resolver_.ResolverService
    54  	}{
    55  		// TODO: Add test cases.
    56  		{
    57  			name: "www.baidu.com",
    58  			args: args{
    59  				consistkey:       "1",
    60  				resolverInterval: 0,
    61  				services: []resolver_.ResolverQuery{
    62  					{
    63  						Domain: "www.baidu.com",
    64  						Opts: resolver_.ResolverOptions{
    65  							ResolverType:    resolver_.Resolver_resolver_type_dns,
    66  							LoadBalanceMode: resolver_.Resolver_load_balance_mode_consist,
    67  						},
    68  					},
    69  				},
    70  			},
    71  		},
    72  	}
    73  	for _, tt := range tests {
    74  		t.Run(tt.name, func(t *testing.T) {
    75  			err = s.AddServices(tt.args.services...)
    76  			for i := 0; i < 100; i++ {
    77  				consistkey := fmt.Sprintf("consist-key-%d", i)
    78  				node, has := s.PickNode(tt.name, consistkey)
    79  				t.Logf("pick node: %v, has: %v, consistkey: %v", node, has, consistkey)
    80  			}
    81  		})
    82  	}
    83  }
    84  */
    85  
    86  func TestNewResolverService2(t *testing.T) {
    87  	cfgFile := "./resolver.yaml"
    88  	config := resolver_.NewConfig(resolver_.WithViper(viper_.GetViper(cfgFile, "resolver")))
    89  	s, err := config.Complete().New(context.Background())
    90  	if err != nil {
    91  		t.Errorf("failed to new config err: %v", err)
    92  	}
    93  	s.Run(context.Background())
    94  
    95  	type args struct {
    96  		consistkey       string
    97  		resolverInterval time.Duration
    98  		services         []resolver_.ResolverQuery
    99  	}
   100  
   101  	tests := []struct {
   102  		serviceName      string
   103  		nodeGroup        string
   104  		nodeUnit         string
   105  		consistkey       string
   106  		loadBalanceMode  resolver_.Resolver_LoadBalanceMode
   107  		resolverType     resolver_.Resolver_ResolverType
   108  		resolverInterval time.Duration
   109  	}{
   110  		// TODO: Add test cases.
   111  		{
   112  			serviceName:      "test-cube-algo-backend",
   113  			nodeGroup:        "edge-global-group",
   114  			nodeUnit:         "edge-node-zone",
   115  			consistkey:       "1",
   116  			resolverInterval: 0,
   117  			resolverType:     resolver_.Resolver_resolver_type_k8s,
   118  		},
   119  	}
   120  	for _, tt := range tests {
   121  		t.Run(tt.serviceName, func(t *testing.T) {
   122  			rq, err := resolver_.NewResolverQuery(
   123  				tt.serviceName,
   124  				resolver_.WithLoadBalanceMode(tt.loadBalanceMode),
   125  				resolver_.WithResolverType(tt.resolverType),
   126  				resolver_.WithNodeGroup(tt.nodeGroup),
   127  				resolver_.WithNodeUnit(tt.nodeUnit),
   128  			)
   129  			if err != nil {
   130  				t.Fatalf("new resolver query err: %v", err)
   131  			}
   132  			s.AddServices(rq)
   133  			time.Sleep(5 * time.Second)
   134  			for i := 0; i < 100; i++ {
   135  				consistkey := fmt.Sprintf("consist-key-%d", i)
   136  				node, has := s.PickNode(tt.serviceName, consistkey)
   137  				t.Logf("pick node: %v, has: %v, consistkey: %v", node, has, consistkey)
   138  			}
   139  		})
   140  	}
   141  }