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