2020-02-20 18:12:51 +00:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
2020-11-18 12:43:28 +00:00
|
|
|
"errors"
|
2020-02-20 18:12:51 +00:00
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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)
|
|
|
|
require.Nil(t, err)
|
2021-03-11 14:32:58 +00:00
|
|
|
|
2021-02-09 12:17:02 +00:00
|
|
|
assert.NotNil(t, reader)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
assert.Nil(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()
|
|
|
|
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++
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
timestamp, err := time.Parse(time.RFC3339Nano, tc.time)
|
2021-03-11 14:32:58 +00:00
|
|
|
require.Nil(t, err)
|
2021-02-09 12:17:02 +00:00
|
|
|
|
2020-12-07 11:32:06 +00:00
|
|
|
err = r.SeekTS(timestamp.UnixNano())
|
2021-02-09 12:17:02 +00:00
|
|
|
assert.True(t, errors.Is(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-03-11 14:32:58 +00:00
|
|
|
require.Nil(t, err)
|
2020-11-18 12:43:28 +00:00
|
|
|
|
|
|
|
for i := 1; i < tc.start; i++ {
|
|
|
|
_, err := r.ReadNext()
|
2021-03-11 14:32:58 +00:00
|
|
|
require.Nil(t, err)
|
2020-11-18 12:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = r.ReadNext()
|
|
|
|
assert.Equal(t, tc.want, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|