2020-02-20 18:12:51 +00:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-10-22 08:58:18 +00:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2020-02-20 18:12:51 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-09 12:17:02 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-02-20 18:12:51 +00:00
|
|
|
)
|
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
// newTestQLogReader creates new *QLogReader for tests and registers the
|
|
|
|
// required cleanup functions.
|
|
|
|
func newTestQLogReader(t *testing.T, filesNum, linesNum int) (reader *QLogReader) {
|
|
|
|
t.Helper()
|
2020-02-20 18:12:51 +00:00
|
|
|
|
2021-02-09 16:38:31 +00:00
|
|
|
testFiles := prepareTestFiles(t, filesNum, linesNum)
|
2020-02-20 18:12:51 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
// Create the new QLogReader instance.
|
|
|
|
reader, err := NewQLogReader(testFiles)
|
2021-10-22 08:58:18 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-11 14:32:58 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
assert.NotNil(t, reader)
|
2021-10-22 08:58:18 +00:00
|
|
|
testutil.CleanupAndRequireSuccess(t, reader.Close)
|
2020-02-20 18:12:51 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
return reader
|
2020-02-20 18:12:51 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
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,
|
|
|
|
}}
|
2020-02-20 18:12:51 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
r := newTestQLogReader(t, tc.filesNum, tc.linesNum)
|
2020-02-20 18:12:51 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
// Seek to the start.
|
|
|
|
err := r.SeekStart()
|
2021-10-22 08:58:18 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-09 12:17:02 +00:00
|
|
|
|
|
|
|
// Read everything.
|
|
|
|
var read int
|
|
|
|
var line string
|
|
|
|
for err == nil {
|
|
|
|
line, err = r.ReadNext()
|
|
|
|
if err == nil {
|
|
|
|
assert.NotEmpty(t, line)
|
|
|
|
read++
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 18:12:51 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
require.Equal(t, io.EOF, err)
|
|
|
|
assert.Equal(t, tc.filesNum*tc.linesNum, read)
|
|
|
|
})
|
2020-02-20 18:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 12:43:28 +00:00
|
|
|
func TestQLogReader_Seek(t *testing.T) {
|
2021-02-09 12:17:02 +00:00
|
|
|
r := newTestQLogReader(t, 2, 10000)
|
2020-11-18 12:43:28 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
time string
|
|
|
|
want error
|
|
|
|
}{{
|
|
|
|
name: "not_too_old",
|
2021-02-09 12:17:02 +00:00
|
|
|
time: "2020-02-18T22:39:35.920973+03:00",
|
2020-11-18 12:43:28 +00:00
|
|
|
want: nil,
|
|
|
|
}, {
|
|
|
|
name: "old",
|
|
|
|
time: "2020-02-19T01:28:16.920973+03:00",
|
|
|
|
want: nil,
|
|
|
|
}, {
|
|
|
|
name: "first",
|
2021-02-09 12:17:02 +00:00
|
|
|
time: "2020-02-18T22:36:36.920973+03:00",
|
2020-11-18 12:43:28 +00: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",
|
2020-11-19 09:53:31 +00:00
|
|
|
want: ErrTSTooEarly,
|
2020-11-18 12:43:28 +00:00
|
|
|
}, {
|
|
|
|
name: "non-existent_far_ahead",
|
|
|
|
time: "2100-02-19T01:23:16.920973+03:00",
|
2020-11-19 09:53:31 +00:00
|
|
|
want: nil,
|
2020-11-18 12:43:28 +00:00
|
|
|
}, {
|
|
|
|
name: "non-existent_but_could",
|
2020-11-19 09:53:31 +00:00
|
|
|
time: "2020-02-18T22:36:37.000000+03:00",
|
|
|
|
want: ErrTSNotFound,
|
2020-11-18 12:43:28 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-03-11 17:36:54 +00:00
|
|
|
ts, err := time.Parse(time.RFC3339Nano, tc.time)
|
2021-10-22 08:58:18 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-09 12:17:02 +00:00
|
|
|
|
2021-03-11 17:36:54 +00:00
|
|
|
err = r.SeekTS(ts.UnixNano())
|
2021-03-22 13:46:36 +00:00
|
|
|
assert.ErrorIs(t, err, tc.want)
|
2020-11-18 12:43:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQLogReader_ReadNext(t *testing.T) {
|
2021-02-09 12:17:02 +00:00
|
|
|
const linesNum = 10
|
|
|
|
const filesNum = 1
|
|
|
|
r := newTestQLogReader(t, filesNum, linesNum)
|
2020-11-18 12:43:28 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
start int
|
|
|
|
want error
|
|
|
|
}{{
|
|
|
|
name: "ok",
|
|
|
|
start: 0,
|
|
|
|
want: nil,
|
|
|
|
}, {
|
|
|
|
name: "too_big",
|
2021-02-09 12:17:02 +00:00
|
|
|
start: linesNum + 1,
|
2020-11-18 12:43:28 +00:00
|
|
|
want: io.EOF,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
err := r.SeekStart()
|
2021-10-22 08:58:18 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-18 12:43:28 +00:00
|
|
|
|
|
|
|
for i := 1; i < tc.start; i++ {
|
2021-03-11 17:36:54 +00:00
|
|
|
_, err = r.ReadNext()
|
2021-10-22 08:58:18 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-18 12:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = r.ReadNext()
|
|
|
|
assert.Equal(t, tc.want, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|