github.com/blend/go-sdk@v1.20220411.3/collections/channel_queue_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package collections 9 10 import ( 11 "testing" 12 13 "github.com/blend/go-sdk/assert" 14 ) 15 16 func TestConcurrentQueue(t *testing.T) { 17 a := assert.New(t) 18 19 q := NewChannelQueueWithCapacity(4) 20 a.Empty(q.Contents()) 21 a.Nil(q.Dequeue()) 22 a.Equal(0, q.Len()) 23 24 q.Enqueue("foo") 25 a.Equal(1, q.Len()) 26 a.Equal("foo", q.Peek()) 27 a.Equal("foo", q.PeekBack()) 28 29 q.Enqueue("bar") 30 a.Equal(2, q.Len()) 31 32 q.Enqueue("baz") 33 a.Equal(3, q.Len()) 34 35 q.Enqueue("fizz") 36 a.Equal(q.Len(), 4) 37 38 values := q.Contents() 39 a.Len(values, 4) 40 a.Equal("foo", values[0]) 41 a.Equal("bar", values[1]) 42 a.Equal("baz", values[2]) 43 a.Equal("fizz", values[3]) 44 45 shouldBeFoo := q.Dequeue() 46 a.Equal("foo", shouldBeFoo) 47 a.Equal(q.Len(), 3) 48 49 shouldBeBar := q.Dequeue() 50 a.Equal("bar", shouldBeBar) 51 a.Equal(2, q.Len()) 52 53 shouldBeBaz := q.Dequeue() 54 a.Equal("baz", shouldBeBaz) 55 a.Equal(1, q.Len()) 56 57 shouldBeFizz := q.Dequeue() 58 a.Equal("fizz", shouldBeFizz) 59 a.Equal(0, q.Len()) 60 61 q.Enqueue("foo") 62 a.Equal(1, q.Len()) 63 q.Clear() 64 a.Equal(0, q.Len()) 65 66 q.Enqueue("foo") 67 q.Enqueue("bar") 68 q.Enqueue("baz") 69 70 var items []string 71 q.Consume(func(v Any) { 72 items = append(items, v.(string)) 73 }) 74 a.Equal(0, q.Len()) 75 a.Len(items, 3) 76 a.Equal("foo", items[0]) 77 a.Equal("bar", items[1]) 78 a.Equal("baz", items[2]) 79 80 q.Enqueue("foo") 81 q.Enqueue("bar") 82 q.Enqueue("baz") 83 84 items = []string{} 85 q.Each(func(v Any) { 86 items = append(items, v.(string)) 87 }) 88 a.Equal(3, q.Len()) 89 a.Len(items, 3) 90 a.Equal("foo", items[0]) 91 a.Equal("bar", items[1]) 92 a.Equal("baz", items[2]) 93 94 contents := q.Drain() 95 a.Len(contents, 3) 96 }