Skip to content

Commit

Permalink
all: imp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Jan 9, 2024
1 parent 1cb404c commit 2da1228
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
20 changes: 10 additions & 10 deletions internal/aghalg/ringbuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
)

// elements is a helper function that returns n elements of the buffer.
func elements(b *aghalg.RingBuffer[int], n int, reverse bool) (es []int) {
func elements(b *aghalg.RingBuffer[int], n uint, reverse bool) (es []int) {
fn := b.Range
if reverse {
fn = b.ReverseRange
}

i := 0
var i uint
fn(func(e int) (cont bool) {
if i >= n {
return false
Expand All @@ -36,7 +36,7 @@ func TestNewRingBuffer(t *testing.T) {
for i := 0; i < 10; i++ {
b.Append(i)
}
assert.Equal(t, []int{5, 6, 7, 8, 9}, elements(b, int(b.Len()), false))
assert.Equal(t, []int{5, 6, 7, 8, 9}, elements(b, b.Len(), false))

b.Clear()
assert.Zero(t, b.Len())
Expand All @@ -46,8 +46,8 @@ func TestNewRingBuffer(t *testing.T) {
b := aghalg.NewRingBuffer[int](0)
for i := 0; i < 10; i++ {
b.Append(i)
bufLen := int(b.Len())
assert.Equal(t, 0, bufLen)
bufLen := b.Len()
assert.EqualValues(t, 0, bufLen)
assert.Empty(t, elements(b, bufLen, false))
assert.Empty(t, elements(b, bufLen, true))
}
Expand All @@ -57,8 +57,8 @@ func TestNewRingBuffer(t *testing.T) {
b := aghalg.NewRingBuffer[int](1)
for i := 0; i < 10; i++ {
b.Append(i)
bufLen := int(b.Len())
assert.Equal(t, 1, bufLen)
bufLen := b.Len()
assert.EqualValues(t, 1, bufLen)
assert.Equal(t, []int{i}, elements(b, bufLen, false))
assert.Equal(t, []int{i}, elements(b, bufLen, true))
}
Expand All @@ -74,7 +74,7 @@ func TestRingBuffer_Range(t *testing.T) {
name string
want []int
count int
length int
length uint
}{{
name: "three",
count: 3,
Expand All @@ -98,7 +98,7 @@ func TestRingBuffer_Range(t *testing.T) {
b.Append(i)
}

bufLen := int(b.Len())
bufLen := b.Len()
assert.Equal(t, tc.length, bufLen)

want := tc.want
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestRingBuffer_Range_increment(t *testing.T) {
for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
b.Append(i)
bufLen := int(b.Len())
bufLen := b.Len()
assert.Equal(t, tc.want, elements(b, bufLen, false))

slices.Reverse(tc.want)
Expand Down
12 changes: 10 additions & 2 deletions internal/querylog/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ func (l *queryLog) client(clientID, ip string, cache clientCache) (c *Client, er
// searchMemory looks up log records which are currently in the in-memory
// buffer. It optionally uses the client cache, if provided. It also returns
// the total amount of records in the buffer at the moment of searching.
// l.confMu is expected to be locked.
func (l *queryLog) searchMemory(params *searchParams, cache clientCache) (entries []*logEntry, total int) {
// We use this configuration check because a buffer can contain a single log
// record. See [newQueryLog].
if l.conf.MemSize == 0 {
return nil, 0
}

l.bufferLock.Lock()
defer l.bufferLock.Unlock()

Expand Down Expand Up @@ -76,8 +83,9 @@ func (l *queryLog) searchMemory(params *searchParams, cache clientCache) (entrie
return entries, int(l.buffer.Len())
}

// search - searches log entries in the query log using specified parameters
// returns the list of entries found + time of the oldest entry
// search searches log entries in memory buffer and log file using specified
// parameters and returns the list of entries found and the time of the oldest
// entry. l.confMu is expected to be locked.
func (l *queryLog) search(params *searchParams) (entries []*logEntry, oldest time.Time) {
start := time.Now()

Expand Down

0 comments on commit 2da1228

Please sign in to comment.