dubbo.apache.org/dubbo-go/v3@v3.1.1/metrics/util/aggregate/counter_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 aggregate
    19  
    20  import (
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  func TestTimeWindowCounterCount(t1 *testing.T) {
    26  	tests := []struct {
    27  		name       string
    28  		queryTimes int
    29  		want       float64
    30  	}{
    31  		{
    32  			name:       "Query Times: 0",
    33  			queryTimes: 0,
    34  			want:       0,
    35  		},
    36  		{
    37  			name:       "Query Times: 3",
    38  			queryTimes: 3,
    39  			want:       3,
    40  		},
    41  		{
    42  			name:       "Query Times: 10",
    43  			queryTimes: 10,
    44  			want:       10,
    45  		},
    46  	}
    47  	for _, tt := range tests {
    48  		t1.Run(tt.name, func(t1 *testing.T) {
    49  			t := NewTimeWindowCounter(10, 1)
    50  			for i := 0; i < tt.queryTimes; i++ {
    51  				if i%3 == 0 {
    52  					time.Sleep(time.Millisecond * 100)
    53  				}
    54  				t.Inc()
    55  			}
    56  			if got := t.Count(); got != tt.want {
    57  				t1.Errorf("Count() = %v, want %v", got, tt.want)
    58  			}
    59  		})
    60  	}
    61  }
    62  
    63  func TestTimeWindowCounterLivedSeconds(t1 *testing.T) {
    64  	tests := []struct {
    65  		name       string
    66  		queryTimes int
    67  		want       int64
    68  	}{
    69  		{
    70  			name:       "Query Times: 0",
    71  			queryTimes: 0,
    72  			want:       0,
    73  		},
    74  		{
    75  			name:       "Query Times: 3",
    76  			queryTimes: 3,
    77  			want:       1,
    78  		},
    79  		{
    80  			name:       "Query Times: 9",
    81  			queryTimes: 9,
    82  			want:       3,
    83  		},
    84  	}
    85  	for _, tt := range tests {
    86  		t1.Run(tt.name, func(t1 *testing.T) {
    87  			t := NewTimeWindowCounter(10, 10)
    88  			for i := 0; i < tt.queryTimes; i++ {
    89  				if i%3 == 0 {
    90  					time.Sleep(time.Second * 1)
    91  				}
    92  				t.Inc()
    93  			}
    94  			if got := t.LivedSeconds(); got != tt.want {
    95  				t1.Errorf("Count() = %v, want %v", got, tt.want)
    96  			}
    97  		})
    98  	}
    99  }