dubbo.apache.org/dubbo-go/v3@v3.1.1/filter/active/filter_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 active
    19  
    20  import (
    21  	"context"
    22  	"errors"
    23  	"strconv"
    24  	"testing"
    25  )
    26  
    27  import (
    28  	"github.com/golang/mock/gomock"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  import (
    34  	"dubbo.apache.org/dubbo-go/v3/common"
    35  	"dubbo.apache.org/dubbo-go/v3/protocol"
    36  	"dubbo.apache.org/dubbo-go/v3/protocol/invocation"
    37  	"dubbo.apache.org/dubbo-go/v3/protocol/mock"
    38  )
    39  
    40  func TestFilterInvoke(t *testing.T) {
    41  	invoc := invocation.NewRPCInvocation("test", []interface{}{"OK"}, make(map[string]interface{}))
    42  	url, _ := common.NewURL("dubbo://192.168.10.10:20000/com.ikurento.user.UserProvider")
    43  	filter := activeFilter{}
    44  	ctrl := gomock.NewController(t)
    45  	defer ctrl.Finish()
    46  	invoker := mock.NewMockInvoker(ctrl)
    47  	invoker.EXPECT().Invoke(gomock.Any(), gomock.Any()).Return(nil)
    48  	invoker.EXPECT().GetURL().Return(url).Times(1)
    49  	filter.Invoke(context.Background(), invoker, invoc)
    50  	assert.True(t, invoc.GetAttachmentWithDefaultValue(dubboInvokeStartTime, "") != "")
    51  }
    52  
    53  func TestFilterOnResponse(t *testing.T) {
    54  	c := protocol.CurrentTimeMillis()
    55  	elapsed := 100
    56  	invoc := invocation.NewRPCInvocation("test", []interface{}{"OK"}, map[string]interface{}{
    57  		dubboInvokeStartTime: strconv.FormatInt(c-int64(elapsed), 10),
    58  	})
    59  	url, _ := common.NewURL("dubbo://192.168.10.10:20000/com.ikurento.user.UserProvider")
    60  	filter := activeFilter{}
    61  	ctrl := gomock.NewController(t)
    62  	defer ctrl.Finish()
    63  	invoker := mock.NewMockInvoker(ctrl)
    64  	invoker.EXPECT().GetURL().Return(url).Times(1)
    65  	result := &protocol.RPCResult{
    66  		Err: errors.New("test"),
    67  	}
    68  	filter.OnResponse(context.TODO(), result, invoker, invoc)
    69  	methodStatus := protocol.GetMethodStatus(url, "test")
    70  	urlStatus := protocol.GetURLStatus(url)
    71  
    72  	assert.Equal(t, int32(1), methodStatus.GetTotal())
    73  	assert.Equal(t, int32(1), urlStatus.GetTotal())
    74  	assert.Equal(t, int32(1), methodStatus.GetFailed())
    75  	assert.Equal(t, int32(1), urlStatus.GetFailed())
    76  	assert.Equal(t, int32(1), methodStatus.GetSuccessiveRequestFailureCount())
    77  	assert.Equal(t, int32(1), urlStatus.GetSuccessiveRequestFailureCount())
    78  	assert.True(t, methodStatus.GetFailedElapsed() >= int64(elapsed))
    79  	assert.True(t, urlStatus.GetFailedElapsed() >= int64(elapsed))
    80  	assert.True(t, urlStatus.GetLastRequestFailedTimestamp() != int64(0))
    81  	assert.True(t, methodStatus.GetLastRequestFailedTimestamp() != int64(0))
    82  }