Skip to content

Commit

Permalink
* querylog: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Nov 10, 2020
1 parent 774159c commit 32508a3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
9 changes: 4 additions & 5 deletions internal/querylog/qlog_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ type QLogFile struct {

// NewQLogFile initializes a new instance of the QLogFile
func NewQLogFile(path string) (*QLogFile, error) {
f, err := os.OpenFile(path, os.O_RDONLY, 0644)

f, err := os.OpenFile(path, os.O_RDONLY, 0o644)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -232,7 +231,7 @@ func (q *QLogFile) readNextLine(position int64) (string, int64, error) {

// Look for the end of the prev line
// This is where we'll read from
var startLine = int64(0)
startLine := int64(0)
for i := relativePos - 1; i >= 0; i-- {
if q.buffer[i] == '\n' {
startLine = i + 1
Expand Down Expand Up @@ -298,15 +297,15 @@ func (q *QLogFile) readProbeLine(position int64) (string, int64, int64, error) {

// Now start looking for the new line character starting
// from the relativePos and going left
var startLine = int64(0)
startLine := int64(0)
for i := relativePos - 1; i >= 0; i-- {
if buffer[i] == '\n' {
startLine = i + 1
break
}
}
// Looking for the end of line now
var endLine = int64(bufferLen)
endLine := int64(bufferLen)
lineEndIdx := endLine + seekPosition
for i := relativePos; i < int64(bufferLen); i++ {
if buffer[i] == '\n' {
Expand Down
29 changes: 29 additions & 0 deletions internal/querylog/qlog_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,32 @@ func TestQLogSeek(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, 1, depth)
}

func TestQLogSeek_ErrEndOfLog(t *testing.T) {
testDir := prepareTestDir()
t.Cleanup(func() {
_ = os.RemoveAll(testDir)
})

d := `{"T":"2020-08-31T18:44:23.911246629+03:00","QH":"wfqvjymurpwegyv","QT":"A","QC":"IN","CP":"","Answer":"","Result":{},"Elapsed":66286385,"Upstream":"tls://dns-unfiltered.adguard.com:853"}
{"T":"2020-08-31T18:44:25.376690873+03:00"}
{"T":"2020-08-31T18:44:25.382540454+03:00"}
`
f, err := ioutil.TempFile(testDir, "*.txt")
assert.Nil(t, err)
defer f.Close()

_, err = f.WriteString(d)
assert.Nil(t, err)

q, err := NewQLogFile(f.Name())
assert.Nil(t, err)
defer q.Close()

target, err := time.Parse(time.RFC3339, "2020-08-31T18:44:25.382540454+03:00")
assert.Nil(t, err)

_, depth, err := q.Seek(target.UnixNano() + int64(time.Second))
assert.Equal(t, ErrEndOfLog, err)
assert.Equal(t, 2, depth)
}

0 comments on commit 32508a3

Please sign in to comment.