trpc.group/trpc-go/trpc-go@v1.0.3/naming/selector/options_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package selector
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  
    20  	"trpc.group/trpc-go/trpc-go/naming/circuitbreaker"
    21  	"trpc.group/trpc-go/trpc-go/naming/discovery"
    22  	"trpc.group/trpc-go/trpc-go/naming/loadbalance"
    23  	"trpc.group/trpc-go/trpc-go/naming/servicerouter"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestOptions(t *testing.T) {
    29  	opts := &Options{}
    30  	ctx := context.Background()
    31  	WithContext(ctx)(opts)
    32  	WithKey("key")(opts)
    33  	WithReplicas(100)(opts)
    34  	WithSourceSetName("set")(opts)
    35  	WithDestinationSetName("dstSet")(opts)
    36  	d := &discovery.IPDiscovery{}
    37  	WithDiscovery(d)(opts)
    38  	r := &servicerouter.NoopServiceRouter{}
    39  	WithServiceRouter(r)(opts)
    40  	b := loadbalance.NewRandom()
    41  	WithLoadBalancer(b)(opts)
    42  	cb := &circuitbreaker.NoopCircuitBreaker{}
    43  	WithCircuitBreaker(cb)(opts)
    44  	WithDisableServiceRouter()(opts)
    45  	WithDestinationEnvName("dst_env")(opts)
    46  	WithNamespace("test_namespace")(opts)
    47  	WithSourceNamespace("src_namespace")(opts)
    48  	WithEnvKey("env_key")(opts)
    49  	WithSourceServiceName("src_svcname")(opts)
    50  	WithSourceEnvName("src_env")(opts)
    51  	WithSourceMetadata("srcMeta", "value")(opts)
    52  	WithDestinationMetadata("dstMeta", "value")(opts)
    53  	WithEnvTransfer("env_transfer")(opts)
    54  	WithLoadBalanceType("hash")(opts)
    55  
    56  	assert.Equal(t, opts.Ctx, ctx)
    57  	assert.Equal(t, opts.SourceSetName, "set")
    58  	assert.Equal(t, opts.Key, "key")
    59  	assert.Equal(t, opts.Replicas, 100)
    60  	assert.Equal(t, opts.CircuitBreaker, cb)
    61  	assert.Equal(t, opts.LoadBalancer, b)
    62  	assert.Equal(t, opts.Discovery, d)
    63  	assert.Equal(t, opts.ServiceRouter, r)
    64  	assert.True(t, opts.DisableServiceRouter)
    65  	assert.Equal(t, opts.DestinationEnvName, "dst_env")
    66  	assert.Equal(t, opts.DestinationSetName, "dstSet")
    67  	assert.Equal(t, opts.Namespace, "test_namespace")
    68  	assert.Equal(t, opts.SourceNamespace, "src_namespace")
    69  	assert.Equal(t, opts.SourceServiceName, "src_svcname")
    70  	assert.Equal(t, opts.EnvKey, "env_key")
    71  	assert.Equal(t, opts.SourceEnvName, "src_env")
    72  	assert.Equal(t, opts.SourceMetadata["srcMeta"], "value")
    73  	assert.Equal(t, opts.DestinationMetadata["dstMeta"], "value")
    74  	assert.Equal(t, opts.EnvTransfer, "env_transfer")
    75  	assert.Len(t, opts.LoadBalanceOptions, 5)
    76  }