Skip to content

Commit

Permalink
Merge branch 'master' into revert-locales
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Feb 10, 2021
2 parents 00b1978 + 9df0935 commit d8c2bfe
Show file tree
Hide file tree
Showing 11 changed files with 353 additions and 275 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ and this project adheres to
- Improved HTTP requests handling and timeouts ([#2343]).
- Our snap package now uses the `core20` image as its base ([#2306]).
- New build system and various internal improvements ([#2271], [#2276], [#2297],
[#2509], [#2552]).
[#2509], [#2552], [#2639], [#2646]).

### Deprecated

Expand Down Expand Up @@ -107,6 +107,8 @@ and this project adheres to
[#2552]: https://github.com/AdguardTeam/AdGuardHome/issues/2552
[#2589]: https://github.com/AdguardTeam/AdGuardHome/issues/2589
[#2630]: https://github.com/AdguardTeam/AdGuardHome/issues/2630
[#2639]: https://github.com/AdguardTeam/AdGuardHome/issues/2639
[#2646]: https://github.com/AdguardTeam/AdGuardHome/issues/2646

## [v0.104.3] - 2020-11-19

Expand Down
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
Loading

0 comments on commit d8c2bfe

Please sign in to comment.