Skip to content

Commit

Permalink
all: fix readability, imp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Feb 9, 2021
1 parent 1231a82 commit 484f477
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 82 deletions.
45 changes: 45 additions & 0 deletions internal/aghtest/os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package aghtest

import (
"io/ioutil"
"os"
"runtime"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// PrepareTestDir returns the full path to temporary created directory and
// registers the appropriate cleanup for *t.
func PrepareTestDir(t *testing.T) (dir string) {
t.Helper()

wd, err := os.Getwd()
require.Nil(t, err)

dir, err = ioutil.TempDir(wd, "agh-test")
require.Nil(t, err)
require.NotEmpty(t, dir)

t.Cleanup(func() {
// TODO(e.burkov): Replace with t.TempDir methods after updating
// go version to 1.15.
start := time.Now()
for {
err := os.RemoveAll(dir)
if err == nil {
break
}

if runtime.GOOS != "windows" || time.Since(start) >= 500*time.Millisecond {
break
}
time.Sleep(5 * time.Millisecond)
}
assert.Nil(t, err)
})

return dir
}
43 changes: 4 additions & 39 deletions internal/querylog/qlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package querylog

import (
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
"runtime"
"sort"
"testing"
"time"
Expand All @@ -24,38 +21,6 @@ func TestMain(m *testing.M) {
aghtest.DiscardLogOutput(m)
}

func prepareTestDir(t *testing.T) string {
t.Helper()

wd, err := os.Getwd()
require.Nil(t, err)

dir, err := ioutil.TempDir(wd, "agh-tests")
require.Nil(t, err)
require.NotEmpty(t, dir)

t.Cleanup(func() {
// TODO(e.burkov): Replace with t.TempDir methods after updating
// go version to 1.15.
start := time.Now()
for {
err := os.RemoveAll(dir)
if err == nil {
break
}

if runtime.GOOS != "windows" || time.Since(start) >= 500*time.Millisecond {
break
}
time.Sleep(5 * time.Millisecond)
}

assert.Nil(t, err)
})

return dir
}

// TestQueryLog tests adding and loading (with filtering) entries from disk and
// memory.
func TestQueryLog(t *testing.T) {
Expand All @@ -64,7 +29,7 @@ func TestQueryLog(t *testing.T) {
FileEnabled: true,
Interval: 1,
MemSize: 100,
BaseDir: prepareTestDir(t),
BaseDir: aghtest.PrepareTestDir(t),
})

// Add disk entries.
Expand Down Expand Up @@ -166,7 +131,7 @@ func TestQueryLogOffsetLimit(t *testing.T) {
Enabled: true,
Interval: 1,
MemSize: 100,
BaseDir: prepareTestDir(t),
BaseDir: aghtest.PrepareTestDir(t),
})

const (
Expand Down Expand Up @@ -240,7 +205,7 @@ func TestQueryLogMaxFileScanEntries(t *testing.T) {
FileEnabled: true,
Interval: 1,
MemSize: 100,
BaseDir: prepareTestDir(t),
BaseDir: aghtest.PrepareTestDir(t),
})

const entNum = 10
Expand Down Expand Up @@ -268,7 +233,7 @@ func TestQueryLogFileDisabled(t *testing.T) {
FileEnabled: false,
Interval: 1,
MemSize: 2,
BaseDir: prepareTestDir(t),
BaseDir: aghtest.PrepareTestDir(t),
})

addEntry(l, "example1.org", net.IPv4(1, 1, 1, 1), net.IPv4(2, 2, 2, 1))
Expand Down
17 changes: 12 additions & 5 deletions internal/querylog/qlogfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ import (
"testing"
"time"

"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// prepareTestFiles prepares several test query log files, each with the
// specified lines count.
func prepareTestFiles(t *testing.T, dir string, filesNum, linesNum int) []string {
func prepareTestFiles(t *testing.T, filesNum, linesNum int) []string {
t.Helper()

if filesNum == 0 {
return []string{}
}

const strV = "\"%s\""
const nl = "\n"
const format = `{"IP":` + strV + `,"T":` + strV + `,` +
Expand All @@ -31,6 +36,8 @@ func prepareTestFiles(t *testing.T, dir string, filesNum, linesNum int) []string
lineTime, _ := time.Parse(time.RFC3339Nano, "2020-02-18T22:36:35.920973+03:00")
lineIP := uint32(0)

dir := aghtest.PrepareTestDir(t)

files := make([]string, filesNum)
for j := range files {
f, err := ioutil.TempFile(dir, "*.txt")
Expand All @@ -56,18 +63,18 @@ func prepareTestFiles(t *testing.T, dir string, filesNum, linesNum int) []string

// prepareTestFile prepares a test query log file with the specified number of
// lines.
func prepareTestFile(t *testing.T, dir string, linesCount int) string {
func prepareTestFile(t *testing.T, linesCount int) string {
t.Helper()

return prepareTestFiles(t, dir, 1, linesCount)[0]
return prepareTestFiles(t, 1, linesCount)[0]
}

// newTestQLogFile creates new *QLogFile for tests and registers the required
// cleanup functions.
func newTestQLogFile(t *testing.T, linesNum int) (file *QLogFile) {
t.Helper()

testFile := prepareTestFile(t, prepareTestDir(t), linesNum)
testFile := prepareTestFile(t, linesNum)

// Create the new QLogFile instance.
file, err := NewQLogFile(testFile)
Expand Down Expand Up @@ -275,7 +282,7 @@ func TestQLogFile(t *testing.T) {
}

func NewTestQLogFileData(t *testing.T, data string) (file *QLogFile) {
f, err := ioutil.TempFile(prepareTestDir(t), "*.txt")
f, err := ioutil.TempFile(aghtest.PrepareTestDir(t), "*.txt")
require.Nil(t, err)
t.Cleanup(func() {
assert.Nil(t, f.Close())
Expand Down
2 changes: 1 addition & 1 deletion internal/querylog/qlogreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func newTestQLogReader(t *testing.T, filesNum, linesNum int) (reader *QLogReader) {
t.Helper()

testFiles := prepareTestFiles(t, prepareTestDir(t), filesNum, linesNum)
testFiles := prepareTestFiles(t, filesNum, linesNum)

// Create the new QLogReader instance.
reader, err := NewQLogReader(testFiles)
Expand Down
28 changes: 2 additions & 26 deletions internal/util/autohosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"io/ioutil"
"net"
"os"
"runtime"
"strings"
"testing"
"time"
Expand All @@ -22,32 +21,9 @@ func TestMain(m *testing.M) {
func prepareTestFile(t *testing.T) (f *os.File) {
t.Helper()

wd, err := os.Getwd()
require.Nil(t, err)

dir, err := ioutil.TempDir(wd, "agh-tests")
require.Nil(t, err)
require.NotEmpty(t, dir)

t.Cleanup(func() {
// TODO(e.burkov): Replace with t.TempDir methods after updating
// go version to 1.15.
start := time.Now()
for {
err := os.RemoveAll(dir)
if err == nil {
break
}

if runtime.GOOS != "windows" || time.Since(start) >= 500*time.Millisecond {
break
}
time.Sleep(5 * time.Millisecond)
}
assert.Nil(t, err)
})
dir := aghtest.PrepareTestDir(t)

f, err = ioutil.TempFile(dir, "")
f, err := ioutil.TempFile(dir, "")
require.Nil(t, err)
require.NotNil(t, f)
t.Cleanup(func() {
Expand Down
15 changes: 4 additions & 11 deletions internal/util/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@ import (
)

func TestSplitNext(t *testing.T) {
const (
a = "a"
b = "b"
c = "c"
sp = " "
cl = ","
)
s := sp + a + cl + b + sp + cl + sp + c + sp
s := " a,b , c "

for _, want := range []string{"a", "b", "c"} {
assert.Equal(t, want, SplitNext(&s, ','))
}
assert.Equal(t, "a", SplitNext(&s, ','))
assert.Equal(t, "b", SplitNext(&s, ','))
assert.Equal(t, "c", SplitNext(&s, ','))
require.Empty(t, s)
}

0 comments on commit 484f477

Please sign in to comment.