github.com/iotexproject/iotex-core@v1.14.1-rc1/pkg/counter/counter_test.go (about) 1 // Copyright (c) 2018 IoTeX 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package counter 7 8 import ( 9 "testing" 10 "time" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestSlidingWindowCounter1(t *testing.T) { 16 c := NewSlidingWindowCounter(time.Second, 100*time.Millisecond) 17 for i := 0; i < 10; i++ { 18 c.Increment() 19 } 20 assert.Equal(t, uint64(10), c.Count()) 21 assert.Equal(t, uint64(10), c.Count()) 22 time.Sleep(200 * time.Millisecond) 23 assert.Equal(t, uint64(10), c.Count()) 24 time.Sleep(1000 * time.Millisecond) 25 assert.Equal(t, uint64(0), c.Count()) 26 } 27 28 func TestSlidingWindowCounter2(t *testing.T) { 29 c := NewSlidingWindowCounter(time.Second, 100*time.Millisecond) 30 for i := 0; i < 15; i++ { 31 time.Sleep(110 * time.Millisecond) 32 c.Increment() 33 } 34 assert.Equal(t, uint64(10), c.Count()) 35 for c.Count() > 0 { 36 time.Sleep(100 * time.Millisecond) 37 } 38 } 39 40 func TestSlidingWindowCounter3(t *testing.T) { 41 c := NewSlidingWindowCounter(time.Second, 100*time.Millisecond) 42 for i := 0; i < 150; i++ { 43 time.Sleep(10 * time.Millisecond) 44 c.Increment() 45 } 46 for _, slot := range c.window { 47 assert.True(t, slot > 0) 48 } 49 }