Skip to content

Commit

Permalink
home: imp duration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Jun 15, 2021
1 parent 8fe7cb0 commit 6ed6599
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
3 changes: 1 addition & 2 deletions internal/home/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ type dnsConfig struct {
FiltersUpdateIntervalHours uint32 `yaml:"filters_update_interval"` // time period to update filters (in hours)
DnsfilterConf filtering.Config `yaml:",inline"`

// UpstreamTimeout is the timeout for querying upstream servers in
// seconds.
// UpstreamTimeout is the timeout for querying upstream servers.
UpstreamTimeout Duration `yaml:"upstream_timeout"`

// LocalDomainName is the domain name used for known internal hosts.
Expand Down
2 changes: 1 addition & 1 deletion internal/home/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/AdguardTeam/golibs/errors"
)

// Duration is a wrapper of time.Duration extending it's functionality.
// Duration is a wrapper for time.Duration providing functionality for encoding.
type Duration struct {
// time.Duration is embedded here to avoid implementing all the methods.
time.Duration
Expand Down
70 changes: 42 additions & 28 deletions internal/home/duration_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package home

import (
"bytes"
"encoding/json"
"strings"
"testing"
Expand All @@ -11,17 +12,17 @@ import (
yaml "gopkg.in/yaml.v2"
)

// durationMarshalTester is a helper struct to simplify testing different
// durationEncodingTester is a helper struct to simplify testing different
// Duration marshalling and unmarshalling cases.
type durationMarshalTester struct {
PtrMap map[string]*Duration `json:"ptr_map"`
PtrSlice []*Duration `json:"ptr_slice"`
PtrValue *Duration `json:"ptr_value"`
PtrArray [1]*Duration `json:"ptr_array"`
Map map[string]Duration `json:"map"`
Slice []Duration `json:"slice"`
Value Duration `json:"value"`
Array [1]Duration `json:"array"`
type durationEncodingTester struct {
PtrMap map[string]*Duration `json:"ptr_map" yaml:"ptr_map"`
PtrSlice []*Duration `json:"ptr_slice" yaml:"ptr_slice"`
PtrValue *Duration `json:"ptr_value" yaml:"ptr_value"`
PtrArray [1]*Duration `json:"ptr_array" yaml:"ptr_array"`
Map map[string]Duration `json:"map" yaml:"map"`
Slice []Duration `json:"slice" yaml:"slice"`
Value Duration `json:"value" yaml:"value"`
Array [1]Duration `json:"array" yaml:"array"`
}

const nl = "\n"
Expand All @@ -36,12 +37,12 @@ const (
`"value":"1ms",` +
`"array":["1ms"]` +
`}`
yamlStr = `ptrmap:` + nl +
yamlStr = `ptr_map:` + nl +
` dur: 1ms` + nl +
`ptrslice:` + nl +
`ptr_slice:` + nl +
`- 1ms` + nl +
`ptrvalue: 1ms` + nl +
`ptrarray:` + nl +
`ptr_value: 1ms` + nl +
`ptr_array:` + nl +
`- 1ms` + nl +
`map:` + nl +
` dur: 1ms` + nl +
Expand All @@ -54,28 +55,43 @@ const (

// checkFields verifies m's fields. It expects the m to be unmarshalled from
// one of the constant strings above.
func (m *durationMarshalTester) checkFields(t *testing.T, d Duration) {
func (m *durationEncodingTester) checkFields(t *testing.T, d Duration) {
// PtrMap

require.NotNil(t, m.PtrMap)

fromPtrMap, ok := m.PtrMap["dur"]
require.True(t, ok)
require.NotNil(t, fromPtrMap)

// PtrSlice

require.Len(t, m.PtrSlice, 1)

fromPtrSlice := m.PtrSlice[0]
require.NotNil(t, fromPtrSlice)

// PtrArray

fromPtrArray := m.PtrArray[0]
require.NotNil(t, fromPtrArray)

// PtrValue

require.NotNil(t, m.PtrValue)

// Map

var fromMap Duration
fromMap, ok = m.Map["dur"]
require.True(t, ok)

// Slice

require.Len(t, m.Slice, 1)

// Value Check

assert.Equal(t, d, *fromPtrMap)
assert.Equal(t, d, *fromPtrSlice)
assert.Equal(t, d, *m.PtrValue)
Expand All @@ -86,15 +102,15 @@ func (m *durationMarshalTester) checkFields(t *testing.T, d Duration) {
assert.Equal(t, d, m.Array[0])
}

// val is the default time.Duration value to be used throughout the tests of
// defaultTestDur is the default time.Duration value to be used throughout the tests of
// Duration.
const val = time.Millisecond
const defaultTestDur = time.Millisecond

func TestDuration_MarshalText(t *testing.T) {
d := Duration{val}
d := Duration{defaultTestDur}
dPtr := &d

m := durationMarshalTester{
m := durationEncodingTester{
PtrMap: map[string]*Duration{"dur": dPtr},
PtrSlice: []*Duration{dPtr},
PtrValue: dPtr,
Expand All @@ -105,7 +121,7 @@ func TestDuration_MarshalText(t *testing.T) {
Array: [1]Duration{d},
}

b := &strings.Builder{}
b := &bytes.Buffer{}
t.Run("json", func(t *testing.T) {
t.Cleanup(b.Reset)
err := json.NewEncoder(b).Encode(m)
Expand All @@ -126,16 +142,16 @@ func TestDuration_MarshalText(t *testing.T) {
data, err := d.MarshalText()
require.NoError(t, err)

assert.EqualValues(t, []byte(val.String()), data)
assert.EqualValues(t, []byte(defaultTestDur.String()), data)
})
}

func TestDuration_UnmarshalText(t *testing.T) {
d := Duration{val}
var m *durationMarshalTester
d := Duration{defaultTestDur}
var m *durationEncodingTester

t.Run("json", func(t *testing.T) {
m = &durationMarshalTester{}
m = &durationEncodingTester{}

r := strings.NewReader(jsonStr)
err := json.NewDecoder(r).Decode(m)
Expand All @@ -145,7 +161,7 @@ func TestDuration_UnmarshalText(t *testing.T) {
})

t.Run("yaml", func(t *testing.T) {
m = &durationMarshalTester{}
m = &durationEncodingTester{}

r := strings.NewReader(yamlStr)
err := yaml.NewDecoder(r).Decode(m)
Expand All @@ -164,8 +180,6 @@ func TestDuration_UnmarshalText(t *testing.T) {
})

t.Run("bad_data", func(t *testing.T) {
const wrongData = `abc`

assert.Error(t, (&Duration{}).UnmarshalText([]byte(wrongData)))
assert.Error(t, (&Duration{}).UnmarshalText([]byte(`abc`)))
})
}

0 comments on commit 6ed6599

Please sign in to comment.