github.com/cloudwego/kitex@v0.9.0/pkg/retry/util_test.go (about)

     1  /*
     2   * Copyright 2023 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 retry
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/bytedance/gopkg/cloud/metainfo"
    24  
    25  	"github.com/cloudwego/kitex/internal/test"
    26  	"github.com/cloudwego/kitex/pkg/rpcinfo"
    27  )
    28  
    29  func mockRPCInfo(retryTag string) rpcinfo.RPCInfo {
    30  	var tags map[string]string
    31  	if retryTag != "" {
    32  		tags = map[string]string{
    33  			rpcinfo.RetryTag: retryTag,
    34  		}
    35  	}
    36  	to := rpcinfo.NewEndpointInfo("service", "method", nil, tags)
    37  	return rpcinfo.NewRPCInfo(nil, to, nil, nil, nil)
    38  }
    39  
    40  func mockContext(retryTag string) context.Context {
    41  	return rpcinfo.NewCtxWithRPCInfo(context.TODO(), mockRPCInfo(retryTag))
    42  }
    43  
    44  func TestIsLocalRetryRequest(t *testing.T) {
    45  	t.Run("no-retry-tag", func(t *testing.T) {
    46  		test.Assertf(t, !IsLocalRetryRequest(mockContext("")), "no-retry-tag")
    47  	})
    48  	t.Run("retry-tag=0", func(t *testing.T) {
    49  		test.Assertf(t, !IsLocalRetryRequest(mockContext("0")), "retry-tag=0")
    50  	})
    51  	t.Run("retry-tag=1", func(t *testing.T) {
    52  		test.Assertf(t, IsLocalRetryRequest(mockContext("1")), "retry-tag=1")
    53  	})
    54  }
    55  
    56  func TestIsRemoteRetryRequest(t *testing.T) {
    57  	t.Run("no-retry", func(t *testing.T) {
    58  		test.Assertf(t, !IsRemoteRetryRequest(context.Background()), "should be not retry")
    59  	})
    60  	t.Run("retry", func(t *testing.T) {
    61  		ctx := metainfo.WithPersistentValue(context.Background(), TransitKey, "2")
    62  		test.Assertf(t, IsRemoteRetryRequest(ctx), "should be retry")
    63  	})
    64  }