github.com/blend/go-sdk@v1.20220411.3/collections/sync_ring_buffer_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 TestSyncRingBuffer(t *testing.T) { 17 assert := assert.New(t) 18 19 buffer := NewSyncRingBuffer() 20 21 buffer.Enqueue(1) 22 assert.Equal(1, buffer.Len()) 23 assert.Equal(1, buffer.Peek()) 24 assert.Equal(1, buffer.PeekBack()) 25 26 buffer.Enqueue(2) 27 assert.Equal(2, buffer.Len()) 28 assert.Equal(1, buffer.Peek()) 29 assert.Equal(2, buffer.PeekBack()) 30 31 buffer.Enqueue(3) 32 assert.Equal(3, buffer.Len()) 33 assert.Equal(1, buffer.Peek()) 34 assert.Equal(3, buffer.PeekBack()) 35 36 buffer.Enqueue(4) 37 assert.Equal(4, buffer.Len()) 38 assert.Equal(1, buffer.Peek()) 39 assert.Equal(4, buffer.PeekBack()) 40 41 buffer.Enqueue(5) 42 assert.Equal(5, buffer.Len()) 43 assert.Equal(1, buffer.Peek()) 44 assert.Equal(5, buffer.PeekBack()) 45 46 buffer.Enqueue(6) 47 assert.Equal(6, buffer.Len()) 48 assert.Equal(1, buffer.Peek()) 49 assert.Equal(6, buffer.PeekBack()) 50 51 buffer.Enqueue(7) 52 assert.Equal(7, buffer.Len()) 53 assert.Equal(1, buffer.Peek()) 54 assert.Equal(7, buffer.PeekBack()) 55 56 buffer.Enqueue(8) 57 assert.Equal(8, buffer.Len()) 58 assert.Equal(1, buffer.Peek()) 59 assert.Equal(8, buffer.PeekBack()) 60 61 value := buffer.Dequeue() 62 assert.Equal(1, value) 63 assert.Equal(7, buffer.Len()) 64 assert.Equal(2, buffer.Peek()) 65 assert.Equal(8, buffer.PeekBack()) 66 67 value = buffer.Dequeue() 68 assert.Equal(2, value) 69 assert.Equal(6, buffer.Len()) 70 assert.Equal(3, buffer.Peek()) 71 assert.Equal(8, buffer.PeekBack()) 72 73 value = buffer.Dequeue() 74 assert.Equal(3, value) 75 assert.Equal(5, buffer.Len()) 76 assert.Equal(4, buffer.Peek()) 77 assert.Equal(8, buffer.PeekBack()) 78 79 value = buffer.Dequeue() 80 assert.Equal(4, value) 81 assert.Equal(4, buffer.Len()) 82 assert.Equal(5, buffer.Peek()) 83 assert.Equal(8, buffer.PeekBack()) 84 85 value = buffer.Dequeue() 86 assert.Equal(5, value) 87 assert.Equal(3, buffer.Len()) 88 assert.Equal(6, buffer.Peek()) 89 assert.Equal(8, buffer.PeekBack()) 90 91 value = buffer.Dequeue() 92 assert.Equal(6, value) 93 assert.Equal(2, buffer.Len()) 94 assert.Equal(7, buffer.Peek()) 95 assert.Equal(8, buffer.PeekBack()) 96 97 value = buffer.Dequeue() 98 assert.Equal(7, value) 99 assert.Equal(1, buffer.Len()) 100 assert.Equal(8, buffer.Peek()) 101 assert.Equal(8, buffer.PeekBack()) 102 103 value = buffer.Dequeue() 104 assert.Equal(8, value) 105 assert.Equal(0, buffer.Len()) 106 assert.Nil(buffer.Peek()) 107 assert.Nil(buffer.PeekBack()) 108 } 109 110 func TestSynchronizedRingBufferClear(t *testing.T) { 111 assert := assert.New(t) 112 113 buffer := NewSyncRingBuffer() 114 buffer.Enqueue(1) 115 buffer.Enqueue(1) 116 buffer.Enqueue(1) 117 buffer.Enqueue(1) 118 buffer.Enqueue(1) 119 buffer.Enqueue(1) 120 buffer.Enqueue(1) 121 buffer.Enqueue(1) 122 123 assert.Equal(8, buffer.Len()) 124 125 buffer.Clear() 126 assert.Equal(0, buffer.Len()) 127 assert.Nil(buffer.Peek()) 128 assert.Nil(buffer.PeekBack()) 129 } 130 131 func TestSynchronizedRingBufferAsSlice(t *testing.T) { 132 assert := assert.New(t) 133 134 buffer := NewSyncRingBuffer() 135 buffer.Enqueue(1) 136 buffer.Enqueue(2) 137 buffer.Enqueue(3) 138 buffer.Enqueue(4) 139 buffer.Enqueue(5) 140 141 contents := buffer.Contents() 142 assert.Len(contents, 5) 143 assert.Equal(1, contents[0]) 144 assert.Equal(2, contents[1]) 145 assert.Equal(3, contents[2]) 146 assert.Equal(4, contents[3]) 147 assert.Equal(5, contents[4]) 148 } 149 150 func TestSynchronizedRingBufferEach(t *testing.T) { 151 assert := assert.New(t) 152 153 buffer := NewSyncRingBuffer() 154 155 for x := 1; x < 17; x++ { 156 buffer.Enqueue(x) 157 } 158 159 var called int 160 buffer.Each(func(v interface{}) { 161 if typed, isTyped := v.(int); isTyped { 162 if typed == (called + 1) { 163 called++ 164 } 165 } 166 }) 167 168 assert.Equal(16, called) 169 } 170 171 func TestSynchronizedRingBufferDrain(t *testing.T) { 172 assert := assert.New(t) 173 174 buffer := NewSyncRingBuffer() 175 176 for x := 1; x < 17; x++ { 177 buffer.Enqueue(x) 178 } 179 180 assert.Equal(16, buffer.Len()) 181 182 var called int 183 buffer.Consume(func(v interface{}) { 184 if _, isTyped := v.(int); isTyped { 185 called++ 186 } 187 }) 188 189 assert.Equal(16, called) 190 assert.Zero(buffer.Len()) 191 }