github.com/schumacherfm/hugo@v0.47.1/common/types/evictingqueue_test.go (about) 1 // Copyright 2017-present The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package types 15 16 import ( 17 "sync" 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestEvictingStringQueue(t *testing.T) { 24 assert := require.New(t) 25 26 queue := NewEvictingStringQueue(3) 27 28 assert.Equal("", queue.Peek()) 29 queue.Add("a") 30 queue.Add("b") 31 queue.Add("a") 32 assert.Equal("b", queue.Peek()) 33 queue.Add("b") 34 assert.Equal("b", queue.Peek()) 35 36 queue.Add("a") 37 queue.Add("b") 38 39 assert.Equal([]string{"b", "a"}, queue.PeekAll()) 40 assert.Equal("b", queue.Peek()) 41 queue.Add("c") 42 queue.Add("d") 43 // Overflowed, a should now be removed. 44 assert.Equal([]string{"d", "c", "b"}, queue.PeekAll()) 45 assert.Len(queue.PeekAllSet(), 3) 46 assert.True(queue.PeekAllSet()["c"]) 47 } 48 49 func TestEvictingStringQueueConcurrent(t *testing.T) { 50 var wg sync.WaitGroup 51 val := "someval" 52 53 queue := NewEvictingStringQueue(3) 54 55 for j := 0; j < 100; j++ { 56 wg.Add(1) 57 go func() { 58 defer wg.Done() 59 queue.Add(val) 60 v := queue.Peek() 61 if v != val { 62 t.Error("wrong val") 63 } 64 vals := queue.PeekAll() 65 if len(vals) != 1 || vals[0] != val { 66 t.Error("wrong val") 67 } 68 }() 69 } 70 wg.Wait() 71 }