"Fossies" - the Fresh Open Source Software Archive

Member "AdGuardHome-0.104.3/internal/querylog/qlog_reader_test.go" (19 Nov 2020, 5281 Bytes) of package /linux/misc/dns/AdGuardHome-0.104.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "qlog_reader_test.go": 0.104.1_vs_0.104.3.

    1 package querylog
    2 
    3 import (
    4     "errors"
    5     "io"
    6     "os"
    7     "testing"
    8     "time"
    9 
   10     "github.com/stretchr/testify/assert"
   11 )
   12 
   13 func TestQLogReaderEmpty(t *testing.T) {
   14     r, err := NewQLogReader([]string{})
   15     assert.Nil(t, err)
   16     assert.NotNil(t, r)
   17     defer r.Close()
   18 
   19     // seek to the start
   20     err = r.SeekStart()
   21     assert.Nil(t, err)
   22 
   23     line, err := r.ReadNext()
   24     assert.Equal(t, "", line)
   25     assert.Equal(t, io.EOF, err)
   26 }
   27 
   28 func TestQLogReaderOneFile(t *testing.T) {
   29     // let's do one small file
   30     count := 10
   31     filesCount := 1
   32 
   33     testDir := prepareTestDir()
   34     defer func() { _ = os.RemoveAll(testDir) }()
   35     testFiles := prepareTestFiles(testDir, filesCount, count)
   36 
   37     r, err := NewQLogReader(testFiles)
   38     assert.Nil(t, err)
   39     assert.NotNil(t, r)
   40     defer r.Close()
   41 
   42     // seek to the start
   43     err = r.SeekStart()
   44     assert.Nil(t, err)
   45 
   46     // read everything
   47     read := 0
   48     var line string
   49     for err == nil {
   50         line, err = r.ReadNext()
   51         if err == nil {
   52             assert.True(t, len(line) > 0)
   53             read += 1
   54         }
   55     }
   56 
   57     assert.Equal(t, count*filesCount, read)
   58     assert.Equal(t, io.EOF, err)
   59 }
   60 
   61 func TestQLogReaderMultipleFiles(t *testing.T) {
   62     // should be large enough
   63     count := 10000
   64     filesCount := 5
   65 
   66     testDir := prepareTestDir()
   67     defer func() { _ = os.RemoveAll(testDir) }()
   68     testFiles := prepareTestFiles(testDir, filesCount, count)
   69 
   70     r, err := NewQLogReader(testFiles)
   71     assert.Nil(t, err)
   72     assert.NotNil(t, r)
   73     defer r.Close()
   74 
   75     // seek to the start
   76     err = r.SeekStart()
   77     assert.Nil(t, err)
   78 
   79     // read everything
   80     read := 0
   81     var line string
   82     for err == nil {
   83         line, err = r.ReadNext()
   84         if err == nil {
   85             assert.True(t, len(line) > 0)
   86             read += 1
   87         }
   88     }
   89 
   90     assert.Equal(t, count*filesCount, read)
   91     assert.Equal(t, io.EOF, err)
   92 }
   93 
   94 func TestQLogReader_Seek(t *testing.T) {
   95     count := 10000
   96     filesCount := 2
   97 
   98     testDir := prepareTestDir()
   99     t.Cleanup(func() {
  100         _ = os.RemoveAll(testDir)
  101     })
  102     testFiles := prepareTestFiles(testDir, filesCount, count)
  103 
  104     r, err := NewQLogReader(testFiles)
  105     assert.Nil(t, err)
  106     assert.NotNil(t, r)
  107     t.Cleanup(func() {
  108         _ = r.Close()
  109     })
  110 
  111     testCases := []struct {
  112         name string
  113         time string
  114         want error
  115     }{{
  116         name: "not_too_old",
  117         time: "2020-02-19T04:04:56.920973+03:00",
  118         want: nil,
  119     }, {
  120         name: "old",
  121         time: "2020-02-19T01:28:16.920973+03:00",
  122         want: nil,
  123     }, {
  124         name: "first",
  125         time: "2020-02-19T04:09:55.920973+03:00",
  126         want: nil,
  127     }, {
  128         name: "last",
  129         time: "2020-02-19T01:23:16.920973+03:00",
  130         want: nil,
  131     }, {
  132         name: "non-existent_long_ago",
  133         time: "2000-02-19T01:23:16.920973+03:00",
  134         want: ErrTSTooEarly,
  135     }, {
  136         name: "non-existent_far_ahead",
  137         time: "2100-02-19T01:23:16.920973+03:00",
  138         want: nil,
  139     }, {
  140         name: "non-existent_but_could",
  141         time: "2020-02-18T22:36:37.000000+03:00",
  142         want: ErrTSNotFound,
  143     }}
  144 
  145     for _, tc := range testCases {
  146         t.Run(tc.name, func(t *testing.T) {
  147             timestamp, err := time.Parse(time.RFC3339Nano, tc.time)
  148             assert.Nil(t, err)
  149 
  150             err = r.Seek(timestamp.UnixNano())
  151             assert.True(t, errors.Is(err, tc.want), err)
  152         })
  153     }
  154 }
  155 
  156 func TestQLogReader_ReadNext(t *testing.T) {
  157     count := 10
  158     filesCount := 1
  159 
  160     testDir := prepareTestDir()
  161     t.Cleanup(func() {
  162         _ = os.RemoveAll(testDir)
  163     })
  164     testFiles := prepareTestFiles(testDir, filesCount, count)
  165 
  166     r, err := NewQLogReader(testFiles)
  167     assert.Nil(t, err)
  168     assert.NotNil(t, r)
  169     t.Cleanup(func() {
  170         _ = r.Close()
  171     })
  172 
  173     testCases := []struct {
  174         name  string
  175         start int
  176         want  error
  177     }{{
  178         name:  "ok",
  179         start: 0,
  180         want:  nil,
  181     }, {
  182         name:  "too_big",
  183         start: count + 1,
  184         want:  io.EOF,
  185     }}
  186 
  187     for _, tc := range testCases {
  188         t.Run(tc.name, func(t *testing.T) {
  189             err := r.SeekStart()
  190             assert.Nil(t, err, err)
  191 
  192             for i := 1; i < tc.start; i++ {
  193                 _, err := r.ReadNext()
  194                 assert.Nil(t, err)
  195             }
  196 
  197             _, err = r.ReadNext()
  198             assert.Equal(t, tc.want, err)
  199         })
  200     }
  201 }
  202 
  203 // TODO(e.burkov): Remove the tests below.  Make tests above more compelling.
  204 func TestQLogReaderSeek(t *testing.T) {
  205     // more or less big file
  206     count := 10000
  207     filesCount := 2
  208 
  209     testDir := prepareTestDir()
  210     defer func() { _ = os.RemoveAll(testDir) }()
  211     testFiles := prepareTestFiles(testDir, filesCount, count)
  212 
  213     r, err := NewQLogReader(testFiles)
  214     assert.Nil(t, err)
  215     assert.NotNil(t, r)
  216     defer r.Close()
  217 
  218     // CASE 1: NOT TOO OLD LINE
  219     testSeekLineQLogReader(t, r, 300)
  220 
  221     // CASE 2: OLD LINE
  222     testSeekLineQLogReader(t, r, count-300)
  223 
  224     // CASE 3: FIRST LINE
  225     testSeekLineQLogReader(t, r, 0)
  226 
  227     // CASE 4: LAST LINE
  228     testSeekLineQLogReader(t, r, count)
  229 
  230     // CASE 5: Seek non-existent (too low)
  231     err = r.Seek(123)
  232     assert.NotNil(t, err)
  233 
  234     // CASE 6: Seek non-existent (too high)
  235     ts, _ := time.Parse(time.RFC3339, "2100-01-02T15:04:05Z07:00")
  236     err = r.Seek(ts.UnixNano())
  237     assert.NotNil(t, err)
  238 }
  239 
  240 func testSeekLineQLogReader(t *testing.T, r *QLogReader, lineNumber int) {
  241     line, err := getQLogReaderLine(r, lineNumber)
  242     assert.Nil(t, err)
  243     ts := readQLogTimestamp(line)
  244     assert.NotEqual(t, uint64(0), ts)
  245 
  246     // try seeking to that line now
  247     err = r.Seek(ts)
  248     assert.Nil(t, err)
  249 
  250     testLine, err := r.ReadNext()
  251     assert.Nil(t, err)
  252     assert.Equal(t, line, testLine)
  253 }
  254 
  255 func getQLogReaderLine(r *QLogReader, lineNumber int) (string, error) {
  256     err := r.SeekStart()
  257     if err != nil {
  258         return "", err
  259     }
  260 
  261     for i := 1; i < lineNumber; i++ {
  262         _, err := r.ReadNext()
  263         if err != nil {
  264             return "", err
  265         }
  266     }
  267     return r.ReadNext()
  268 }