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