gitlab.com/beacon-software/gadget@v0.0.0-20181217202115-54565ea1ed5e/collection/specialized/ratehashpriorityqueue_test.go (about) 1 package specialized 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 9 "gitlab.com/beacon-software/gadget/generator" 10 ) 11 12 func TestRateHashPriorityQueue_Size(t *testing.T) { 13 assert := assert.New(t) 14 q := NewRateHashPriorityQueue(1, time.Microsecond) 15 assert.Equal(0, q.Size()) 16 sameHash := generator.String(20) 17 q.Push(NewMockHashPriority(2, sameHash)) 18 assert.Equal(1, q.Size()) 19 q.Push(NewMockHashPriority(2, sameHash)) 20 assert.Equal(1, q.Size()) 21 q.Push(NewMockHashPriority(3, generator.String(20))) 22 assert.Equal(2, q.Size()) 23 q.Pop() 24 assert.Equal(1, q.Size()) 25 } 26 27 func TestRateHashPriorityQueue_Peek(t *testing.T) { 28 assert := assert.New(t) 29 // this needs to be slow enough that it does not elapse before size is called, 30 // but fast enough that Pop does not take too long to return 31 q := NewRateHashPriorityQueue(1, 10*time.Millisecond) 32 expected := NewMockHashPriority(3, generator.String(20)) 33 q.Push(expected) 34 actual, ok := q.Peek() 35 assert.True(ok) 36 assert.Equal(expected, actual) 37 assert.Equal(1, q.Size()) 38 q.Pop() 39 actual, ok = q.Peek() 40 assert.Nil(actual) 41 assert.False(ok) 42 assert.Equal(0, q.Size()) 43 } 44 45 func TestRateHashPriorityQueue_Stop(t *testing.T) { 46 assert := assert.New(t) 47 obj := NewRateHashPriorityQueue(1, time.Microsecond) 48 q, ok := obj.(*rhpQueue) 49 assert.True(ok) 50 // making sure Stop is reentrant and does not block forever. 51 q.Stop() 52 q.Stop() 53 } 54 55 func TestRateHashPriorityQueue_Channel(t *testing.T) { 56 assert := assert.New(t) 57 q := NewRateHashPriorityQueue(1, 1*time.Microsecond) 58 expected := NewMockHashPriority(3, generator.String(20)) 59 q.Push(expected) 60 var actual HashPriority 61 select { 62 case actual = <-q.Channel(): 63 // noop 64 case <-time.After(2 * time.Millisecond): 65 assert.Fail("should have gotten an element") 66 } 67 assert.Equal(expected, actual) 68 } 69 70 func TestRateHashPriorityQueue_Pop(t *testing.T) { 71 assert := assert.New(t) 72 // we have to get all the elements in before this time elapses once 73 q := NewRateHashPriorityQueue(1, 55*time.Millisecond) 74 for i := 0; i < 10; i++ { 75 q.Push(NewMockHashPriority(i, generator.String(20))) 76 } 77 for j := 9; j >= 0; j-- { 78 start := time.Now() 79 elm, ok := q.Pop() 80 assert.True(ok) 81 // we want to make sure we waited at least 50ms. Don't make this dead on 82 // with the rate limit as the timers are not millisecond accurate. 83 assert.True(time.Now().Sub(start) > 50*time.Millisecond) 84 assert.Equal(j, elm.GetPriority()) 85 } 86 } 87 88 func TestRateHashPriorityQueue_NoLimitPop(t *testing.T) { 89 assert := assert.New(t) 90 // we have to get all the elements in before this time elapses once 91 q := NewRateHashPriorityQueue(1, 50*time.Millisecond) 92 for i := 0; i < 10; i++ { 93 q.Push(NewMockHashPriority(i, generator.String(20))) 94 } 95 for j := 9; j >= 0; j-- { 96 start := time.Now() 97 elm, ok := q.NoLimitPop() 98 assert.True(time.Now().Sub(start) < time.Millisecond) 99 assert.True(ok) 100 assert.Equal(j, elm.GetPriority()) 101 } 102 }