AdGuardHome/internal/querylog/qlogreader_test.go
Eugene Burkov 3b2f5d7842 Pull request: 2704 local resolvers vol.1
Merge in DNS/adguard-home from 2704-local-addresses-vol.1 to master

Updates #2704.
Updates #2829.
Updates #2846.

Squashed commit of the following:

commit 9a49b3d27edcb30da7f16a065226907833b1dc81
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Mon Mar 22 15:39:17 2021 +0300

    aghnet: imp docs and logging

commit 74f95a29c55b9e732276601b0ecc63fb7c3a9f9e
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Mar 19 20:56:51 2021 +0300

    all: fix friday evening mistakes

commit 0e2066bc5c16ed807fa601780b99e154502361a9
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Mar 19 20:51:15 2021 +0300

    all: upd testify, imp code quality

commit 8237c50b670c58361ccf7adec3ff2452b1196677
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Mar 19 20:19:29 2021 +0300

    aghnet: imp test naming

commit 14eb1e189339554c0a6d38e2ba7a93917774ebab
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Mar 19 19:41:43 2021 +0300

    aghnet: isolate windows-specific functionality

commit d461ac8b18c187999da3e3aba116571b7ebe6785
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Mar 19 14:50:05 2021 +0300

    aghnet: imp code quality

commit d0ee01cb1f8613de2085c0f2f2f396e46beb52a5
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Fri Mar 19 11:59:10 2021 +0300

    all: mv funcs to agherr, mk system resolvers getter
2021-03-22 16:46:36 +03:00

156 lines
2.9 KiB
Go

package querylog
import (
"io"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// newTestQLogReader creates new *QLogReader for tests and registers the
// required cleanup functions.
func newTestQLogReader(t *testing.T, filesNum, linesNum int) (reader *QLogReader) {
t.Helper()
testFiles := prepareTestFiles(t, filesNum, linesNum)
// Create the new QLogReader instance.
reader, err := NewQLogReader(testFiles)
require.Nil(t, err)
assert.NotNil(t, reader)
t.Cleanup(func() {
assert.Nil(t, reader.Close())
})
return reader
}
func TestQLogReader(t *testing.T) {
testCases := []struct {
name string
filesNum int
linesNum int
}{{
name: "empty",
filesNum: 0,
linesNum: 0,
}, {
name: "one_file",
filesNum: 1,
linesNum: 10,
}, {
name: "multiple_files",
filesNum: 5,
linesNum: 10000,
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
r := newTestQLogReader(t, tc.filesNum, tc.linesNum)
// Seek to the start.
err := r.SeekStart()
require.Nil(t, err)
// Read everything.
var read int
var line string
for err == nil {
line, err = r.ReadNext()
if err == nil {
assert.NotEmpty(t, line)
read++
}
}
require.Equal(t, io.EOF, err)
assert.Equal(t, tc.filesNum*tc.linesNum, read)
})
}
}
func TestQLogReader_Seek(t *testing.T) {
r := newTestQLogReader(t, 2, 10000)
testCases := []struct {
name string
time string
want error
}{{
name: "not_too_old",
time: "2020-02-18T22:39:35.920973+03:00",
want: nil,
}, {
name: "old",
time: "2020-02-19T01:28:16.920973+03:00",
want: nil,
}, {
name: "first",
time: "2020-02-18T22:36:36.920973+03:00",
want: nil,
}, {
name: "last",
time: "2020-02-19T01:23:16.920973+03:00",
want: nil,
}, {
name: "non-existent_long_ago",
time: "2000-02-19T01:23:16.920973+03:00",
want: ErrTSTooEarly,
}, {
name: "non-existent_far_ahead",
time: "2100-02-19T01:23:16.920973+03:00",
want: nil,
}, {
name: "non-existent_but_could",
time: "2020-02-18T22:36:37.000000+03:00",
want: ErrTSNotFound,
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ts, err := time.Parse(time.RFC3339Nano, tc.time)
require.Nil(t, err)
err = r.SeekTS(ts.UnixNano())
assert.ErrorIs(t, err, tc.want)
})
}
}
func TestQLogReader_ReadNext(t *testing.T) {
const linesNum = 10
const filesNum = 1
r := newTestQLogReader(t, filesNum, linesNum)
testCases := []struct {
name string
start int
want error
}{{
name: "ok",
start: 0,
want: nil,
}, {
name: "too_big",
start: linesNum + 1,
want: io.EOF,
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := r.SeekStart()
require.Nil(t, err)
for i := 1; i < tc.start; i++ {
_, err = r.ReadNext()
require.Nil(t, err)
}
_, err = r.ReadNext()
assert.Equal(t, tc.want, err)
})
}
}