github.com/polarismesh/polaris@v1.17.8/plugin/ratelimit/lrurate/lrurate_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package lrurate
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/polarismesh/polaris/plugin"
    24  )
    25  
    26  // getEntry 获取初始化的entry
    27  func getEntry() *plugin.ConfigEntry {
    28  	entry := plugin.ConfigEntry{
    29  		Option: make(map[string]interface{}),
    30  	}
    31  	entry.Option["rateLimitIPLruSize"] = 10
    32  	entry.Option["rateLimitIPRate"] = 10
    33  	entry.Option["rateLimitIPBurst"] = 10
    34  	entry.Option["rateLimitServiceLruSize"] = 10
    35  	entry.Option["rateLimitServiceRate"] = 10
    36  	entry.Option["rateLimitServiceBurst"] = 10
    37  
    38  	return &entry
    39  }
    40  
    41  // getUninitializedEntry 获取未初始化的entry
    42  func getUninitializedEntry() *plugin.ConfigEntry {
    43  	entry := plugin.ConfigEntry{
    44  		Option: make(map[string]interface{}),
    45  	}
    46  
    47  	return &entry
    48  }
    49  
    50  // TestInvalidConfig 测试错误配置
    51  func TestInvalidConfig(t *testing.T) {
    52  	entry := getUninitializedEntry()
    53  	s := &LRURate{}
    54  
    55  	t.Run("InvalidIPLruSize", func(t *testing.T) {
    56  		if err := s.Initialize(entry); err == nil {
    57  			t.Errorf("failed, shouldn't Initialize")
    58  		}
    59  
    60  		entry.Option["rateLimitIPLruSize"] = 0
    61  		if err := s.Initialize(entry); err == nil {
    62  			t.Errorf("failed, shouldn't Initialize")
    63  		}
    64  	})
    65  
    66  	t.Run("InvalidIPLruRate", func(t *testing.T) {
    67  		entry.Option["rateLimitIPLruSize"] = 10
    68  		if err := s.Initialize(entry); err == nil {
    69  			t.Errorf("failed, shouldn't Initialize")
    70  		}
    71  
    72  		entry.Option["rateLimitIPRate"] = 0
    73  		if err := s.Initialize(entry); err == nil {
    74  			t.Errorf("failed, shouldn't Initialize")
    75  		}
    76  	})
    77  
    78  	t.Run("InvalidIPLruBurst", func(t *testing.T) {
    79  		entry.Option["rateLimitIPRate"] = 10
    80  		if err := s.Initialize(entry); err == nil {
    81  			t.Errorf("failed, shouldn't Initialize")
    82  		}
    83  
    84  		entry.Option["rateLimitIPBurst"] = 0
    85  		if err := s.Initialize(entry); err == nil {
    86  			t.Errorf("failed, shouldn't Initialize")
    87  		}
    88  	})
    89  
    90  	t.Run("InvalidServiceLruSize", func(t *testing.T) {
    91  		entry.Option["rateLimitIPBurst"] = 10
    92  		if err := s.Initialize(entry); err == nil {
    93  			t.Errorf("failed, shouldn't Initialize")
    94  		}
    95  
    96  		entry.Option["rateLimitServiceLruSize"] = 0
    97  		if err := s.Initialize(entry); err == nil {
    98  			t.Errorf("failed, shouldn't Initialize")
    99  		}
   100  	})
   101  
   102  	t.Run("InvalidServiceLruRate", func(t *testing.T) {
   103  		entry.Option["rateLimitServiceLruSize"] = 10
   104  		if err := s.Initialize(entry); err == nil {
   105  			t.Errorf("failed, shouldn't Initialize")
   106  		}
   107  
   108  		entry.Option["rateLimitServiceRate"] = 0
   109  		if err := s.Initialize(entry); err == nil {
   110  			t.Errorf("failed, shouldn't Initialize")
   111  		}
   112  	})
   113  
   114  	t.Run("InvalidServiceLruBurst", func(t *testing.T) {
   115  		entry.Option["rateLimitServiceRate"] = 10
   116  		if err := s.Initialize(entry); err == nil {
   117  			t.Errorf("failed, shouldn't Initialize")
   118  		}
   119  
   120  		entry.Option["rateLimitServiceBurst"] = 0
   121  		if err := s.Initialize(entry); err == nil {
   122  			t.Errorf("failed, shouldn't Initialize")
   123  		}
   124  	})
   125  }
   126  
   127  // TestValidConfig 测试正确配置
   128  func TestValidConfig(t *testing.T) {
   129  	t.Run("ValidConfig", func(t *testing.T) {
   130  		entry := getEntry()
   131  		s := &LRURate{}
   132  		if err := s.Initialize(entry); err != nil {
   133  			t.Errorf("failed: %s", err)
   134  		} else {
   135  			t.Logf("pass")
   136  		}
   137  	})
   138  }
   139  
   140  // TestCommon 测试一般函数
   141  func TestCommon(t *testing.T) {
   142  	entry := getEntry()
   143  	s := &LRURate{}
   144  	if err := s.Initialize(entry); err != nil {
   145  		t.Fatalf("failed: %s", err)
   146  	} else {
   147  		t.Logf("pass")
   148  	}
   149  
   150  	t.Run("Name", func(t *testing.T) {
   151  		if s.Name() != "lrurate" {
   152  			t.Errorf("failed, invalid plgin name: %s", s.Name())
   153  		} else {
   154  			t.Logf("pass")
   155  		}
   156  	})
   157  
   158  	t.Run("Destroy", func(t *testing.T) {
   159  		if s.Destroy() != nil {
   160  			t.Errorf("failed, bad Destroy")
   161  		} else {
   162  			t.Logf("pass")
   163  		}
   164  	})
   165  }
   166  
   167  // TestRateLimit 测试限流功能
   168  func TestRateLimit(t *testing.T) {
   169  	ipLruSize := 10
   170  	ipRate := 100
   171  	ipBurst := 200
   172  
   173  	serviceLruSize := 10
   174  	serviceRate := 50
   175  	serviceBurst := 100
   176  
   177  	entry := plugin.ConfigEntry{
   178  		Option: make(map[string]interface{}),
   179  	}
   180  	entry.Option["rateLimitIPLruSize"] = ipLruSize
   181  	entry.Option["rateLimitIPRate"] = ipRate
   182  	entry.Option["rateLimitIPBurst"] = ipBurst
   183  	entry.Option["rateLimitServiceLruSize"] = serviceLruSize
   184  	entry.Option["rateLimitServiceRate"] = serviceRate
   185  	entry.Option["rateLimitServiceBurst"] = serviceBurst
   186  
   187  	s := LRURate{}
   188  	if err := s.Initialize(&entry); err != nil {
   189  		t.Errorf("failed: %s", err)
   190  	} else {
   191  		t.Logf("pass")
   192  	}
   193  
   194  	t.Run("RateLimit_UNKNOWN", func(t *testing.T) {
   195  		count := 0
   196  		total := 2 * ipBurst
   197  		for i := 0; i < total; i++ {
   198  			if s.Allow(10, "19216811") {
   199  				count++
   200  			}
   201  		}
   202  
   203  		if count != total {
   204  			t.Errorf("failed, count: %d not %d", count, total)
   205  		} else {
   206  			t.Logf("pass")
   207  		}
   208  	})
   209  
   210  	t.Run("RateLimit_IP", func(t *testing.T) {
   211  		count := 0
   212  		total := ipBurst + 10
   213  		for i := 0; i < total; i++ {
   214  			if s.Allow(plugin.IPRatelimit, "19216811") {
   215  				count++
   216  			}
   217  		}
   218  
   219  		if count != ipBurst {
   220  			t.Errorf("failed, count: %d not %d", count, ipBurst)
   221  		} else {
   222  			t.Logf("pass")
   223  		}
   224  	})
   225  
   226  	t.Run("RateLimit_SERVICE_SERVICE", func(t *testing.T) {
   227  		count := 0
   228  		total := serviceBurst + 10
   229  		for i := 0; i < total; i++ {
   230  			if s.Allow(plugin.ServiceRatelimit, "hello_world") {
   231  				count++
   232  			}
   233  		}
   234  
   235  		if count != serviceBurst {
   236  			t.Errorf("failed, count: %d not %d", count, serviceBurst)
   237  		} else {
   238  			t.Logf("pass")
   239  		}
   240  	})
   241  
   242  	t.Run("RateLimit_SERVICE_SERVICEID", func(t *testing.T) {
   243  		count := 0
   244  		for i := 0; i < serviceBurst+10; i++ {
   245  			if s.Allow(plugin.ServiceRatelimit, "helloworld") {
   246  				count++
   247  			}
   248  		}
   249  
   250  		if count != serviceBurst {
   251  			t.Errorf("failed, count: %d not %d", count, serviceBurst)
   252  		} else {
   253  			t.Logf("pass")
   254  		}
   255  	})
   256  }