Remove unused vars in buffer.go (#15553)

This commit is contained in:
Vitor Enes
2022-08-22 19:22:06 +00:00
committed by GitHub
parent 6caba42ec1
commit e66ead445d
2 changed files with 1 additions and 126 deletions
+1 -51
View File
@@ -74,10 +74,6 @@ type CircularBuffer struct {
sync.Mutex
*log.Entry
cfg bufferConfig
events []Event
start int
end int
size int
init, closed bool
watchers *watcherTree
}
@@ -97,10 +93,6 @@ func NewCircularBuffer(opts ...BufferOption) *CircularBuffer {
trace.Component: teleport.ComponentBuffer,
}),
cfg: cfg,
events: make([]Event, cfg.capacity),
start: -1,
end: -1,
size: 0,
watchers: newWatcherTree(),
}
}
@@ -130,12 +122,6 @@ func (c *CircularBuffer) clear() {
w.closeWatcher()
})
c.watchers = newWatcherTree()
c.start = -1
c.end = -1
c.size = 0
for i := 0; i < len(c.events); i++ {
c.events[i] = Event{}
}
}
// SetInit puts the buffer into an initialized state if it isn't already. Any watchers already queued
@@ -177,29 +163,6 @@ func (c *CircularBuffer) Close() error {
return nil
}
// Events returns a copy of records as arranged from start to end
func (c *CircularBuffer) Events() []Event {
c.Lock()
defer c.Unlock()
return c.eventsCopy()
}
// eventsCopy returns a copy of events as arranged from start to end
func (c *CircularBuffer) eventsCopy() []Event {
if c.size == 0 {
return nil
}
var out []Event
for i := 0; i < c.size; i++ {
index := (c.start + i) % len(c.events)
if out == nil {
out = make([]Event, 0, c.size)
}
out = append(out, c.events[index])
}
return out
}
// Emit emits events to currently registered watchers and stores them to
// the buffer. Panics if called before SetInit(), and returns false if called
// after Close().
@@ -221,19 +184,6 @@ func (c *CircularBuffer) emit(r Event) {
if !c.init {
panic("push called on uninitialized buffer instance")
}
if c.size == 0 {
c.start = 0
c.end = 0
c.size = 1
} else if c.size < len(c.events) {
c.end = (c.end + 1) % len(c.events)
c.events[c.end] = r
c.size++
} else {
c.end = c.start
c.start = (c.start + 1) % len(c.events)
}
c.events[c.end] = r
c.fanOutEvent(r)
}
@@ -287,7 +237,7 @@ func (c *CircularBuffer) NewWatcher(ctx context.Context, watch Watch) (Watcher,
}
if watch.QueueSize == 0 {
watch.QueueSize = len(c.events)
watch.QueueSize = c.cfg.capacity
}
if len(watch.Prefixes) == 0 {
-75
View File
@@ -27,29 +27,6 @@ import (
"github.com/stretchr/testify/require"
)
// TestBufferSizes tests various combinations of various
// buffer sizes and lists
func TestBufferSizes(t *testing.T) {
list(t, 1, 100)
list(t, 2, 100)
list(t, 3, 100)
list(t, 4, 100)
}
// TestBufferSizesReset tests various combinations of various
// buffer sizes and lists with clear.
func TestBufferSizesReset(t *testing.T) {
b := NewCircularBuffer(
BufferCapacity(1),
)
defer b.Close()
b.SetInit()
listWithBuffer(t, b, 1, 100)
b.Clear()
listWithBuffer(t, b, 1, 100)
}
// TestWatcherSimple tests scenarios with watchers
func TestWatcherSimple(t *testing.T) {
ctx := context.Background()
@@ -350,55 +327,3 @@ func TestWatcherTree(t *testing.T) {
require.Equal(t, wt.rm(w2), true)
}
func makeIDs(size int) []int64 {
out := make([]int64, size)
for i := 0; i < size; i++ {
out[i] = int64(i)
}
return out
}
func expectEvents(t *testing.T, b *CircularBuffer, ids []int64) {
events := b.Events()
if len(ids) == 0 {
require.Equal(t, len(events), 0)
return
}
require.Empty(t, cmp.Diff(toIDs(events), ids))
}
func toIDs(e []Event) []int64 {
var out []int64
for i := 0; i < len(e); i++ {
out = append(out, e[i].Item.ID)
}
return out
}
func list(t *testing.T, bufferSize int, listSize int) {
b := NewCircularBuffer(
BufferCapacity(bufferSize),
)
defer b.Close()
b.SetInit()
listWithBuffer(t, b, bufferSize, listSize)
}
func listWithBuffer(t *testing.T, b *CircularBuffer, bufferSize int, listSize int) {
// empty by default
expectEvents(t, b, nil)
elements := makeIDs(listSize)
// push through all elements of the list and make sure
// the slice always matches
for i := 0; i < len(elements); i++ {
b.Emit(Event{Item: Item{ID: elements[i]}})
sliceEnd := i + 1 - bufferSize
if sliceEnd < 0 {
sliceEnd = 0
}
expectEvents(t, b, elements[sliceEnd:i+1])
}
}