dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/rpc_status_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  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  package protocol
    19  
    20  import (
    21  	"strconv"
    22  	"testing"
    23  )
    24  
    25  import (
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  import (
    30  	"dubbo.apache.org/dubbo-go/v3/common"
    31  )
    32  
    33  const (
    34  	mockCommonDubboUrl = "dubbo://192.168.10.10:20000/com.ikurento.user.UserProvider"
    35  )
    36  
    37  func TestBeginCount(t *testing.T) {
    38  	defer CleanAllStatus()
    39  
    40  	url, _ := common.NewURL(mockCommonDubboUrl)
    41  	BeginCount(url, "test")
    42  	urlStatus := GetURLStatus(url)
    43  	methodStatus := GetMethodStatus(url, "test")
    44  	methodStatus1 := GetMethodStatus(url, "test1")
    45  	assert.Equal(t, int32(1), methodStatus.active)
    46  	assert.Equal(t, int32(1), urlStatus.active)
    47  	assert.Equal(t, int32(0), methodStatus1.active)
    48  }
    49  
    50  func TestEndCount(t *testing.T) {
    51  	defer CleanAllStatus()
    52  
    53  	url, _ := common.NewURL(mockCommonDubboUrl)
    54  	EndCount(url, "test", 100, true)
    55  	urlStatus := GetURLStatus(url)
    56  	methodStatus := GetMethodStatus(url, "test")
    57  	assert.Equal(t, int32(-1), methodStatus.active)
    58  	assert.Equal(t, int32(-1), urlStatus.active)
    59  	assert.Equal(t, int32(1), methodStatus.total)
    60  	assert.Equal(t, int32(1), urlStatus.total)
    61  }
    62  
    63  func TestGetMethodStatus(t *testing.T) {
    64  	defer CleanAllStatus()
    65  
    66  	url, _ := common.NewURL(mockCommonDubboUrl)
    67  	status := GetMethodStatus(url, "test")
    68  	assert.NotNil(t, status)
    69  	assert.Equal(t, int32(0), status.total)
    70  }
    71  
    72  func TestGetUrlStatus(t *testing.T) {
    73  	defer CleanAllStatus()
    74  
    75  	url, _ := common.NewURL(mockCommonDubboUrl)
    76  	status := GetURLStatus(url)
    77  	assert.NotNil(t, status)
    78  	assert.Equal(t, int32(0), status.total)
    79  }
    80  
    81  func TestBeginCount0(t *testing.T) {
    82  	defer CleanAllStatus()
    83  
    84  	url, _ := common.NewURL(mockCommonDubboUrl)
    85  	status := GetURLStatus(url)
    86  	beginCount0(status)
    87  	assert.Equal(t, int32(1), status.active)
    88  }
    89  
    90  func TestAll(t *testing.T) {
    91  	defer CleanAllStatus()
    92  
    93  	url, _ := common.NewURL(mockCommonDubboUrl)
    94  	request(url, "test", 100, false, true)
    95  	urlStatus := GetURLStatus(url)
    96  	methodStatus := GetMethodStatus(url, "test")
    97  	assert.Equal(t, int32(1), methodStatus.total)
    98  	assert.Equal(t, int32(1), urlStatus.total)
    99  	assert.Equal(t, int32(0), methodStatus.active)
   100  	assert.Equal(t, int32(0), urlStatus.active)
   101  	assert.Equal(t, int32(0), methodStatus.failed)
   102  	assert.Equal(t, int32(0), urlStatus.failed)
   103  	assert.Equal(t, int32(0), methodStatus.successiveRequestFailureCount)
   104  	assert.Equal(t, int32(0), urlStatus.successiveRequestFailureCount)
   105  	assert.Equal(t, int64(100), methodStatus.totalElapsed)
   106  	assert.Equal(t, int64(100), urlStatus.totalElapsed)
   107  	request(url, "test", 100, false, false)
   108  	request(url, "test", 100, false, false)
   109  	request(url, "test", 100, false, false)
   110  	request(url, "test", 100, false, false)
   111  	request(url, "test", 100, false, false)
   112  	assert.Equal(t, int32(6), methodStatus.total)
   113  	assert.Equal(t, int32(6), urlStatus.total)
   114  	assert.Equal(t, int32(5), methodStatus.failed)
   115  	assert.Equal(t, int32(5), urlStatus.failed)
   116  	assert.Equal(t, int32(5), methodStatus.successiveRequestFailureCount)
   117  	assert.Equal(t, int32(5), urlStatus.successiveRequestFailureCount)
   118  	assert.Equal(t, int64(600), methodStatus.totalElapsed)
   119  	assert.Equal(t, int64(600), urlStatus.totalElapsed)
   120  	assert.Equal(t, int64(500), methodStatus.failedElapsed)
   121  	assert.Equal(t, int64(500), urlStatus.failedElapsed)
   122  
   123  	request(url, "test", 100, false, true)
   124  	assert.Equal(t, int32(0), methodStatus.successiveRequestFailureCount)
   125  	assert.Equal(t, int32(0), urlStatus.successiveRequestFailureCount)
   126  
   127  	request(url, "test", 200, false, false)
   128  	request(url, "test", 200, false, false)
   129  	assert.Equal(t, int32(2), methodStatus.successiveRequestFailureCount)
   130  	assert.Equal(t, int32(2), urlStatus.successiveRequestFailureCount)
   131  	assert.Equal(t, int64(200), methodStatus.maxElapsed)
   132  	assert.Equal(t, int64(200), urlStatus.maxElapsed)
   133  
   134  	request(url, "test1", 200, false, false)
   135  	request(url, "test1", 200, false, false)
   136  	request(url, "test1", 200, false, false)
   137  	assert.Equal(t, int32(5), urlStatus.successiveRequestFailureCount)
   138  	methodStatus1 := GetMethodStatus(url, "test1")
   139  	assert.Equal(t, int32(2), methodStatus.successiveRequestFailureCount)
   140  	assert.Equal(t, int32(3), methodStatus1.successiveRequestFailureCount)
   141  }
   142  
   143  func request(url *common.URL, method string, elapsed int64, active, succeeded bool) {
   144  	BeginCount(url, method)
   145  	if !active {
   146  		EndCount(url, method, elapsed, succeeded)
   147  	}
   148  }
   149  
   150  func TestCurrentTimeMillis(t *testing.T) {
   151  	defer CleanAllStatus()
   152  	c := CurrentTimeMillis()
   153  	assert.NotNil(t, c)
   154  	str := strconv.FormatInt(c, 10)
   155  	i, _ := strconv.ParseInt(str, 10, 64)
   156  	assert.Equal(t, c, i)
   157  }