File: time_unmarshal_test.go

package info (click to toggle)
golang-github-minio-pkg 3.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,500 kB
  • sloc: xml: 37; makefile: 35; asm: 22
file content (144 lines) | stat: -rw-r--r-- 3,479 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 2015-2024 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package xtime

import (
	"bytes"
	"encoding/json"
	"testing"
	"time"

	"github.com/tinylib/msgp/msgp"
	"gopkg.in/yaml.v3"
)

type testDuration struct {
	A               string    `yaml:"a" json:"a"`
	Dur             Duration  `yaml:"dur" json:"dur"`
	DurationPointer *Duration `yaml:"durationPointer" json:"durationPointer"`
}

func TestDuration_Unmarshal(t *testing.T) {
	jsonData := []byte(`{"a":"1s","dur":"1w1s","durationPointer":"7d1s"}`)
	yamlData := []byte(`a: 1s
dur: 1w1s
durationPointer: 7d1s`)
	yamlTest := testDuration{}
	if err := yaml.Unmarshal(yamlData, &yamlTest); err != nil {
		t.Fatal(err)
	}
	jsonTest := testDuration{}
	if err := json.Unmarshal(jsonData, &jsonTest); err != nil {
		t.Fatal(err)
	}

	jsonData = []byte(`{"a":"1s","dur":"1w1s"}`)
	yamlData = []byte(`a: 1s
dur: 1w1s`)

	if err := yaml.Unmarshal(yamlData, &yamlTest); err != nil {
		t.Fatal(err)
	}
	if err := json.Unmarshal(jsonData, &jsonTest); err != nil {
		t.Fatal(err)
	}
}

func TestMarshalUnmarshalDuration(t *testing.T) {
	v := Duration(time.Hour)
	var vn Duration
	bts, err := v.MarshalMsg(nil)
	if err != nil {
		t.Fatal(err)
	}
	left, err := vn.UnmarshalMsg(bts)
	if err != nil {
		t.Fatal(err)
	}
	if len(left) > 0 {
		t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left)
	}
	if vn != v {
		t.Errorf("v=%#v; want=%#v", vn, v)
	}

	left, err = msgp.Skip(bts)
	if err != nil {
		t.Fatal(err)
	}
	if len(left) > 0 {
		t.Errorf("%d bytes left over after Skip(): %q", len(left), left)
	}
}

func TestEncodeDecodeDuration(t *testing.T) {
	v := Duration(time.Hour)
	var buf bytes.Buffer
	msgp.Encode(&buf, &v)

	m := v.Msgsize()
	if buf.Len() > m {
		t.Log("WARNING: TestEncodeDecodeDuration Msgsize() is inaccurate")
	}

	var vn Duration
	err := msgp.Decode(&buf, &vn)
	if err != nil {
		t.Error(err)
	}
	if vn != v {
		t.Errorf("v=%#v; want=%#v", vn, v)
	}
	buf.Reset()
	msgp.Encode(&buf, &v)
	err = msgp.NewReader(&buf).Skip()
	if err != nil {
		t.Error(err)
	}
}

func TestDuration_Marshal(t *testing.T) {
	type testDuration struct {
		A               Duration  `json:"a" yaml:"a"`
		Dur             Duration  `json:"dur" yaml:"dur"`
		DurationPointer *Duration `json:"durationPointer,omitempty" yaml:"durationPointer,omitempty"`
	}

	d1 := Duration(time.Second)
	d2 := Duration(0)
	d3 := Duration(time.Hour*24*7 + time.Second)

	testData := testDuration{
		A:               d1,
		Dur:             d2,
		DurationPointer: &d3,
	}

	yamlData, err := yaml.Marshal(&testData)
	if err != nil {
		t.Fatalf("Failed to marshal YAML: %v", err)
	}

	expected := `a: 1s
dur: 0s
durationPointer: 168h0m1s
`
	if string(yamlData) != expected {
		t.Errorf("Expected:\n%s\nGot:\n%s", expected, string(yamlData))
	}
}