diff --git a/internal/home/config.go b/internal/home/config.go index fedb43ab357..d660ddb834a 100644 --- a/internal/home/config.go +++ b/internal/home/config.go @@ -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. diff --git a/internal/home/duration.go b/internal/home/duration.go index 50086f5e7e7..6e6ab79e041 100644 --- a/internal/home/duration.go +++ b/internal/home/duration.go @@ -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 diff --git a/internal/home/duration_test.go b/internal/home/duration_test.go index 4c728aadebe..afbff16d508 100644 --- a/internal/home/duration_test.go +++ b/internal/home/duration_test.go @@ -1,6 +1,7 @@ package home import ( + "bytes" "encoding/json" "strings" "testing" @@ -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" @@ -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 + @@ -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) @@ -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, @@ -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) @@ -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) @@ -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) @@ -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`))) }) }