vitess.io/vitess@v0.16.2/go/ratelimiter/ratelimiter_test.go (about) 1 /* 2 Copyright 2019 The Vitess 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 ratelimiter 18 19 import ( 20 "testing" 21 "time" 22 ) 23 24 func TestLimiter1(t *testing.T) { 25 rl := NewRateLimiter(1, 10*time.Millisecond) 26 result := rl.Allow() 27 if !result { 28 t.Error("Allow: false, want true") 29 } 30 result = rl.Allow() 31 if result { 32 t.Error("Allow: true, want false") 33 } 34 35 time.Sleep(11 * time.Millisecond) 36 result = rl.Allow() 37 if !result { 38 t.Error("Allow: false, want true") 39 } 40 result = rl.Allow() 41 if result { 42 t.Error("Allow: true, want false") 43 } 44 } 45 46 func TestLimiter2(t *testing.T) { 47 rl := NewRateLimiter(2, 10*time.Millisecond) 48 var result bool 49 for i := 0; i < 2; i++ { 50 result = rl.Allow() 51 if !result { 52 t.Errorf("Allow(%d): false, want true", i) 53 } 54 } 55 result = rl.Allow() 56 if result { 57 t.Error("Allow: true, want false") 58 } 59 60 time.Sleep(11 * time.Millisecond) 61 for i := 0; i < 2; i++ { 62 result = rl.Allow() 63 if !result { 64 t.Errorf("Allow(%d): false, want true", i) 65 } 66 } 67 result = rl.Allow() 68 if result { 69 t.Error("Allow: true, want false") 70 } 71 }